summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vlendec@samba.org>2007-08-30 19:48:31 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 12:30:24 -0500
commit929e1d99209e20a9c2c95c8bdfc8eaa37b2c2291 (patch)
tree169c06d247826eac8c3d3054ce8de78fce7d454d /source3/lib
parenta646210b2b8ead5690b3c6baf058c2de6e0c1a26 (diff)
downloadsamba-929e1d99209e20a9c2c95c8bdfc8eaa37b2c2291.tar.gz
samba-929e1d99209e20a9c2c95c8bdfc8eaa37b2c2291.tar.bz2
samba-929e1d99209e20a9c2c95c8bdfc8eaa37b2c2291.zip
r24809: Consolidate the use of temporary talloc contexts.
This adds the two functions talloc_stackframe() and talloc_tos(). * When a new talloc stackframe is allocated with talloc_stackframe(), then * the TALLOC_CTX returned with talloc_tos() is reset to that new * frame. Whenever that stack frame is TALLOC_FREE()'ed, then the reverse * happens: The previous talloc_tos() is restored. * * This API is designed to be robust in the sense that if someone forgets to * TALLOC_FREE() a stackframe, then the next outer one correctly cleans up and * resets the talloc_tos(). The original motivation for this patch was to get rid of the sid_string_static & friends buffers. Explicitly passing talloc context everywhere clutters code too much for my taste, so an implicit talloc_tos() is introduced here. Many of these static buffers are replaced by a single static pointer. The intended use would thus be that low-level functions can rather freely push stuff to talloc_tos, the upper layers clean up by freeing the stackframe. The more of these stackframes are used and correctly freed the more exact the memory cleanup happens. This patch removes the main_loop_talloc_ctx, tmp_talloc_ctx and lp_talloc_ctx (did I forget any?) So, never do a tmp_ctx = talloc_init("foo"); anymore, instead, use tmp_ctx = talloc_stackframe() :-) Volker (This used to be commit 6585ea2cb7f417e14540495b9c7380fe9c8c717b)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/talloc_stack.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c
new file mode 100644
index 0000000000..baebbd4fac
--- /dev/null
+++ b/source3/lib/talloc_stack.c
@@ -0,0 +1,108 @@
+/*
+ Unix SMB/CIFS implementation.
+ Implement a stack of talloc contexts
+ Copyright (C) Volker Lendecke 2007
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/*
+ * Implement a stack of talloc frames.
+ *
+ * When a new talloc stackframe is allocated with talloc_stackframe(), then
+ * the TALLOC_CTX returned with talloc_tos() is reset to that new
+ * frame. Whenever that stack frame is TALLOC_FREE()'ed, then the reverse
+ * happens: The previous talloc_tos() is restored.
+ *
+ * This API is designed to be robust in the sense that if someone forgets to
+ * TALLOC_FREE() a stackframe, then the next outer one correctly cleans up and
+ * resets the talloc_tos().
+ *
+ * This robustness feature means that we can't rely on a linked list with
+ * talloc destructors because in a hierarchy of talloc destructors the parent
+ * destructor is called before its children destructors. The child destructor
+ * called after the parent would set the talloc_tos() to the wrong value.
+ */
+
+#include "includes.h"
+
+static int talloc_stacksize;
+static TALLOC_CTX **talloc_stack;
+
+static int talloc_pop(int *ptr)
+{
+ int tos = *ptr;
+ int i;
+
+ for (i=talloc_stacksize-1; i>=tos; i--) {
+ talloc_free(talloc_stack[i]);
+ }
+
+ talloc_stacksize = tos;
+ return 0;
+}
+
+/*
+ * Create a new talloc stack frame.
+ *
+ * When free'd, it frees all stack frames that were created after this one and
+ * not explicitly freed.
+ */
+
+TALLOC_CTX *talloc_stackframe(void)
+{
+ TALLOC_CTX **tmp, *top;
+ int *cleanup;
+
+ if (!(tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *,
+ talloc_stacksize + 1))) {
+ goto fail;
+ }
+
+ talloc_stack = tmp;
+
+ if (!(top = talloc_new(talloc_stack))) {
+ goto fail;
+ }
+
+ if (!(cleanup = talloc(top, int))) {
+ goto fail;
+ }
+
+ *cleanup = talloc_stacksize;
+ talloc_set_destructor(cleanup, talloc_pop);
+
+ talloc_stack[talloc_stacksize++] = top;
+
+ return top;
+
+ fail:
+ smb_panic("talloc_stackframe failed");
+ return NULL;
+}
+
+/*
+ * Get us the current top of the talloc stack.
+ */
+
+TALLOC_CTX *talloc_tos(void)
+{
+ if (talloc_stacksize == 0) {
+ DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
+ talloc_stackframe();
+ }
+
+ return talloc_stack[talloc_stacksize-1];
+}