talloc: added talloc_stackframe_exists()
authorAndrew Tridgell <tridge@samba.org>
Fri, 17 Jun 2011 04:22:28 +0000 (14:22 +1000)
committerAndrew Tridgell <tridge@samba.org>
Fri, 17 Jun 2011 05:25:42 +0000 (15:25 +1000)
This can be used to tell if a talloc stackframe is currently
available. Callers can use this to decide if they will use
talloc_tos() or instead use an alternative strategy. This gives us a
way to safely have calls to talloc_tos() in common code that may end
up in external libraries, as long as all talloc_tos() calls in these
pieces of common code check first that a stackframe is available.

lib/util/talloc_stack.c
lib/util/talloc_stack.h

index 8e559cc20f414415437f43b60090880dc0f46056..16e9d745d347fe340b7fd40b3e027dc358934e5a 100644 (file)
@@ -188,3 +188,20 @@ TALLOC_CTX *talloc_tos(void)
 
        return ts->talloc_stack[ts->talloc_stacksize-1];
 }
+
+/*
+ * return true if a talloc stackframe exists
+ * this can be used to prevent memory leaks for code that can
+ * optionally use a talloc stackframe (eg. nt_errstr())
+ */
+
+bool talloc_stackframe_exists(void)
+{
+       struct talloc_stackframe *ts =
+               (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+
+       if (ts == NULL || ts->talloc_stacksize == 0) {
+               return false;
+       }
+       return true;
+}
index 0e8fab37598e405250f58e7e0883a7cce8937f89..ec0c1c6f37e951d77c9498f34a21b6522e455c24 100644 (file)
@@ -53,4 +53,12 @@ TALLOC_CTX *talloc_stackframe_pool(size_t poolsize);
 
 TALLOC_CTX *talloc_tos(void);
 
+/*
+ * return true if a talloc stackframe exists
+ * this can be used to prevent memory leaks for code that can
+ * optionally use a talloc stackframe (eg. nt_errstr())
+ */
+
+bool talloc_stackframe_exists(void);
+
 #endif