r17886: add talloc_ptrtype() and talloc_array_ptrtype(),
authorStefan Metzmacher <metze@samba.org>
Mon, 28 Aug 2006 16:55:51 +0000 (16:55 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 19:16:51 +0000 (14:16 -0500)
see the manpage what they do:-)

metze

source/lib/talloc/talloc.3.xml
source/lib/talloc/talloc.h
source/lib/talloc/talloc_guide.txt
source/lib/talloc/testsuite.c

index ef032f8426d7cb6425676eec408bebd6e541f502..952083a8056826d8336ecfb1c85e205fbbee6105 100644 (file)
          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 @@ if (ptr) memcpy(ptr, p, strlen(p)+1);</programlisting>
          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
index fec2e8d8d7c5738773e681883e20faf30ed34df7..40351693fad34a24e84cc854ffcd0450af4b341d 100644 (file)
@@ -74,6 +74,7 @@ typedef void TALLOC_CTX;
 /* 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 @@ typedef void TALLOC_CTX;
 #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__)
index 5aa8dfa24f5f650f470ab918f2e5513fe63d7240..65fb2017a4e25b612842a0c4f58ffd7ea2de7892 100644 (file)
@@ -82,6 +82,14 @@ 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.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(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,6 +523,14 @@ The talloc_array_size() function is useful when the type is not
 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);
index f3b20fe657def85bc4fef95840fbecbe45b938eb..45d73bd1c0a4b945c0273f798f1bc360c046a891 100644 (file)
@@ -938,6 +938,81 @@ static BOOL test_free_parent_deny_child(void)
        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 @@ BOOL torture_local_talloc(struct torture_context *torture)
        ret &= test_lifeless();
        ret &= test_loop();
        ret &= test_free_parent_deny_child();
+       ret &= test_talloc_ptrtype();
        if (ret) {
                ret &= test_speed();
        }