ldb: version 1.5.2
[Samba.git] / lib / util / talloc_stack.c
blob4971150e0d5ca8ac056239adde15b3b05661ac27
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 /* Variable to ensure TLS value is only initialized once. */
59 static smb_thread_once_t ts_initialized = SMB_THREAD_ONCE_INIT;
61 static void talloc_stackframe_init(void * unused)
63 if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
64 smb_panic("talloc_stackframe_init create_tls failed");
68 static struct talloc_stackframe *talloc_stackframe_create(void)
70 #if defined(PARANOID_MALLOC_CHECKER)
71 #ifdef calloc
72 #undef calloc
73 #endif
74 #endif
75 struct talloc_stackframe *ts = (struct talloc_stackframe *)calloc(
76 1, sizeof(struct talloc_stackframe));
77 #if defined(PARANOID_MALLOC_CHECKER)
78 #define calloc(n, s) __ERROR_DONT_USE_MALLOC_DIRECTLY
79 #endif
81 if (!ts) {
82 smb_panic("talloc_stackframe_init malloc failed");
85 SMB_THREAD_ONCE(&ts_initialized, talloc_stackframe_init, NULL);
87 if (SMB_THREAD_SET_TLS(global_ts, ts)) {
88 smb_panic("talloc_stackframe_init set_tls failed");
90 return ts;
93 static int talloc_pop(TALLOC_CTX *frame)
95 struct talloc_stackframe *ts =
96 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
97 size_t blocks;
98 int i;
100 /* Catch lazy frame-freeing. */
101 if (ts->talloc_stack[ts->talloc_stacksize-1] != frame) {
102 DEBUG(0, ("Freed frame %s, expected %s.\n",
103 talloc_get_name(frame),
104 talloc_get_name(ts->talloc_stack
105 [ts->talloc_stacksize-1])));
106 #ifdef DEVELOPER
107 smb_panic("Frame not freed in order.");
108 #endif
111 for (i=0; i<10; i++) {
114 * We have to free our children first, calling all
115 * destructors. If a destructor hanging deeply off
116 * "frame" uses talloc_tos() itself while freeing the
117 * toplevel frame, we panic because that nested
118 * talloc_tos() in the destructor does not find a
119 * stackframe anymore.
121 * Do it in a loop up to 10 times as the destructors
122 * might use more of talloc_tos().
125 talloc_free_children(frame);
127 blocks = talloc_total_blocks(frame);
128 if (blocks == 1) {
129 break;
133 if (blocks != 1) {
134 DBG_WARNING("Left %zu blocks after %i "
135 "talloc_free_children(frame) calls\n",
136 blocks, i);
139 for (i=ts->talloc_stacksize-1; i>0; i--) {
140 if (frame == ts->talloc_stack[i]) {
141 break;
143 TALLOC_FREE(ts->talloc_stack[i]);
146 ts->talloc_stack[i] = NULL;
147 ts->talloc_stacksize = i;
148 return 0;
152 * Create a new talloc stack frame.
154 * When free'd, it frees all stack frames that were created after this one and
155 * not explicitly freed.
158 static TALLOC_CTX *talloc_stackframe_internal(const char *location,
159 size_t poolsize)
161 TALLOC_CTX **tmp, *top;
162 struct talloc_stackframe *ts =
163 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
165 if (ts == NULL) {
166 ts = talloc_stackframe_create();
169 if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
170 tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
171 ts->talloc_stacksize + 1);
172 if (tmp == NULL) {
173 goto fail;
175 ts->talloc_stack = tmp;
176 ts->talloc_stack_arraysize = ts->talloc_stacksize + 1;
179 if (poolsize) {
180 top = talloc_pool(ts->talloc_stack, poolsize);
181 } else {
182 TALLOC_CTX *parent;
183 /* We chain parentage, so if one is a pool we draw from it. */
184 if (ts->talloc_stacksize == 0) {
185 parent = ts->talloc_stack;
186 } else {
187 parent = ts->talloc_stack[ts->talloc_stacksize-1];
189 top = talloc_new(parent);
192 if (top == NULL) {
193 goto fail;
195 talloc_set_name_const(top, location);
196 talloc_set_destructor(top, talloc_pop);
198 ts->talloc_stack[ts->talloc_stacksize++] = top;
199 return top;
201 fail:
202 smb_panic("talloc_stackframe failed");
203 return NULL;
206 TALLOC_CTX *_talloc_stackframe(const char *location)
208 return talloc_stackframe_internal(location, 0);
211 TALLOC_CTX *_talloc_stackframe_pool(const char *location, size_t poolsize)
213 return talloc_stackframe_internal(location, poolsize);
217 * Get us the current top of the talloc stack.
220 TALLOC_CTX *_talloc_tos(const char *location)
222 struct talloc_stackframe *ts =
223 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
225 if (ts == NULL || ts->talloc_stacksize == 0) {
226 _talloc_stackframe(location);
227 ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
228 DEBUG(0, ("no talloc stackframe at %s, leaking memory\n",
229 location));
230 #ifdef DEVELOPER
231 smb_panic("No talloc stackframe");
232 #endif
235 return ts->talloc_stack[ts->talloc_stacksize-1];
239 * return true if a talloc stackframe exists
240 * this can be used to prevent memory leaks for code that can
241 * optionally use a talloc stackframe (eg. nt_errstr())
244 bool talloc_stackframe_exists(void)
246 struct talloc_stackframe *ts =
247 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
249 if (ts == NULL || ts->talloc_stacksize == 0) {
250 return false;
252 return true;