r24809: Consolidate the use of temporary talloc contexts.
[ira/wip.git] / source / lib / talloc_stack.c
1 /*
2    Unix SMB/CIFS implementation.
3    Implement a stack of talloc contexts
4    Copyright (C) Volker Lendecke 2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22  * Implement a stack of talloc frames.
23  *
24  * When a new talloc stackframe is allocated with talloc_stackframe(), then
25  * the TALLOC_CTX returned with talloc_tos() is reset to that new
26  * frame. Whenever that stack frame is TALLOC_FREE()'ed, then the reverse
27  * happens: The previous talloc_tos() is restored.
28  *
29  * This API is designed to be robust in the sense that if someone forgets to
30  * TALLOC_FREE() a stackframe, then the next outer one correctly cleans up and
31  * resets the talloc_tos().
32  *
33  * This robustness feature means that we can't rely on a linked list with
34  * talloc destructors because in a hierarchy of talloc destructors the parent
35  * destructor is called before its children destructors. The child destructor
36  * called after the parent would set the talloc_tos() to the wrong value.
37  */
38
39 #include "includes.h"
40
41 static int talloc_stacksize;
42 static TALLOC_CTX **talloc_stack;
43
44 static int talloc_pop(int *ptr)
45 {
46         int tos = *ptr;
47         int i;
48
49         for (i=talloc_stacksize-1; i>=tos; i--) {
50                 talloc_free(talloc_stack[i]);
51         }
52
53         talloc_stacksize = tos;
54         return 0;
55 }
56
57 /*
58  * Create a new talloc stack frame.
59  *
60  * When free'd, it frees all stack frames that were created after this one and
61  * not explicitly freed.
62  */
63
64 TALLOC_CTX *talloc_stackframe(void)
65 {
66         TALLOC_CTX **tmp, *top;
67         int *cleanup;
68
69         if (!(tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *,
70                                          talloc_stacksize + 1))) {
71                 goto fail;
72         }
73
74         talloc_stack = tmp;
75
76         if (!(top = talloc_new(talloc_stack))) {
77                 goto fail;
78         }
79
80         if (!(cleanup = talloc(top, int))) {
81                 goto fail;
82         }
83
84         *cleanup = talloc_stacksize;
85         talloc_set_destructor(cleanup, talloc_pop);
86
87         talloc_stack[talloc_stacksize++] = top;
88
89         return top;
90
91  fail:
92         smb_panic("talloc_stackframe failed");
93         return NULL;
94 }
95
96 /*
97  * Get us the current top of the talloc stack.
98  */
99
100 TALLOC_CTX *talloc_tos(void)
101 {
102         if (talloc_stacksize == 0) {
103                 DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
104                 talloc_stackframe();
105         }
106
107         return talloc_stack[talloc_stacksize-1];
108 }