Fix scripts to generate correct tables for compilers which have character constants...
[Samba/gebeck_regimport.git] / source3 / lib / talloc.c
blobb6c8b2efdf09a80fe9f53c696762382b54b1c33a
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"
57 struct talloc_chunk {
58 struct talloc_chunk *next;
59 size_t size;
60 void *ptr;
64 struct talloc_ctx {
65 struct talloc_chunk *list;
66 size_t total_alloc_size;
68 /** The name recorded for this pool, if any. Should describe
69 * the purpose for which it was allocated. The string is
70 * allocated within the pool. **/
71 char *name;
73 /** Pointer to the next allocate talloc pool, so that we can
74 * summarize all talloc memory usage. **/
75 struct talloc_ctx *next_ctx;
79 /**
80 * Start of linked list of all talloc pools.
82 * @todo We should turn the global list off when using Insure++,
83 * otherwise all the memory will be seen as still reachable.
84 **/
85 static TALLOC_CTX *list_head = NULL;
88 /**
89 * Add to the global list
90 **/
91 static void talloc_enroll(TALLOC_CTX *t)
93 t->next_ctx = list_head;
94 list_head = t;
98 static void talloc_disenroll(TALLOC_CTX *t)
100 TALLOC_CTX **ttmp;
102 /* Use a double-* so that no special case is required for the
103 * list head. */
104 for (ttmp = &list_head; *ttmp; ttmp = &((*ttmp)->next_ctx))
105 if (*ttmp == t) {
106 /* ttmp is the link that points to t, either
107 * list_head or the next_ctx link in its
108 * predecessor */
109 *ttmp = t->next_ctx;
110 t->next_ctx = NULL; /* clobber */
111 return;
113 abort(); /* oops, this talloc was already
114 * clobbered or something else went
115 * wrong. */
119 /** Create a new talloc context. **/
120 static TALLOC_CTX *talloc_init_internal(void)
122 TALLOC_CTX *t;
124 t = (TALLOC_CTX *)malloc(sizeof(TALLOC_CTX));
125 if (t) {
126 t->list = NULL;
127 t->total_alloc_size = 0;
128 t->name = NULL;
129 talloc_enroll(t);
132 return t;
138 * Create a new talloc context, with a name specifying its purpose.
141 TALLOC_CTX *talloc_init(char const *fmt, ...)
143 TALLOC_CTX *t;
144 va_list ap;
146 t = talloc_init_internal();
147 if (t && fmt) {
149 * t->name must not be talloced.
150 * as destroying the pool would destroy it. JRA.
152 t->name = NULL;
153 va_start(ap, fmt);
154 vasprintf(&t->name, fmt, ap);
155 va_end(ap);
156 if (!t->name) {
157 talloc_destroy(t);
158 t = NULL;
162 return t;
166 /** Allocate a bit of memory from the specified pool **/
167 void *talloc(TALLOC_CTX *t, size_t size)
169 void *p;
170 struct talloc_chunk *tc;
172 if (!t || size == 0) return NULL;
174 p = malloc(size);
175 if (p) {
176 tc = malloc(sizeof(*tc));
177 if (tc) {
178 tc->ptr = p;
179 tc->size = size;
180 tc->next = t->list;
181 t->list = tc;
182 t->total_alloc_size += size;
184 else {
185 SAFE_FREE(p);
188 return p;
191 /** A talloc version of realloc */
192 void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
194 struct talloc_chunk *tc;
195 void *new_ptr;
197 /* size zero is equivalent to free() */
198 if (!t || size == 0)
199 return NULL;
201 /* realloc(NULL) is equavalent to malloc() */
202 if (ptr == NULL)
203 return talloc(t, size);
205 for (tc=t->list; tc; tc=tc->next) {
206 if (tc->ptr == ptr) {
207 new_ptr = Realloc(ptr, size);
208 if (new_ptr) {
209 t->total_alloc_size += (size - tc->size);
210 tc->size = size;
211 tc->ptr = new_ptr;
213 return new_ptr;
216 return NULL;
219 /** Destroy all the memory allocated inside @p t, but not @p t
220 * itself. */
221 void talloc_destroy_pool(TALLOC_CTX *t)
223 struct talloc_chunk *c;
225 if (!t)
226 return;
228 while (t->list) {
229 c = t->list->next;
230 SAFE_FREE(t->list->ptr);
231 SAFE_FREE(t->list);
232 t->list = c;
235 t->total_alloc_size = 0;
238 /** Destroy a whole pool including the context */
239 void talloc_destroy(TALLOC_CTX *t)
241 if (!t)
242 return;
244 talloc_destroy_pool(t);
245 talloc_disenroll(t);
246 SAFE_FREE(t->name);
247 memset(t, 0, sizeof(TALLOC_CTX));
248 SAFE_FREE(t);
251 /** Return the current total size of the pool. */
252 size_t talloc_pool_size(TALLOC_CTX *t)
254 if (t)
255 return t->total_alloc_size;
256 else
257 return 0;
260 const char * talloc_pool_name(TALLOC_CTX const *t)
262 if (t)
263 return t->name;
264 else
265 return NULL;
269 /** talloc and zero memory. */
270 void *talloc_zero(TALLOC_CTX *t, size_t size)
272 void *p = talloc(t, size);
274 if (p)
275 memset(p, '\0', size);
277 return p;
280 /** memdup with a talloc. */
281 void *talloc_memdup(TALLOC_CTX *t, const void *p, size_t size)
283 void *newp = talloc(t,size);
285 if (newp)
286 memcpy(newp, p, size);
288 return newp;
291 /** strdup with a talloc */
292 char *talloc_strdup(TALLOC_CTX *t, const char *p)
294 if (p)
295 return talloc_memdup(t, p, strlen(p) + 1);
296 else
297 return NULL;
300 /** strdup_w with a talloc */
301 smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
303 if (p)
304 return talloc_memdup(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t));
305 else
306 return NULL;
310 * Perform string formatting, and return a pointer to newly allocated
311 * memory holding the result, inside a memory pool.
313 char *talloc_asprintf(TALLOC_CTX *t, const char *fmt, ...)
315 va_list ap;
316 char *ret;
318 va_start(ap, fmt);
319 ret = talloc_vasprintf(t, fmt, ap);
320 va_end(ap);
321 return ret;
325 char *talloc_vasprintf(TALLOC_CTX *t, const char *fmt, va_list ap)
327 int len;
328 char *ret;
329 va_list ap2;
331 VA_COPY(ap2, ap);
333 len = vsnprintf(NULL, 0, fmt, ap2);
335 ret = talloc(t, len+1);
336 if (ret) {
337 VA_COPY(ap2, ap);
338 vsnprintf(ret, len+1, fmt, ap2);
341 return ret;
346 * Realloc @p s to append the formatted result of @p fmt and return @p
347 * s, which may have moved. Good for gradually accumulating output
348 * into a string buffer.
350 char *talloc_asprintf_append(TALLOC_CTX *t, char *s,
351 const char *fmt, ...)
353 va_list ap;
355 va_start(ap, fmt);
356 s = talloc_vasprintf_append(t, s, fmt, ap);
357 va_end(ap);
358 return s;
364 * Realloc @p s to append the formatted result of @p fmt and @p ap,
365 * and return @p s, which may have moved. Good for gradually
366 * accumulating output into a string buffer.
368 char *talloc_vasprintf_append(TALLOC_CTX *t, char *s,
369 const char *fmt, va_list ap)
371 int len, s_len;
372 va_list ap2;
374 VA_COPY(ap2, ap);
376 s_len = strlen(s);
377 len = vsnprintf(NULL, 0, fmt, ap2);
379 s = talloc_realloc(t, s, s_len + len+1);
380 if (!s) return NULL;
382 VA_COPY(ap2, ap);
384 vsnprintf(s+s_len, len+1, fmt, ap2);
386 return s;
391 * Return a human-readable description of all talloc memory usage.
392 * The result is allocated from @p t.
394 char *talloc_describe_all(TALLOC_CTX *rt)
396 int n_pools = 0, total_chunks = 0;
397 size_t total_bytes = 0;
398 TALLOC_CTX *it;
399 char *s;
401 if (!rt) return NULL;
403 s = talloc_asprintf(rt, "global talloc allocations in pid: %u\n",
404 (unsigned) sys_getpid());
405 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
406 "name", "chunks", "bytes");
407 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
408 "----------------------------------------",
409 "--------",
410 "--------");
412 for (it = list_head; it; it = it->next_ctx) {
413 size_t bytes;
414 int n_chunks;
415 fstring what;
417 n_pools++;
419 talloc_get_allocation(it, &bytes, &n_chunks);
421 if (it->name)
422 fstrcpy(what, it->name);
423 else
424 slprintf(what, sizeof(what), "@%p", it);
426 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
427 what,
428 (unsigned) n_chunks,
429 (unsigned) bytes);
430 total_bytes += bytes;
431 total_chunks += n_chunks;
434 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
435 "----------------------------------------",
436 "--------",
437 "--------");
439 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
440 "TOTAL",
441 (unsigned) total_chunks, (unsigned) total_bytes);
443 return s;
449 * Return an estimated memory usage for the specified pool. This does
450 * not include memory used by the underlying malloc implementation.
452 void talloc_get_allocation(TALLOC_CTX *t,
453 size_t *total_bytes,
454 int *n_chunks)
456 struct talloc_chunk *chunk;
458 if (t) {
459 *total_bytes = 0;
460 *n_chunks = 0;
462 for (chunk = t->list; chunk; chunk = chunk->next) {
463 n_chunks[0]++;
464 *total_bytes += chunk->size;
470 /** @} */