Author: abartlet Date: 2004-11-05 12:20:27 +0000 (Fri, 05 Nov 2004) New Revision: 3553
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=3553 Log: Allow talloc_reference to take a NULL pointer for the "ptr" argument. This allows potentially NULL pointers to be referenced, without an if () for every use. (previously, it would segfault). Update doco, and allow talloc_unlink to match. Andrew Bartlett Modified: branches/SAMBA_4_0/source/lib/talloc.c branches/SAMBA_4_0/source/torture/local/talloc.c branches/SAMBA_4_0/talloc_guide.txt Changeset: Modified: branches/SAMBA_4_0/source/lib/talloc.c =================================================================== --- branches/SAMBA_4_0/source/lib/talloc.c 2004-11-05 12:10:28 UTC (rev 3552) +++ branches/SAMBA_4_0/source/lib/talloc.c 2004-11-05 12:20:27 UTC (rev 3553) @@ -222,9 +222,13 @@ */ void *talloc_reference(const void *context, const void *ptr) { - struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); + struct talloc_chunk *tc; struct talloc_reference_handle *handle; + if (ptr == NULL) return NULL; + + tc = talloc_chunk_from_ptr(ptr); handle = talloc_named_const(context, sizeof(*handle), TALLOC_MAGIC_REFERENCE); + if (handle == NULL) return NULL; /* note that we hang the destructor off the handle, not the @@ -273,6 +277,10 @@ struct talloc_chunk *tc_p, *new_p; void *new_parent; + if (ptr == NULL) { + return -1; + } + if (context == NULL) { context = null_context; } Modified: branches/SAMBA_4_0/source/torture/local/talloc.c =================================================================== --- branches/SAMBA_4_0/source/torture/local/talloc.c 2004-11-05 12:10:28 UTC (rev 3552) +++ branches/SAMBA_4_0/source/torture/local/talloc.c 2004-11-05 12:20:27 UTC (rev 3553) @@ -117,6 +117,11 @@ talloc_free(r1); talloc_report_full(NULL, stdout); + printf("Testing NULL\n"); + if (talloc_reference(root, NULL)) { + return False; + } + CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); @@ -478,8 +483,13 @@ talloc_unlink(NULL, p2); talloc_unlink(root, p1); - + /* Test that talloc_unlink is a no-op */ + if (talloc_unlink(root, NULL) != -1) { + printf("failed: talloc_unlink(root, NULL) == -1\n"); + return False; + } + talloc_report(root, stdout); talloc_report(NULL, stdout); Modified: branches/SAMBA_4_0/talloc_guide.txt =================================================================== --- branches/SAMBA_4_0/talloc_guide.txt 2004-11-05 12:10:28 UTC (rev 3552) +++ branches/SAMBA_4_0/talloc_guide.txt 2004-11-05 12:20:27 UTC (rev 3553) @@ -122,6 +122,8 @@ which case it will return NULL (each additional reference consumes around 48 bytes of memory on intel x86 platforms). +If "ptr" is NULL, then the function is a no-op, and simply returns NULL. + After creating a reference you can free it in one of the following ways: @@ -144,7 +146,8 @@ with this pointer, or must be a direct parent of ptr. Note that if the parent has already been removed using talloc_free() -then this function will fail and will return -1. +then this function will fail and will return -1. Likewise, if "ptr" +is NULL, then the function will make no modifications and return -1. Usually you can just use talloc_free() instead of talloc_unlink(), but sometimes it is useful to have the additional control on which parent
