talloc: talloc_set_flag/talloc_unset_flag and TALLOC_FLAG_WARN_MAY_REFERENCE
[rusty/samba.git] / lib / talloc / talloc.c
index 41684dfee18c360c5aaf3b7040b1ad2b306992b2..5756a5e51f558b3b74958a526b6c64f3e67e2dbc 100644 (file)
 */
 static void *null_context;
 static void *autofree_context;
+static unsigned int flags;
 
 /* used to enable fill of memory on free, which can be useful for
  * catching use after free errors when valgrind is too slow
@@ -285,6 +286,24 @@ _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message))
        talloc_log_fn = log_fn;
 }
 
+int talloc_set_flag(unsigned int flag)
+{
+       if (flag != TALLOC_FLAG_WARN_MAY_REFERENCE) {
+               return -1;
+       }
+       flags |= flag;
+       return 0;
+}
+
+int talloc_unset_flag(unsigned int flag)
+{
+       if (flag != TALLOC_FLAG_WARN_MAY_REFERENCE) {
+               return -1;
+       }
+       flags |= ~flag;
+       return 0;
+}
+
 static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
 static void talloc_log(const char *fmt, ...)
 {
@@ -711,6 +730,10 @@ _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const
        if (unlikely(ptr == NULL)) return NULL;
 
        tc = talloc_chunk_from_ptr(ptr);
+       if ((flags & TALLOC_FLAG_WARN_MAY_REFERENCE) && !(tc->flags & TALLOC_FLAG_MAY_REF)) {
+               talloc_log("talloc_reference on non-may_reference pointer at %s)", location);
+       }
+
        handle = (struct talloc_reference_handle *)_talloc_named_const(context,
                                                   sizeof(struct talloc_reference_handle),
                                                   TALLOC_MAGIC_REFERENCE);