svn commit: samba r17886 - in branches/SAMBA_4_0/source/lib/talloc: .

metze at samba.org metze at samba.org
Mon Aug 28 16:55:52 GMT 2006


Author: metze
Date: 2006-08-28 16:55:51 +0000 (Mon, 28 Aug 2006)
New Revision: 17886

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

Log:
add talloc_ptrtype() and talloc_array_ptrtype(),
see the manpage what they do:-)

metze
Modified:
   branches/SAMBA_4_0/source/lib/talloc/talloc.3.xml
   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


Changeset:
Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.3.xml
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.3.xml	2006-08-28 15:36:12 UTC (rev 17885)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.3.xml	2006-08-28 16:55:51 UTC (rev 17886)
@@ -97,6 +97,15 @@
 	  type checking.
         </para>
     </refsect2>
+    <refsect2><title>(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);</title>
+        <para>
+	  The talloc_ptrtype() macro should be used when you have a pointer and
+	  want to allocate memory to point at with this pointer. When compiling
+	  with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
+	  and talloc_get_name() will return the current location in the source file.
+	  and not the type.
+        </para>
+    </refsect2>
     <refsect2><title>int talloc_free(void *ptr);</title>
         <para>
 	  The talloc_free() function frees a piece of talloc memory, and
@@ -551,6 +560,15 @@
 	  size instead of a type.
         </para>
     </refsect2>
+    <refsect2><title>(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);</title>
+        <para>
+	  The talloc_ptrtype() macro should be used when you have a pointer to an array
+	  and want to allocate memory of an array to point at with this pointer. When compiling
+	  with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
+	  and talloc_get_name() will return the current location in the source file.
+	  and not the type.
+        </para>
+    </refsect2>
     <refsect2><title>void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)</title>
         <para>
 	  This is a non-macro version of talloc_realloc(), which is useful

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.h
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.h	2006-08-28 15:36:12 UTC (rev 17885)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.h	2006-08-28 16:55:51 UTC (rev 17886)
@@ -74,6 +74,7 @@
 /* useful macros for creating type checked pointers */
 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
 #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
+#define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
 
 #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
 
@@ -83,6 +84,7 @@
 #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
 #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
 #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
+#define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
 
 #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
 #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2006-08-28 15:36:12 UTC (rev 17885)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2006-08-28 16:55:51 UTC (rev 17886)
@@ -82,7 +82,15 @@
 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.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);
 
+The talloc_ptrtype() macro should be used when you have a pointer and
+want to allocate memory to point at with this pointer. When compiling
+with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
+and talloc_get_name() will return the current location in the source file.
+and not the type.
+
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 int talloc_free(void *ptr);
 
@@ -515,7 +523,15 @@
 known. It operates in the same way as talloc_array(), but takes a size
 instead of a type.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
 
+The talloc_ptrtype() macro should be used when you have a pointer to an array
+and want to allocate memory of an array to point at with this pointer. When compiling
+with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
+and talloc_get_name() will return the current location in the source file.
+and not the type.
+
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size);
 

Modified: branches/SAMBA_4_0/source/lib/talloc/testsuite.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2006-08-28 15:36:12 UTC (rev 17885)
+++ branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2006-08-28 16:55:51 UTC (rev 17886)
@@ -938,6 +938,81 @@
 	return True;
 }
 
+static BOOL test_talloc_ptrtype(void)
+{
+	BOOL ret = True;
+	char *top = talloc_new(NULL);
+	struct struct1 {
+		int foo;
+		int bar;
+	} *s1, *s2, **s3, ***s4;
+	const char *location1;
+	const char *location2;
+	const char *location3;
+	const char *location4;
+
+	printf("TESTING TALLOC PTRTYPE\n");
+	s1 = talloc_ptrtype(top, s1);location1 = __location__;
+
+	if (talloc_get_size(s1) != sizeof(struct struct1)) {
+		printf("%s: talloc_ptrtype() allocated the wrong size %u (should be %u)\n",
+			__location__, talloc_get_size(s1), sizeof(struct struct1));
+		ret = False;
+	}
+
+	if (strcmp(location1, talloc_get_name(s1)) != 0) {
+		printf("%s: talloc_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+			__location__, talloc_get_name(s1), location1);
+		ret = False;
+	}
+
+	s2 = talloc_array_ptrtype(top, s2, 10);location2 = __location__;
+
+	if (talloc_get_size(s2) != (sizeof(struct struct1) * 10)) {
+		printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+			__location__, talloc_get_size(s2), (sizeof(struct struct1)*10));
+		ret = False;
+	}
+
+	if (strcmp(location2, talloc_get_name(s2)) != 0) {
+		printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+			__location__, talloc_get_name(s2), location2);
+		ret = False;
+	}
+
+	s3 = talloc_array_ptrtype(top, s3, 10);location3 = __location__;
+
+	if (talloc_get_size(s3) != (sizeof(struct struct1 *) * 10)) {
+		printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+			__location__, talloc_get_size(s3), (sizeof(struct struct1 *)*10));
+		ret = False;
+	}
+
+	if (strcmp(location3, talloc_get_name(s3)) != 0) {
+		printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+			__location__, talloc_get_name(s3), location3);
+		ret = False;
+	}
+
+	s4 = talloc_array_ptrtype(top, s4, 10);location4 = __location__;
+
+	if (talloc_get_size(s4) != (sizeof(struct struct1 **) * 10)) {
+		printf("%s: talloc_array_ptrtype() allocated the wrong size %u (should be %u)\n",
+			__location__, talloc_get_size(s4), (sizeof(struct struct1 **)*10));
+		ret = False;
+	}
+
+	if (strcmp(location4, talloc_get_name(s4)) != 0) {
+		printf("%s: talloc_array_ptrtype() sets the wrong name '%s' (should be '%s')\n",
+			__location__, talloc_get_name(s4), location4);
+		ret = False;
+	}
+
+	talloc_free(top);
+
+	return ret;
+}
+
 BOOL torture_local_talloc(struct torture_context *torture) 
 {
 	BOOL ret = True;
@@ -959,6 +1034,7 @@
 	ret &= test_lifeless();
 	ret &= test_loop();
 	ret &= test_free_parent_deny_child();
+	ret &= test_talloc_ptrtype();
 	if (ret) {
 		ret &= test_speed();
 	}



More information about the samba-cvs mailing list