Add talloc_get_type_abort as a macro
commit8ac3872b0bf650f0dc7b16d07d45398f39241b2a
authorSam Liddicott <sam@liddicott.com>
Fri, 7 Nov 2008 15:39:56 +0000 (7 15:39 +0000)
committerSam Liddicott <sam@liddicott.com>
Wed, 10 Dec 2008 10:10:51 +0000 (10 10:10 +0000)
tree32e2b6d538f8afff4b43b773ec93270024286363
parentf5d853a34ec27341725756a3a1a084420a72abad
Add talloc_get_type_abort as a macro

Usage:

struct smb_read *io = talloc_get_type_abort(r, struct smb_read);

will abort if r is NULL or not "struct smb_read".
When aborting, a helpful error would be shown, like:

  Talloc type mismatch at xx.c:24, expected struct smb_read,
  got struct smbcli_request

Based on vlendec's talloc_get_type_abort.

As abort is application specific, not library specific, the abort action
should be specificed by the application.

Implemented talloc_get_type_abort as a macro with the abort action
defined by default as TALLOC_ABORT(text), but can be overridden for
the application in any C file to specify an application specific abort action.

Because it is implemented as a macro and not a function there is no need
to change the talloc library api in any way at all, no need to link talloc.c
and the talloc library with an application specific abort method,
no need to define system wide talloc_abort handlers, and
so that the location of the offending type mismatch can be
more easily shown.

The abort behaviour defaults to
 #define TALLOC_ABORT(why) (puts(why),abort())

But the TALLOC_ABORT(why) may be defined before
talloc_get_type_abort is used. For smb applications
where smb_panic is linked, I suggest that:

 #define TALLOC_ABORT(why) smb_panic(why)

be defined in appropriate calling contexts in order to get
the error text shown in the usual way.

NOTE:
Because talloc_get_type_abort is a simple inline expression you
probably won't be able to define TALLOC_ABORT in terms
of macros that declare blocks, e.g. do { } while(0),
which would include samba's DEBUG macro.

The only bug is that the talloc_asprintf allocated error
message ought to be free'd by the ABORT function, but as that
function will typically quit the process it doesn't really matter.
lib/talloc/talloc.h
lib/util/util.h