svn commit: samba r4550 - in branches/SAMBA_4_0/source: lib/talloc librpc/ndr

tridge at samba.org tridge at samba.org
Thu Jan 6 03:20:58 GMT 2005


Author: tridge
Date: 2005-01-06 03:20:56 +0000 (Thu, 06 Jan 2005)
New Revision: 4550

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=4550

Log:
talloc() is now typesafe. It is exactly equivalent to the old talloc_p() macro. Use
talloc_size() if you want the old behaviour.

I have kept talloc_p() as an alias for now. Once we change all calls
to be plain talloc() then we can remove it.

Modified:
   branches/SAMBA_4_0/source/lib/talloc/talloc.c
   branches/SAMBA_4_0/source/lib/talloc/talloc.h
   branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt
   branches/SAMBA_4_0/source/lib/talloc/testsuite.c
   branches/SAMBA_4_0/source/librpc/ndr/libndr.h


Changeset:
Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-01-06 03:06:58 UTC (rev 4549)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-01-06 03:20:56 UTC (rev 4550)
@@ -857,7 +857,7 @@
 
 	for (len=0; p[len] && len<n; len++) ;
 
-	ret = talloc(t, len + 1);
+	ret = _talloc(t, len + 1);
 	if (!ret) { return NULL; }
 	memcpy(ret, p, len);
 	ret[len] = 0;
@@ -883,7 +883,7 @@
 
 	len = vsnprintf(NULL, 0, fmt, ap2);
 
-	ret = talloc(t, len+1);
+	ret = _talloc(t, len+1);
 	if (ret) {
 		VA_COPY(ap2, ap);
 		vsnprintf(ret, len+1, fmt, ap2);

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.h
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-01-06 03:06:58 UTC (rev 4549)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-01-06 03:20:56 UTC (rev 4550)
@@ -33,11 +33,11 @@
 #define __location__ __FILE__ ":" __LINESTR__
 
 /* useful macros for creating type checked pointers */
-#define talloc(ctx, size) talloc_named_const(ctx, size, __location__)
+#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
+#define talloc_p(ctx, type) talloc(ctx, type)
 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
 #define talloc_zero(ctx, size) _talloc_zero(ctx, size, __location__)
 #define talloc_realloc(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
-#define talloc_p(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
 #define talloc_zero_p(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
 #define talloc_zero_array_p(ctx, type, count) (type *)talloc_zero_array(ctx, sizeof(type), count, __location__)

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-01-06 03:06:58 UTC (rev 4549)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-01-06 03:20:56 UTC (rev 4550)
@@ -58,11 +58,11 @@
 
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-void *talloc(const void *context, size_t size);
+(type *)talloc(const void *context, type);
 
-The talloc() function is the core of the talloc library. It takes a
-memory context, and returns a pointer to a new area of memory of the
-given size.
+The talloc() macro is the core of the talloc library. It takes a
+memory context and a type, and returns a pointer to a new area of
+memory of the given type.
 
 The returned pointer is itself a talloc context, so you can use it as
 the context argument to more calls to talloc if you wish.
@@ -74,20 +74,20 @@
 The context argument to talloc() can be NULL, in which case a new top
 level context is created. 
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void *talloc_size(const void *context, size_t size);
 
+The function talloc_size() should be used when you don't have a
+convenient type to pass to talloc(). Unlike talloc(), it is not type
+safe (as it returns a void *), so you are on your own for type checking.
+
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 void *talloc_p(const void *context, type);
 
-The talloc_p() macro is the equivalent of 
+talloc_p() is a alias for talloc(). It only exists as a backwards
+compatibity macro for code from the bad old days when talloc() was not
+type safe.
 
-  (type *)talloc(ctx, sizeof(type))
-
-You should use it in preference to talloc() whenever possible, as it
-provides additional type safety. It also automatically calls the
-talloc_set_name_const() function with the name being a string holding
-the name of the type.
-
-
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 int talloc_free(void *ptr);
 
@@ -231,7 +231,7 @@
 The talloc_named() function creates a named talloc pointer. It is
 equivalent to:
 
-   ptr = talloc(context, size);
+   ptr = talloc_size(context, size);
    talloc_set_name(ptr, fmt, ....);
 
 
@@ -240,7 +240,7 @@
 
 This is equivalent to:
 
-   ptr = talloc(context, size);
+   ptr = talloc_size(context, size);
    talloc_set_name_const(ptr, name);
 
 
@@ -275,7 +275,7 @@
 The talloc_realloc() function changes the size of a talloc
 pointer. It has the following equivalences:
 
-  talloc_realloc(context, NULL, size) ==> talloc(context, size);
+  talloc_realloc(context, NULL, size) ==> talloc_size(context, size);
   talloc_realloc(context, ptr, 0)     ==> talloc_free(ptr);
 
 The "context" argument is only used if "ptr" is not NULL, otherwise it
@@ -407,7 +407,7 @@
 
 The talloc_zero() function is equivalent to:
 
-  ptr = talloc(ctx, size);
+  ptr = talloc_size(ctx, size);
   if (ptr) memset(ptr, 0, size);
 
 
@@ -416,7 +416,7 @@
 
 The talloc_memdup() function is equivalent to:
 
-  ptr = talloc(ctx, size);
+  ptr = talloc_size(ctx, size);
   if (ptr) memcpy(ptr, p, size);
 
 
@@ -425,7 +425,7 @@
 
 The talloc_strdup() function is equivalent to:
 
-  ptr = talloc(ctx, strlen(p)+1);
+  ptr = talloc_size(ctx, strlen(p)+1);
   if (ptr) memcpy(ptr, p, strlen(p)+1);
 
 This functions sets the name of the new pointer to the passed
@@ -473,7 +473,7 @@
 
 The talloc_array_p() macro is equivalent to:
 
-  (type *)talloc(ctx, sizeof(type) * count);
+  (type *)talloc_size(ctx, sizeof(type) * count);
 
 except that it provides integer overflow protection for the multiply,
 returning NULL if the multiply overflows.

Modified: branches/SAMBA_4_0/source/lib/talloc/testsuite.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2005-01-06 03:06:58 UTC (rev 4549)
+++ branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2005-01-06 03:20:56 UTC (rev 4550)
@@ -523,11 +523,11 @@
 	p1 = talloc_realloc(NULL, p1, 20);
 	CHECK_SIZE(p1, 20);
 
-	talloc(p1, 0);
+	talloc_new(p1);
 
 	p2 = talloc_realloc(p1, NULL, 30);
 
-	talloc(p1, 0);
+	talloc_new(p1);
 
 	p2 = talloc_realloc(p1, p2, 40);
 

Modified: branches/SAMBA_4_0/source/librpc/ndr/libndr.h
===================================================================
--- branches/SAMBA_4_0/source/librpc/ndr/libndr.h	2005-01-06 03:06:58 UTC (rev 4549)
+++ branches/SAMBA_4_0/source/librpc/ndr/libndr.h	2005-01-06 03:20:56 UTC (rev 4550)
@@ -229,7 +229,7 @@
 
 
 #define NDR_ALLOC_SIZE(ndr, s, size) do { \
-	                       (s) = talloc(ndr, size); \
+	                       (s) = talloc_size(ndr, size); \
                                if ((size) && !(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, \
 							       "Alloc %u failed\n", \
 							       size); \
@@ -247,7 +247,7 @@
 
 
 #define NDR_PUSH_ALLOC_SIZE(ndr, s, size) do { \
-       (s) = talloc(ndr, size); \
+       (s) = talloc_size(ndr, size); \
        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %u failed\n", size); \
 } while (0)
 



More information about the samba-cvs mailing list