]> git.ozlabs.org Git - ccan/blobdiff - ccan/talloc/talloc.h
talloc: define TALLOC_CTX
[ccan] / ccan / talloc / talloc.h
index e38d05b8095ca9d55abdbd8e9ac833e31ca08c0c..58b406e54b8002b47fce9305725b07491868174e 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
+#include <ccan/compiler/compiler.h>
 #include "config.h"
 
 /*
 #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
 #endif
 
-#if HAVE_ATTRIBUTE_PRINTF
-/** Use gcc attribute to check printf fns.  a1 is the 1-based index of
- * the parameter containing the format, and a2 the index of the first
- * argument. Note that some gcc 2.x versions don't handle this
- * properly **/
-#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
-#else
-#define PRINTF_ATTRIBUTE(a1, a2)
-#endif
-
 /* try to make talloc_set_destructor() and talloc_steal() type safe,
    if we have a recent gcc */
 #if HAVE_TYPEOF
  */
 #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
 
+/**
+ * TALLOC_CTX - indicate that a pointer is used as a talloc parent.
+ *
+ * As talloc is a hierarchial memory allocator, every talloc chunk is a
+ * potential parent to other talloc chunks. So defining a separate type for a
+ * talloc chunk is not strictly necessary. TALLOC_CTX is defined nevertheless,
+ * as it provides an indicator for function arguments.
+ *
+ * Example:
+ *     struct foo {
+ *             int val;
+ *     };
+ *
+ *     static struct foo *foo_new(TALLOC_CTX *mem_ctx)
+ *     {
+ *             struct foo *foo = talloc(mem_ctx, struct foo);
+ *             if (foo)
+ *                     foo->val = 0;
+ *     return foo;
+ *     }
+ */
+typedef void TALLOC_CTX;
+
+/**
+ * talloc_set - allocate dynamic memory for a type, into a pointer
+ * @ptr: pointer to the pointer to assign.
+ * @ctx: context to be parent of this allocation, or NULL.
+ *
+ * talloc_set() does a talloc, but also adds a destructor which will make the
+ * pointer invalid when it is freed.  This can find many use-after-free bugs.
+ *
+ * Note that the destructor is chained off a zero-length allocation, and so
+ * is not affected by talloc_set_destructor().
+ *
+ * Example:
+ *     unsigned int *b, *a;
+ *     a = talloc(NULL, unsigned int);
+ *     talloc_set(&b, a);
+ *     talloc_free(a);
+ *     *b = 1; // This will crash!
+ *
+ * See Also:
+ *     talloc.
+ */
+#define talloc_set(pptr, ctx) \
+       _talloc_set((pptr), (ctx), sizeof(&**(pptr)), __location__)
+
 /**
  * talloc_free - free talloc'ed memory and its children
  * @ptr: the talloced pointer to free
 int talloc_free(const void *ptr);
 
 /**
- * talloc_set_destructor: set a destructor for when this pointer is freed
+ * talloc_set_destructor - set a destructor for when this pointer is freed
  * @ptr: the talloc pointer to set the destructor on
  * @destructor: the function to be called
  *
@@ -155,7 +193,7 @@ int talloc_free(const void *ptr);
  *     return 0;
  * }
  *
- * int *open_file(const char *filename)
+ * static int *open_file(const char *filename)
  * {
  *     int *fd = talloc(NULL, int);
  *     *fd = open(filename, O_RDONLY);
@@ -172,7 +210,7 @@ int talloc_free(const void *ptr);
  *     talloc, talloc_free
  */
 #define talloc_set_destructor(ptr, function)                                 \
-       _talloc_set_destructor((ptr), typesafe_cb(int, (function), (ptr)))
+       _talloc_set_destructor((ptr), typesafe_cb_def(int, (function), (ptr)))
 
 /**
  * talloc_zero - allocate zeroed dynamic memory for a type
@@ -231,6 +269,7 @@ int talloc_free(const void *ptr);
  *
  * Example:
  *     void *mem = talloc_size(NULL, 100);
+ *     memset(mem, 0xFF, 100);
  *
  * See Also:
  *     talloc, talloc_array, talloc_zero_size
@@ -327,13 +366,13 @@ void talloc_report_full(const void *ptr, FILE *f);
  *     a = talloc(NULL, unsigned int);
  *     b = talloc(NULL, unsigned int);
  *     c = talloc(a, unsigned int);
- *     // b also serves as a parent of c.
- *     talloc_reference(b, c);
+ *     // b also serves as a parent of c (don't care about errors)
+ *     (void)talloc_reference(b, c);
  */
 #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
 
 /**
- * talloc_unlink: remove a specific parent from a talloc pointer.
+ * talloc_unlink - remove a specific parent from a talloc pointer.
  * @context: the parent to remove
  * @ptr: the talloc pointer
  *
@@ -348,13 +387,14 @@ void talloc_report_full(const void *ptr, FILE *f);
  * Usually you can just use talloc_free() instead of talloc_unlink(), but
  * sometimes it is useful to have the additional control on which parent is
  * removed.
+ *
  * Example:
  *     unsigned int *a, *b, *c;
  *     a = talloc(NULL, unsigned int);
  *     b = talloc(NULL, unsigned int);
  *     c = talloc(a, unsigned int);
  *     // b also serves as a parent of c.
- *     talloc_reference(b, c);
+ *     (void)talloc_reference(b, c);
  *     talloc_unlink(b, c);
  */
 int talloc_unlink(const void *context, void *ptr);
@@ -555,7 +595,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3)
  *
  *    talloc_set_name_const(ptr, ptr)
  */
-char *talloc_append_string(char *orig, const char *append);
+char *WARN_UNUSED_RESULT talloc_append_string(char *orig, const char *append);
 
 /**
  * talloc_asprintf_append - sprintf onto the end of a talloc buffer.
@@ -569,7 +609,8 @@ char *talloc_append_string(char *orig, const char *append);
  * equivalent to:
  *   talloc_set_name_const(ptr, ptr)
  */
-char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
+char *WARN_UNUSED_RESULT talloc_asprintf_append(char *s, const char *fmt, ...)
+       PRINTF_ATTRIBUTE(2,3);
 
 /**
  * talloc_vasprintf - vsprintf into a talloc buffer.
@@ -596,7 +637,8 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIB
  * The talloc_vasprintf_append() function is equivalent to
  * talloc_asprintf_append(), except it takes a va_list.
  */
-char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
+char *WARN_UNUSED_RESULT talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
+       PRINTF_ATTRIBUTE(2,0);
 
 /**
  * talloc_set_type - force the name of a pointer to a particular type
@@ -940,11 +982,12 @@ void *talloc_add_external(const void *ctx,
 
 /* The following definitions come from talloc.c  */
 void *_talloc(const void *context, size_t size);
+void _talloc_set(void *ptr, const void *ctx, size_t size, const char *name);
 void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
 size_t talloc_reference_count(const void *ptr);
 void *_talloc_reference(const void *context, const void *ptr);
 
-void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
+void *WARN_UNUSED_RESULT _talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
 void *talloc_parent(const void *ptr);
 const char *talloc_parent_name(const void *ptr);
 void *_talloc_steal(const void *new_ctx, const void *ptr);
@@ -953,7 +996,7 @@ void *_talloc_zero(const void *ctx, size_t size, const char *name);
 void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
 void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
 void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
-void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
+void *WARN_UNUSED_RESULT _talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
 void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
 void talloc_show_parents(const void *context, FILE *file);
 int talloc_is_parent(const void *context, const void *ptr);