preparing for release of 2.2.3a
[Samba.git] / source / lib / talloc.c
blobfb5c3495fe43a9a00762efeddf45bd3a088cc0c6
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 Samba temporary memory allocation functions
5 Copyright (C) Andrew Tridgell 2000
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.
22 /* this is a very simple temporary memory allocator. To use it do the following:
24 1) when you first want to allocate a pool of meomry use
25 talloc_init() and save the resulting context pointer somewhere
27 2) to allocate memory use talloc()
29 3) when _all_ of the memory allocated using this context is no longer needed
30 use talloc_destroy()
32 talloc does not zero the memory. It guarantees memory of a
33 TALLOC_ALIGN alignment
36 #include "includes.h"
38 /* initialise talloc context. */
39 TALLOC_CTX *talloc_init(void)
41 TALLOC_CTX *t;
43 t = (TALLOC_CTX *)malloc(sizeof(*t));
44 if (!t) return NULL;
46 t->list = NULL;
47 t->total_alloc_size = 0;
49 return t;
52 /* allocate a bit of memory from the specified pool */
53 void *talloc(TALLOC_CTX *t, size_t size)
55 void *p;
56 struct talloc_chunk *tc;
58 if (size == 0) return NULL;
60 p = malloc(size);
61 if (!p) return p;
63 tc = malloc(sizeof(*tc));
64 if (!tc) {
65 SAFE_FREE(p);
66 return NULL;
69 tc->ptr = p;
70 tc->size = size;
71 tc->next = t->list;
72 t->list = tc;
73 t->total_alloc_size += size;
75 return p;
78 /* a talloc version of realloc */
79 void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
81 struct talloc_chunk *tc;
82 void *new_ptr;
84 /* size zero is equivalent to free() */
85 if (size == 0)
86 return NULL;
88 /* realloc(NULL) is equavalent to malloc() */
89 if (ptr == NULL)
90 return talloc(t, size);
92 for (tc=t->list; tc; tc=tc->next) {
93 if (tc->ptr == ptr) {
94 new_ptr = Realloc(ptr, size);
95 if (new_ptr) {
96 t->total_alloc_size += (size - tc->size);
97 tc->size = size;
98 tc->ptr = new_ptr;
100 return new_ptr;
103 return NULL;
106 /* destroy a whole pool */
107 void talloc_destroy_pool(TALLOC_CTX *t)
109 struct talloc_chunk *c;
111 if (!t)
112 return;
114 while (t->list) {
115 c = t->list->next;
116 SAFE_FREE(t->list->ptr);
117 SAFE_FREE(t->list);
118 t->list = c;
121 t->list = NULL;
122 t->total_alloc_size = 0;
125 /* destroy a whole pool including the context */
126 void talloc_destroy(TALLOC_CTX *t)
128 if (!t)
129 return;
130 talloc_destroy_pool(t);
131 memset(t, 0, sizeof(*t));
132 SAFE_FREE(t);
135 /* return the current total size of the pool. */
136 size_t talloc_pool_size(TALLOC_CTX *t)
138 return t->total_alloc_size;
141 /* talloc and zero memory. */
142 void *talloc_zero(TALLOC_CTX *t, size_t size)
144 void *p = talloc(t, size);
146 if (p)
147 memset(p, '\0', size);
149 return p;
152 /* memdup with a talloc. */
153 void *talloc_memdup(TALLOC_CTX *t, void *p, size_t size)
155 void *newp = talloc(t,size);
157 if (!newp)
158 return 0;
160 memcpy(newp, p, size);
162 return newp;
165 /* strdup with a talloc */
166 char *talloc_strdup(TALLOC_CTX *t, char *p)
168 return talloc_memdup(t, p, strlen(p) + 1);