Ensure a tls key is only generated once - wrap create & destroy in a mutex.
[Samba/gebeck_regimport.git] / lib / util / talloc_stack.c
blobf572dd6c77c39c72d0201a03a5076e84166250d8
1 /*
2 Unix SMB/CIFS implementation.
3 Implement a stack of talloc contexts
4 Copyright (C) Volker Lendecke 2007
5 Copyright (C) Jeremy Allison 2009 - made thread safe.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Implement a stack of talloc frames.
25 * When a new talloc stackframe is allocated with talloc_stackframe(), then
26 * the TALLOC_CTX returned with talloc_tos() is reset to that new
27 * frame. Whenever that stack frame is TALLOC_FREE()'ed, then the reverse
28 * happens: The previous talloc_tos() is restored.
30 * This API is designed to be robust in the sense that if someone forgets to
31 * TALLOC_FREE() a stackframe, then the next outer one correctly cleans up and
32 * resets the talloc_tos().
34 * This robustness feature means that we can't rely on a linked list with
35 * talloc destructors because in a hierarchy of talloc destructors the parent
36 * destructor is called before its children destructors. The child destructor
37 * called after the parent would set the talloc_tos() to the wrong value.
40 #include "includes.h"
42 struct talloc_stackframe {
43 int talloc_stacksize;
44 int talloc_stack_arraysize;
45 TALLOC_CTX **talloc_stack;
49 * In the single threaded case this is a pointer
50 * to the global talloc_stackframe. In the MT-case
51 * this is the pointer to the thread-specific key
52 * used to look up the per-thread talloc_stackframe
53 * pointer.
56 static void *global_ts;
58 static struct talloc_stackframe *talloc_stackframe_init(void)
60 #if defined(PARANOID_MALLOC_CHECKER)
61 #ifdef malloc
62 #undef malloc
63 #endif
64 #endif
65 struct talloc_stackframe *ts =
66 (struct talloc_stackframe *)malloc(sizeof(struct talloc_stackframe));
67 #if defined(PARANOID_MALLOC_CHECKER)
68 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
69 #endif
71 if (!ts) {
72 smb_panic("talloc_stackframe_init malloc failed");
75 ZERO_STRUCTP(ts);
77 if (SMB_THREAD_CREATE_TLS_ONCE("talloc_stackframe", global_ts)) {
78 smb_panic("talloc_stackframe_init create_tls failed");
81 if (SMB_THREAD_SET_TLS(global_ts, ts)) {
82 smb_panic("talloc_stackframe_init set_tls failed");
84 return ts;
87 static int talloc_pop(TALLOC_CTX *frame)
89 struct talloc_stackframe *ts =
90 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
91 int i;
93 for (i=ts->talloc_stacksize-1; i>0; i--) {
94 if (frame == ts->talloc_stack[i]) {
95 break;
97 talloc_free(ts->talloc_stack[i]);
100 ts->talloc_stacksize = i;
101 return 0;
105 * Create a new talloc stack frame.
107 * When free'd, it frees all stack frames that were created after this one and
108 * not explicitly freed.
111 static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
113 TALLOC_CTX **tmp, *top, *parent;
114 struct talloc_stackframe *ts =
115 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
117 if (ts == NULL) {
118 ts = talloc_stackframe_init();
121 if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
122 tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
123 ts->talloc_stacksize + 1);
124 if (tmp == NULL) {
125 goto fail;
127 ts->talloc_stack = tmp;
128 ts->talloc_stack_arraysize = ts->talloc_stacksize + 1;
131 if (ts->talloc_stacksize == 0) {
132 parent = ts->talloc_stack;
133 } else {
134 parent = ts->talloc_stack[ts->talloc_stacksize-1];
137 if (poolsize) {
138 top = talloc_pool(parent, poolsize);
139 } else {
140 top = talloc_new(parent);
143 if (top == NULL) {
144 goto fail;
147 talloc_set_destructor(top, talloc_pop);
149 ts->talloc_stack[ts->talloc_stacksize++] = top;
150 return top;
152 fail:
153 smb_panic("talloc_stackframe failed");
154 return NULL;
157 TALLOC_CTX *talloc_stackframe(void)
159 return talloc_stackframe_internal(0);
162 TALLOC_CTX *talloc_stackframe_pool(size_t poolsize)
164 return talloc_stackframe_internal(poolsize);
168 * Get us the current top of the talloc stack.
171 TALLOC_CTX *talloc_tos(void)
173 struct talloc_stackframe *ts =
174 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
176 if (ts == NULL) {
177 talloc_stackframe();
178 ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
179 DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
182 return ts->talloc_stack[ts->talloc_stacksize-1];