f34d495294a6fce9e0483968a5d6177070cd3aee
[sfrench/samba-autobuild/.git] / lib / util / talloc_stack.c
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.
6
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.
11
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.
16
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.
20 */
21
22 /*
23  * Implement a stack of talloc frames.
24  *
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.
29  *
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().
33  *
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.
38  */
39
40 #include "includes.h"
41
42 struct talloc_stackframe {
43         int talloc_stacksize;
44         int talloc_stack_arraysize;
45         TALLOC_CTX **talloc_stack;
46 };
47
48 /*
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.
54  */
55
56 static void *global_ts;
57
58 /* Variable to ensure TLS value is only initialized once. */
59 static smb_thread_once_t ts_initialized = SMB_THREAD_ONCE_INIT;
60
61 static void talloc_stackframe_init(void * unused)
62 {
63         if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
64                 smb_panic("talloc_stackframe_init create_tls failed");
65         }
66 }
67
68 static struct talloc_stackframe *talloc_stackframe_create(void)
69 {
70 #if defined(PARANOID_MALLOC_CHECKER)
71 #ifdef malloc
72 #undef malloc
73 #endif
74 #endif
75         struct talloc_stackframe *ts =
76                 (struct talloc_stackframe *)malloc(sizeof(struct talloc_stackframe));
77 #if defined(PARANOID_MALLOC_CHECKER)
78 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
79 #endif
80
81         if (!ts) {
82                 smb_panic("talloc_stackframe_init malloc failed");
83         }
84
85         ZERO_STRUCTP(ts);
86
87         SMB_THREAD_ONCE(&ts_initialized, talloc_stackframe_init, NULL);
88
89         if (SMB_THREAD_SET_TLS(global_ts, ts)) {
90                 smb_panic("talloc_stackframe_init set_tls failed");
91         }
92         return ts;
93 }
94
95 static int talloc_pop(TALLOC_CTX *frame)
96 {
97         struct talloc_stackframe *ts =
98                 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
99         int i;
100
101         for (i=ts->talloc_stacksize-1; i>0; i--) {
102                 if (frame == ts->talloc_stack[i]) {
103                         break;
104                 }
105                 talloc_free(ts->talloc_stack[i]);
106                 ts->talloc_stack[i] = NULL;
107         }
108
109         ts->talloc_stack[i] = NULL;
110         ts->talloc_stacksize = i;
111         return 0;
112 }
113
114 /*
115  * Create a new talloc stack frame.
116  *
117  * When free'd, it frees all stack frames that were created after this one and
118  * not explicitly freed.
119  */
120
121 static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
122 {
123         TALLOC_CTX **tmp, *top, *parent;
124         struct talloc_stackframe *ts =
125                 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
126
127         if (ts == NULL) {
128                 ts = talloc_stackframe_create();
129         }
130
131         if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
132                 tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
133                                            ts->talloc_stacksize + 1);
134                 if (tmp == NULL) {
135                         goto fail;
136                 }
137                 ts->talloc_stack = tmp;
138                 ts->talloc_stack_arraysize = ts->talloc_stacksize + 1;
139         }
140
141         if (ts->talloc_stacksize == 0) {
142                 parent = ts->talloc_stack;
143         } else {
144                 parent = ts->talloc_stack[ts->talloc_stacksize-1];
145         }
146
147         if (poolsize) {
148                 top = talloc_pool(parent, poolsize);
149         } else {
150                 top = talloc_new(parent);
151         }
152
153         if (top == NULL) {
154                 goto fail;
155         }
156
157         talloc_set_destructor(top, talloc_pop);
158
159         ts->talloc_stack[ts->talloc_stacksize++] = top;
160         return top;
161
162  fail:
163         smb_panic("talloc_stackframe failed");
164         return NULL;
165 }
166
167 TALLOC_CTX *talloc_stackframe(void)
168 {
169         return talloc_stackframe_internal(0);
170 }
171
172 TALLOC_CTX *talloc_stackframe_pool(size_t poolsize)
173 {
174         return talloc_stackframe_internal(poolsize);
175 }
176
177 /*
178  * Get us the current top of the talloc stack.
179  */
180
181 TALLOC_CTX *talloc_tos(void)
182 {
183         struct talloc_stackframe *ts =
184                 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
185
186         if (ts == NULL || ts->talloc_stacksize == 0) {
187                 talloc_stackframe();
188                 ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
189                 DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
190         }
191
192         return ts->talloc_stack[ts->talloc_stacksize-1];
193 }