tevent: add tevent_common_check_double_free() helper function
authorStefan Metzmacher <metze@samba.org>
Tue, 17 Apr 2018 14:33:47 +0000 (16:33 +0200)
committerStefan Metzmacher <metze@samba.org>
Thu, 17 May 2018 07:51:47 +0000 (09:51 +0200)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
lib/tevent/tevent.c
lib/tevent/tevent_internal.h

index 501d548a9f092b92f80d3789d72ccb5e5589d644..08a7896016a01487de27f2b2f9a42d7e7740ab99 100644 (file)
@@ -429,6 +429,24 @@ static int tevent_common_context_constructor(struct tevent_context *ev)
        return 0;
 }
 
+_PRIVATE_
+void tevent_common_check_double_free(TALLOC_CTX *ptr, const char *reason)
+{
+       void *parent_ptr = talloc_parent(ptr);
+       size_t parent_blocks = talloc_total_blocks(parent_ptr);
+
+       if (parent_ptr != NULL && parent_blocks == 0) {
+               /*
+                * This is an implicit talloc free,
+                * as we still have a parent but it's
+                * already being destroyed.
+                */
+               return;
+       }
+
+       tevent_abort(NULL, reason);
+}
+
 /*
   create a event_context structure for a specific implemementation.
   This must be the first events call, and all subsequent calls pass
index cbe2e69548dc1718e25b76571a1ab701a08d6dd2..4fd49f58ffb068d30c121354428dec859d505485 100644 (file)
@@ -248,6 +248,8 @@ void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
 
 void tevent_abort(struct tevent_context *ev, const char *reason);
 
+void tevent_common_check_double_free(TALLOC_CTX *ptr, const char *reason);
+
 struct tevent_context {
        /* the specific events implementation */
        const struct tevent_ops *ops;