From 929e1d99209e20a9c2c95c8bdfc8eaa37b2c2291 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 30 Aug 2007 19:48:31 +0000 Subject: 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) --- source3/lib/talloc_stack.c | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 source3/lib/talloc_stack.c (limited to 'source3/lib/talloc_stack.c') 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]; +} -- cgit From 2800362e3f8e0d980d689807f348723a88d697af Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 2 Sep 2007 17:48:01 +0000 Subject: r24902: DEBUG might use talloc_tos() itself... (This used to be commit 1b39374a173bf6b40426000d010d092e94afc4d7) --- source3/lib/talloc_stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/talloc_stack.c') diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c index baebbd4fac..e6e4ed321a 100644 --- a/source3/lib/talloc_stack.c +++ b/source3/lib/talloc_stack.c @@ -100,8 +100,8 @@ TALLOC_CTX *talloc_stackframe(void) TALLOC_CTX *talloc_tos(void) { if (talloc_stacksize == 0) { - DEBUG(0, ("no talloc stackframe around, leaking memory\n")); talloc_stackframe(); + DEBUG(0, ("no talloc stackframe around, leaking memory\n")); } return talloc_stack[talloc_stacksize-1]; -- cgit From 8d261ee580f7d589920eb405a68aec04e6cc8e7a Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 6 Jan 2008 17:25:20 +0100 Subject: talloc_stackframe only needs 1 talloc (This used to be commit c0c2084d40b79e949dab7c68626aa665b9ea1a8e) --- source3/lib/talloc_stack.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'source3/lib/talloc_stack.c') diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c index e6e4ed321a..cc7ce3a518 100644 --- a/source3/lib/talloc_stack.c +++ b/source3/lib/talloc_stack.c @@ -41,16 +41,18 @@ static int talloc_stacksize; static TALLOC_CTX **talloc_stack; -static int talloc_pop(int *ptr) +static int talloc_pop(TALLOC_CTX *frame) { - int tos = *ptr; int i; - for (i=talloc_stacksize-1; i>=tos; i--) { + for (i=talloc_stacksize-1; i>0; i--) { + if (frame == talloc_stack[i]) { + break; + } talloc_free(talloc_stack[i]); } - talloc_stacksize = tos; + talloc_stacksize = i; return 0; } @@ -64,7 +66,6 @@ static int talloc_pop(int *ptr) TALLOC_CTX *talloc_stackframe(void) { TALLOC_CTX **tmp, *top; - int *cleanup; if (!(tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *, talloc_stacksize + 1))) { @@ -77,12 +78,7 @@ TALLOC_CTX *talloc_stackframe(void) goto fail; } - if (!(cleanup = talloc(top, int))) { - goto fail; - } - - *cleanup = talloc_stacksize; - talloc_set_destructor(cleanup, talloc_pop); + talloc_set_destructor(top, talloc_pop); talloc_stack[talloc_stacksize++] = top; -- cgit From 5274d8cda30f712f01a048862d0fdc532878eba5 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 8 Jan 2008 22:12:35 +0100 Subject: Only realloc the talloc stack if necessary (This used to be commit c7cb98d486ef8af1dc7111c2316fd73db9fef9f8) --- source3/lib/talloc_stack.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'source3/lib/talloc_stack.c') diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c index cc7ce3a518..d887b2d415 100644 --- a/source3/lib/talloc_stack.c +++ b/source3/lib/talloc_stack.c @@ -39,6 +39,7 @@ #include "includes.h" static int talloc_stacksize; +static int talloc_stack_arraysize; static TALLOC_CTX **talloc_stack; static int talloc_pop(TALLOC_CTX *frame) @@ -67,21 +68,25 @@ TALLOC_CTX *talloc_stackframe(void) { TALLOC_CTX **tmp, *top; - if (!(tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *, - talloc_stacksize + 1))) { - goto fail; - } + if (talloc_stack_arraysize < talloc_stacksize + 1) { + tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *, + talloc_stacksize + 1); + if (tmp == NULL) { + goto fail; + } + talloc_stack = tmp; + talloc_stack_arraysize = talloc_stacksize + 1; + } - talloc_stack = tmp; + top = talloc_new(talloc_stack); - if (!(top = talloc_new(talloc_stack))) { + if (top == NULL) { goto fail; } talloc_set_destructor(top, talloc_pop); talloc_stack[talloc_stacksize++] = top; - return top; fail: -- cgit From 5df6018114ca54d97a7d67d554a090a56270d45e Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 9 Jan 2008 17:07:58 -0800 Subject: Add the calls to make use of talloc_pools in a talloc_stackframe. Jeremy. (This used to be commit d27e6c0548d21394f6399d3b737d175ffed8420d) --- source3/lib/talloc_stack.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'source3/lib/talloc_stack.c') diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c index d887b2d415..08ef2281ea 100644 --- a/source3/lib/talloc_stack.c +++ b/source3/lib/talloc_stack.c @@ -64,7 +64,7 @@ static int talloc_pop(TALLOC_CTX *frame) * not explicitly freed. */ -TALLOC_CTX *talloc_stackframe(void) +static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize) { TALLOC_CTX **tmp, *top; @@ -78,7 +78,11 @@ TALLOC_CTX *talloc_stackframe(void) talloc_stack_arraysize = talloc_stacksize + 1; } - top = talloc_new(talloc_stack); + if (poolsize) { + top = talloc_pool(talloc_stack, poolsize); + } else { + top = talloc_new(talloc_stack); + } if (top == NULL) { goto fail; @@ -94,6 +98,16 @@ TALLOC_CTX *talloc_stackframe(void) return NULL; } +TALLOC_CTX *talloc_stackframe(void) +{ + return talloc_stackframe_internal(0); +} + +TALLOC_CTX *talloc_stackframe_pool(size_t poolsize) +{ + return talloc_stackframe_internal(poolsize); +} + /* * Get us the current top of the talloc stack. */ -- cgit From 7579e08599dd2139617da5639144700774233c08 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 10 Jan 2008 09:53:51 +0100 Subject: Form a proper hierarchy in talloc_stack This way talloc_stackframe() can benefit from a pool put on the stack further up. No need to remove talloc_stackframe(). (This used to be commit be6fe381168321ae62e079cd977cbef675c532d4) --- source3/lib/talloc_stack.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'source3/lib/talloc_stack.c') diff --git a/source3/lib/talloc_stack.c b/source3/lib/talloc_stack.c index 08ef2281ea..2722fb9676 100644 --- a/source3/lib/talloc_stack.c +++ b/source3/lib/talloc_stack.c @@ -66,7 +66,7 @@ static int talloc_pop(TALLOC_CTX *frame) static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize) { - TALLOC_CTX **tmp, *top; + TALLOC_CTX **tmp, *top, *parent; if (talloc_stack_arraysize < talloc_stacksize + 1) { tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *, @@ -78,10 +78,17 @@ static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize) talloc_stack_arraysize = talloc_stacksize + 1; } + if (talloc_stacksize == 0) { + parent = talloc_stack; + } + else { + parent = talloc_stack[talloc_stacksize-1]; + } + if (poolsize) { - top = talloc_pool(talloc_stack, poolsize); + top = talloc_pool(parent, poolsize); } else { - top = talloc_new(talloc_stack); + top = talloc_new(parent); } if (top == NULL) { -- cgit