root/lib/util/talloc_stack.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. talloc_pop
  2. talloc_stackframe_internal
  3. talloc_stackframe
  4. talloc_stackframe_pool
  5. talloc_tos

   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 int talloc_stack_arraysize;
  43 static TALLOC_CTX **talloc_stack;
  44 
  45 static int talloc_pop(TALLOC_CTX *frame)
     /* [<][>][^][v][top][bottom][index][help] */
  46 {
  47         int i;
  48 
  49         for (i=talloc_stacksize-1; i>0; i--) {
  50                 if (frame == talloc_stack[i]) {
  51                         break;
  52                 }
  53                 talloc_free(talloc_stack[i]);
  54         }
  55 
  56         talloc_stacksize = i;
  57         return 0;
  58 }
  59 
  60 /*
  61  * Create a new talloc stack frame.
  62  *
  63  * When free'd, it frees all stack frames that were created after this one and
  64  * not explicitly freed.
  65  */
  66 
  67 static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
     /* [<][>][^][v][top][bottom][index][help] */
  68 {
  69         TALLOC_CTX **tmp, *top, *parent;
  70 
  71         if (talloc_stack_arraysize < talloc_stacksize + 1) {
  72                 tmp = talloc_realloc(NULL, talloc_stack, TALLOC_CTX *,
  73                                            talloc_stacksize + 1);
  74                 if (tmp == NULL) {
  75                         goto fail;
  76                 }
  77                 talloc_stack = tmp;
  78                 talloc_stack_arraysize = talloc_stacksize + 1;
  79         }
  80 
  81         if (talloc_stacksize == 0) {
  82                 parent = talloc_stack;
  83         }
  84         else {
  85                 parent = talloc_stack[talloc_stacksize-1];
  86         }
  87 
  88         if (poolsize) {
  89                 top = talloc_pool(parent, poolsize);
  90         } else {
  91                 top = talloc_new(parent);
  92         }
  93 
  94         if (top == NULL) {
  95                 goto fail;
  96         }
  97 
  98         talloc_set_destructor(top, talloc_pop);
  99 
 100         talloc_stack[talloc_stacksize++] = top;
 101         return top;
 102 
 103  fail:
 104         smb_panic("talloc_stackframe failed");
 105         return NULL;
 106 }
 107 
 108 TALLOC_CTX *talloc_stackframe(void)
     /* [<][>][^][v][top][bottom][index][help] */
 109 {
 110         return talloc_stackframe_internal(0);
 111 }
 112 
 113 TALLOC_CTX *talloc_stackframe_pool(size_t poolsize)
     /* [<][>][^][v][top][bottom][index][help] */
 114 {
 115         return talloc_stackframe_internal(poolsize);
 116 }
 117 
 118 /*
 119  * Get us the current top of the talloc stack.
 120  */
 121 
 122 TALLOC_CTX *talloc_tos(void)
     /* [<][>][^][v][top][bottom][index][help] */
 123 {
 124         if (talloc_stacksize == 0) {
 125                 talloc_stackframe();
 126                 DEBUG(0, ("no talloc stackframe around, leaking memory\n"));
 127         }
 128 
 129         return talloc_stack[talloc_stacksize-1];
 130 }

/* [<][>][^][v][top][bottom][index][help] */