split some security related functions in their own files.
[Samba/bb.git] / source3 / lib / talloc.c
blob485dc28f31d3897656cc5a1da1cf9151db8e94bd
1 /*
2 Samba Unix SMB/CIFS implementation.
3 Samba temporary memory allocation functions
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
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 /**
23 @defgroup talloc Simple memory allocator
26 This is a very simple temporary memory allocator. To use it do the following:
28 1) when you first want to allocate a pool of meomry use
29 talloc_init() and save the resulting context pointer somewhere
31 2) to allocate memory use talloc()
33 3) when _all_ of the memory allocated using this context is no longer needed
34 use talloc_destroy()
36 talloc does not zero the memory. It guarantees memory of a
37 TALLOC_ALIGN alignment
39 @sa talloc.h
42 /**
43 * @todo We could allocate both the talloc_chunk structure, and the
44 * memory it contains all in one allocation, which might be a bit
45 * faster and perhaps use less memory overhead.
47 * That smells like a premature optimization, though. -- mbp
48 **/
50 /**
51 * If you want testing for memory corruption, link with dmalloc or use
52 * Insure++. It doesn't seem useful to duplicate them here.
53 **/
55 #include "includes.h"
58 /**
59 * Start of linked list of all talloc pools.
61 * @todo We should turn the global list off when using Insure++,
62 * otherwise all the memory will be seen as still reachable.
63 **/
64 static TALLOC_CTX *list_head = NULL;
67 /**
68 * Add to the global list
69 **/
70 static void talloc_enroll(TALLOC_CTX *t)
72 t->next_ctx = list_head;
73 list_head = t;
77 static void talloc_disenroll(TALLOC_CTX *t)
79 TALLOC_CTX **ttmp;
81 /* Use a double-* so that no special case is required for the
82 * list head. */
83 for (ttmp = &list_head; *ttmp; ttmp = &((*ttmp)->next_ctx))
84 if (*ttmp == t) {
85 /* ttmp is the link that points to t, either
86 * list_head or the next_ctx link in its
87 * predecessor */
88 *ttmp = t->next_ctx;
89 t->next_ctx = NULL; /* clobber */
90 return;
92 abort(); /* oops, this talloc was already
93 * clobbered or something else went
94 * wrong. */
98 /** Create a new talloc context. **/
99 static TALLOC_CTX *talloc_init_internal(void)
101 TALLOC_CTX *t;
103 t = (TALLOC_CTX *)malloc(sizeof(TALLOC_CTX));
104 if (t) {
105 t->list = NULL;
106 t->total_alloc_size = 0;
107 t->name = NULL;
108 talloc_enroll(t);
111 return t;
117 * Create a new talloc context, with a name specifying its purpose.
120 TALLOC_CTX *talloc_init(char const *fmt, ...)
122 TALLOC_CTX *t;
123 va_list ap;
125 t = talloc_init_internal();
126 if (t && fmt) {
128 * t->name must not be talloced.
129 * as destroying the pool would destroy it. JRA.
131 t->name = NULL;
132 va_start(ap, fmt);
133 vasprintf(&t->name, fmt, ap);
134 va_end(ap);
135 if (!t->name) {
136 talloc_destroy(t);
137 t = NULL;
141 return t;
145 /** Allocate a bit of memory from the specified pool **/
146 void *talloc(TALLOC_CTX *t, size_t size)
148 void *p;
149 struct talloc_chunk *tc;
151 if (!t || size == 0) return NULL;
153 p = malloc(size);
154 if (p) {
155 tc = malloc(sizeof(*tc));
156 if (tc) {
157 tc->ptr = p;
158 tc->size = size;
159 tc->next = t->list;
160 t->list = tc;
161 t->total_alloc_size += size;
163 else {
164 SAFE_FREE(p);
167 return p;
170 /** A talloc version of realloc */
171 void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
173 struct talloc_chunk *tc;
174 void *new_ptr;
176 /* size zero is equivalent to free() */
177 if (!t || size == 0)
178 return NULL;
180 /* realloc(NULL) is equavalent to malloc() */
181 if (ptr == NULL)
182 return talloc(t, size);
184 for (tc=t->list; tc; tc=tc->next) {
185 if (tc->ptr == ptr) {
186 new_ptr = Realloc(ptr, size);
187 if (new_ptr) {
188 t->total_alloc_size += (size - tc->size);
189 tc->size = size;
190 tc->ptr = new_ptr;
192 return new_ptr;
195 return NULL;
198 /** Destroy all the memory allocated inside @p t, but not @p t
199 * itself. */
200 void talloc_destroy_pool(TALLOC_CTX *t)
202 struct talloc_chunk *c;
204 if (!t)
205 return;
207 while (t->list) {
208 c = t->list->next;
209 SAFE_FREE(t->list->ptr);
210 SAFE_FREE(t->list);
211 t->list = c;
214 t->total_alloc_size = 0;
217 /** Destroy a whole pool including the context */
218 void talloc_destroy(TALLOC_CTX *t)
220 if (!t)
221 return;
223 talloc_destroy_pool(t);
224 talloc_disenroll(t);
225 SAFE_FREE(t->name);
226 memset(t, 0, sizeof(TALLOC_CTX));
227 SAFE_FREE(t);
230 /** Return the current total size of the pool. */
231 size_t talloc_pool_size(TALLOC_CTX *t)
233 if (t)
234 return t->total_alloc_size;
235 else
236 return 0;
239 const char * talloc_pool_name(TALLOC_CTX const *t)
241 if (t)
242 return t->name;
243 else
244 return NULL;
248 /** talloc and zero memory. */
249 void *talloc_zero(TALLOC_CTX *t, size_t size)
251 void *p = talloc(t, size);
253 if (p)
254 memset(p, '\0', size);
256 return p;
259 /** memdup with a talloc. */
260 void *talloc_memdup(TALLOC_CTX *t, const void *p, size_t size)
262 void *newp = talloc(t,size);
264 if (newp)
265 memcpy(newp, p, size);
267 return newp;
270 /** strdup with a talloc */
271 char *talloc_strdup(TALLOC_CTX *t, const char *p)
273 if (p)
274 return talloc_memdup(t, p, strlen(p) + 1);
275 else
276 return NULL;
279 /** strdup_w with a talloc */
280 smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
282 if (p)
283 return talloc_memdup(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t));
284 else
285 return NULL;
289 * Perform string formatting, and return a pointer to newly allocated
290 * memory holding the result, inside a memory pool.
292 char *talloc_asprintf(TALLOC_CTX *t, const char *fmt, ...)
294 va_list ap;
295 char *ret;
297 va_start(ap, fmt);
298 ret = talloc_vasprintf(t, fmt, ap);
299 va_end(ap);
300 return ret;
304 char *talloc_vasprintf(TALLOC_CTX *t, const char *fmt, va_list ap)
306 int len;
307 char *ret;
308 va_list ap2;
310 VA_COPY(ap2, ap);
312 len = vsnprintf(NULL, 0, fmt, ap2);
314 ret = talloc(t, len+1);
315 if (ret) {
316 VA_COPY(ap2, ap);
317 vsnprintf(ret, len+1, fmt, ap2);
320 return ret;
325 * Realloc @p s to append the formatted result of @p fmt and return @p
326 * s, which may have moved. Good for gradually accumulating output
327 * into a string buffer.
329 char *talloc_asprintf_append(TALLOC_CTX *t, char *s,
330 const char *fmt, ...)
332 va_list ap;
334 va_start(ap, fmt);
335 s = talloc_vasprintf_append(t, s, fmt, ap);
336 va_end(ap);
337 return s;
343 * Realloc @p s to append the formatted result of @p fmt and @p ap,
344 * and return @p s, which may have moved. Good for gradually
345 * accumulating output into a string buffer.
347 char *talloc_vasprintf_append(TALLOC_CTX *t, char *s,
348 const char *fmt, va_list ap)
350 int len, s_len;
351 va_list ap2;
353 VA_COPY(ap2, ap);
355 s_len = strlen(s);
356 len = vsnprintf(NULL, 0, fmt, ap2);
358 s = talloc_realloc(t, s, s_len + len+1);
359 if (!s) return NULL;
361 VA_COPY(ap2, ap);
363 vsnprintf(s+s_len, len+1, fmt, ap2);
365 return s;
370 * Return a human-readable description of all talloc memory usage.
371 * The result is allocated from @p t.
373 char *talloc_describe_all(TALLOC_CTX *rt)
375 int n_pools = 0, total_chunks = 0;
376 size_t total_bytes = 0;
377 TALLOC_CTX *it;
378 char *s;
380 if (!rt) return NULL;
382 s = talloc_asprintf(rt, "global talloc allocations in pid: %u\n",
383 (unsigned) sys_getpid());
384 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
385 "name", "chunks", "bytes");
386 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
387 "----------------------------------------",
388 "--------",
389 "--------");
391 for (it = list_head; it; it = it->next_ctx) {
392 size_t bytes;
393 int n_chunks;
394 fstring what;
396 n_pools++;
398 talloc_get_allocation(it, &bytes, &n_chunks);
400 if (it->name)
401 fstrcpy(what, it->name);
402 else
403 slprintf(what, sizeof(what), "@%p", it);
405 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
406 what,
407 (unsigned) n_chunks,
408 (unsigned) bytes);
409 total_bytes += bytes;
410 total_chunks += n_chunks;
413 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
414 "----------------------------------------",
415 "--------",
416 "--------");
418 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
419 "TOTAL",
420 (unsigned) total_chunks, (unsigned) total_bytes);
422 return s;
428 * Return an estimated memory usage for the specified pool. This does
429 * not include memory used by the underlying malloc implementation.
431 void talloc_get_allocation(TALLOC_CTX *t,
432 size_t *total_bytes,
433 int *n_chunks)
435 struct talloc_chunk *chunk;
437 if (t) {
438 *total_bytes = 0;
439 *n_chunks = 0;
441 for (chunk = t->list; chunk; chunk = chunk->next) {
442 n_chunks[0]++;
443 *total_bytes += chunk->size;
449 /** @} */