summaryrefslogtreecommitdiff
path: root/source4
diff options
context:
space:
mode:
Diffstat (limited to 'source4')
-rw-r--r--source4/lib/talloc/talloc.3.xml18
-rw-r--r--source4/lib/talloc/talloc.h2
-rw-r--r--source4/lib/talloc/talloc_guide.txt16
-rw-r--r--source4/lib/talloc/testsuite.c76
4 files changed, 112 insertions, 0 deletions
diff --git a/source4/lib/talloc/talloc.3.xml b/source4/lib/talloc/talloc.3.xml
index ef032f8426..952083a805 100644
--- a/source4/lib/talloc/talloc.3.xml
+++ b/source4/lib/talloc/talloc.3.xml
@@ -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 @@ 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
diff --git a/source4/lib/talloc/talloc.h b/source4/lib/talloc/talloc.h
index fec2e8d8d7..40351693fa 100644
--- a/source4/lib/talloc/talloc.h
+++ b/source4/lib/talloc/talloc.h
@@ -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__)
diff --git a/source4/lib/talloc/talloc_guide.txt b/source4/lib/talloc/talloc_guide.txt
index 5aa8dfa24f..65fb2017a4 100644
--- a/source4/lib/talloc/talloc_guide.txt
+++ b/source4/lib/talloc/talloc_guide.txt
@@ -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);
diff --git a/source4/lib/talloc/testsuite.c b/source4/lib/talloc/testsuite.c
index f3b20fe657..45d73bd1c0 100644
--- a/source4/lib/talloc/testsuite.c
+++ b/source4/lib/talloc/testsuite.c
@@ -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();
}