source: vendor/current/lib/util/talloc_stack.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 5.9 KB
Line 
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
42struct 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
56static void *global_ts;
57
58/* Variable to ensure TLS value is only initialized once. */
59static smb_thread_once_t ts_initialized = SMB_THREAD_ONCE_INIT;
60
61static 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
68static struct talloc_stackframe *talloc_stackframe_create(void)
69{
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
80
81 if (!ts) {
82 smb_panic("talloc_stackframe_init malloc failed");
83 }
84
85 SMB_THREAD_ONCE(&ts_initialized, talloc_stackframe_init, NULL);
86
87 if (SMB_THREAD_SET_TLS(global_ts, ts)) {
88 smb_panic("talloc_stackframe_init set_tls failed");
89 }
90 return ts;
91}
92
93static int talloc_pop(TALLOC_CTX *frame)
94{
95 struct talloc_stackframe *ts =
96 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
97 int i;
98
99 /* Catch lazy frame-freeing. */
100 if (ts->talloc_stack[ts->talloc_stacksize-1] != frame) {
101 DEBUG(0, ("Freed frame %s, expected %s.\n",
102 talloc_get_name(frame),
103 talloc_get_name(ts->talloc_stack
104 [ts->talloc_stacksize-1])));
105#ifdef DEVELOPER
106 smb_panic("Frame not freed in order.");
107#endif
108 }
109
110 for (i=ts->talloc_stacksize-1; i>0; i--) {
111 if (frame == ts->talloc_stack[i]) {
112 break;
113 }
114 TALLOC_FREE(ts->talloc_stack[i]);
115 }
116
117 ts->talloc_stack[i] = NULL;
118 ts->talloc_stacksize = i;
119 return 0;
120}
121
122/*
123 * Create a new talloc stack frame.
124 *
125 * When free'd, it frees all stack frames that were created after this one and
126 * not explicitly freed.
127 */
128
129static TALLOC_CTX *talloc_stackframe_internal(const char *location,
130 size_t poolsize)
131{
132 TALLOC_CTX **tmp, *top;
133 struct talloc_stackframe *ts =
134 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
135
136 if (ts == NULL) {
137 ts = talloc_stackframe_create();
138 }
139
140 if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
141 tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
142 ts->talloc_stacksize + 1);
143 if (tmp == NULL) {
144 goto fail;
145 }
146 ts->talloc_stack = tmp;
147 ts->talloc_stack_arraysize = ts->talloc_stacksize + 1;
148 }
149
150 if (poolsize) {
151 top = talloc_pool(ts->talloc_stack, poolsize);
152 } else {
153 TALLOC_CTX *parent;
154 /* We chain parentage, so if one is a pool we draw from it. */
155 if (ts->talloc_stacksize == 0) {
156 parent = ts->talloc_stack;
157 } else {
158 parent = ts->talloc_stack[ts->talloc_stacksize-1];
159 }
160 top = talloc_new(parent);
161 }
162
163 if (top == NULL) {
164 goto fail;
165 }
166 talloc_set_name_const(top, location);
167 talloc_set_destructor(top, talloc_pop);
168
169 ts->talloc_stack[ts->talloc_stacksize++] = top;
170 return top;
171
172 fail:
173 smb_panic("talloc_stackframe failed");
174 return NULL;
175}
176
177TALLOC_CTX *_talloc_stackframe(const char *location)
178{
179 return talloc_stackframe_internal(location, 0);
180}
181
182TALLOC_CTX *_talloc_stackframe_pool(const char *location, size_t poolsize)
183{
184 return talloc_stackframe_internal(location, poolsize);
185}
186
187/*
188 * Get us the current top of the talloc stack.
189 */
190
191TALLOC_CTX *_talloc_tos(const char *location)
192{
193 struct talloc_stackframe *ts =
194 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
195
196 if (ts == NULL || ts->talloc_stacksize == 0) {
197 _talloc_stackframe(location);
198 ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
199 DEBUG(0, ("no talloc stackframe at %s, leaking memory\n",
200 location));
201#ifdef DEVELOPER
202 smb_panic("No talloc stackframe");
203#endif
204 }
205
206 return ts->talloc_stack[ts->talloc_stacksize-1];
207}
208
209/*
210 * return true if a talloc stackframe exists
211 * this can be used to prevent memory leaks for code that can
212 * optionally use a talloc stackframe (eg. nt_errstr())
213 */
214
215bool talloc_stackframe_exists(void)
216{
217 struct talloc_stackframe *ts =
218 (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
219
220 if (ts == NULL || ts->talloc_stacksize == 0) {
221 return false;
222 }
223 return true;
224}
Note: See TracBrowser for help on using the repository browser.