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

tridge at samba.org tridge at samba.org
Sun Jan 16 23:21:53 GMT 2005


Author: tridge
Date: 2005-01-16 23:21:52 +0000 (Sun, 16 Jan 2005)
New Revision: 4790

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

Log:
added type checking helper macros in talloc. These take advantage of
the type names that talloc already keeps around for pointers, and
allows the user to type check void* private pointers. It can also be
used to implement polymorphism in C, as I'm sure someone would have
pointed out to me sooner or later :-)

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


Changeset:
Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-01-16 23:10:33 UTC (rev 4789)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.c	2005-01-16 23:21:52 UTC (rev 4790)
@@ -424,7 +424,24 @@
 	return "UNNAMED";
 }
 
+
 /*
+  check if a pointer has the given name. If it does, return the pointer,
+  otherwise return NULL
+*/
+void *talloc_check_name(const void *ptr, const char *name)
+{
+	const char *pname;
+	if (ptr == NULL) return NULL;
+	pname = talloc_get_name(ptr);
+	if (pname == name || strcmp(pname, name) == 0) {
+		return discard_const_p(void, ptr);
+	}
+	return NULL;
+}
+
+
+/*
   this is for compatibility with older versions of talloc
 */
 void *talloc_init(const char *fmt, ...)
@@ -1029,3 +1046,5 @@
 	}
 	return cleanup_context;
 }
+
+

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc.h
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-01-16 23:10:33 UTC (rev 4789)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc.h	2005-01-16 23:21:52 UTC (rev 4790)
@@ -37,7 +37,7 @@
 #endif
 
 /* useful macros for creating type checked pointers */
-#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type ": " __location__)
+#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_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
@@ -45,11 +45,11 @@
 #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
 #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
 
-#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, __location__)
-#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, __location__)
+#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_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, __location__)
+#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__)
 
 #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
@@ -62,7 +62,10 @@
 #define data_blob_talloc(ctx, ptr, size) data_blob_talloc_named(ctx, ptr, size, "DATA_BLOB: "__location__)
 #define data_blob_dup_talloc(ctx, blob) data_blob_talloc_named(ctx, (blob)->data, (blob)->length, "DATA_BLOB: "__location__)
 
+#define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
+#define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
 
+
 #if TALLOC_DEPRECATED
 #define talloc_zero_p(ctx, type) talloc_zero(ctx, type)
 #define talloc_p(ctx, type) talloc(ctx, type)
@@ -76,7 +79,7 @@
 #endif
 
 
-/* The following definitions come from lib/talloc.c  */
+/* The following definitions come from talloc.c  */
 void *_talloc(const void *context, size_t size);
 void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
 void talloc_increase_ref_count(const void *ptr);
@@ -88,6 +91,7 @@
 		   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
 void *talloc_named_const(const void *context, size_t size, const char *name);
 const char *talloc_get_name(const void *ptr);
+void *talloc_check_name(const void *ptr, const char *name);
 void talloc_report_depth(const void *ptr, FILE *f, int depth);
 void *talloc_parent(const void *ptr);
 void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);

Modified: branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-01-16 23:10:33 UTC (rev 4789)
+++ branches/SAMBA_4_0/source/lib/talloc/talloc_guide.txt	2005-01-16 23:21:52 UTC (rev 4790)
@@ -512,3 +512,35 @@
 This is a handy utility function that returns a talloc context
 which will be automatically freed on program exit. This can be used
 to reduce the noise in memory leak reports.
+
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void *talloc_check_name(const void *ptr, const char *name);
+
+This function checks if a pointer has the specified name. If it does
+then the pointer is returned. It it doesn't then NULL is returned.
+
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(type *)talloc_get_type(const void *ptr, type);
+
+This macro allows you to do type checking on talloc pointers. It is
+particularly useful for void* private pointers. It is equivalent to
+this:
+
+   (type *)talloc_check_name(ptr, #type)
+
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+talloc_set_type(const void *ptr, type);
+
+This macro allows you to force the name of a pointer to be a
+particular type. This can be used in conjunction with
+talloc_get_type() to do type checking on void* pointers.
+
+It is equivalent to this:
+   talloc_set_name_const(ptr, #type)
+
+
+
+

Modified: branches/SAMBA_4_0/source/lib/talloc/testsuite.c
===================================================================
--- branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2005-01-16 23:10:33 UTC (rev 4789)
+++ branches/SAMBA_4_0/source/lib/talloc/testsuite.c	2005-01-16 23:21:52 UTC (rev 4790)
@@ -609,7 +609,49 @@
 	return True;
 }
 
+
 /*
+  test type checking
+*/
+static BOOL test_type(void)
+{
+	void *root;
+	struct el1 {
+		int count;
+	};
+	struct el2 {
+		int count;
+	};
+	struct el1 *el1;
+
+	printf("TESTING talloc type checking\n");
+
+	root = talloc_new(NULL);
+
+	el1 = talloc(root, struct el1);
+
+	el1->count = 1;
+
+	if (talloc_get_type(el1, struct el1) != el1) {
+		printf("type check failed on el1\n");
+		return False;
+	}
+	if (talloc_get_type(el1, struct el2) != NULL) {
+		printf("type check failed on el1 with el2\n");
+		return False;
+	}
+	talloc_set_type(el1, struct el2);
+	if (talloc_get_type(el1, struct el2) != el1) {
+		printf("type set failed on el1 with el2\n");
+		return False;
+	}
+
+	talloc_free(root);
+
+	return True;
+}
+
+/*
   test steal
 */
 static BOOL test_steal(void)
@@ -664,13 +706,13 @@
 }
 
 /*
-  test ldb alloc fn
+  test talloc_realloc_fn
 */
-static BOOL test_ldb(void)
+static BOOL test_realloc_fn(void)
 {
 	void *root, *p1;
 
-	printf("TESTING LDB\n");
+	printf("TESTING talloc_realloc_fn\n");
 
 	root = talloc_new(NULL);
 
@@ -774,7 +816,8 @@
 	ret &= test_realloc_child();
 	ret &= test_steal();
 	ret &= test_unref_reparent();
-	ret &= test_ldb();
+	ret &= test_realloc_fn();
+	ret &= test_type();
 	if (ret) {
 		ret &= test_speed();
 	}



More information about the samba-cvs mailing list