[PATCH 1/1] Add talloc_get_type_abort as a macro
Amin Azez
azez at ufomechanic.net
Mon Feb 4 10:12:18 GMT 2008
Metze: I could at least move formatting of the mismatch warning into
talloc.c, and the caller can provide their own abort_fn when using
talloc_get_type_abort, then we can do:
talloc_get_type_abort(ptr, struct smb_read, smb_abort);
#define talloc_get_type_abort(ptr, type, abort_fn) (type *)(\
talloc_check_name(ptr, #type) || \
(abort_fn(talloc_get_type_error( \
__location__, #type, ptr)), NULL))
Is that a more amenable representation?
Sam
* Amin Azez wrote, On 01/02/08 14:20:
> This simpler patch supersedes the previous one, it allows the calling
> context to define the abort action.
>
> I really couldn't implement this in talloc.c, as there is no trivial way
> of getting the error message formatted, output, and then aborting in a
> way that did not impact other libtalloc clients. I spent a couple of
> hours discussing it though!
>
>
>
> 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, but implemented
> as a macro so that 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.
>
> Signed-off-by: Amin Azez <azez at ufomechanic.net>
> ---
> source/lib/talloc/talloc.h | 11 +++++++++++
> 1 files changed, 11 insertions(+), 0 deletions(-)
>
More information about the samba-technical
mailing list