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.
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
36 talloc does not zero the memory. It guarantees memory of a
37 TALLOC_ALIGN alignment
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
51 * If you want testing for memory corruption, link with dmalloc or use
52 * Insure++. It doesn't seem useful to duplicate them here.
57 /* Max allowable allococation - 256mb - 0x10000000 */
58 #define MAX_TALLOC_SIZE (1024*1024*256)
61 * Start of linked list of all talloc pools.
63 * @todo We should turn the global list off when using Insure++,
64 * otherwise all the memory will be seen as still reachable.
66 static TALLOC_CTX *list_head = NULL;
70 * Add to the global list
72 static void talloc_enroll(TALLOC_CTX *t)
74 t->next_ctx = list_head;
79 static void talloc_disenroll(TALLOC_CTX *t)
83 /* Use a double-* so that no special case is required for the
85 for (ttmp = &list_head; *ttmp; ttmp = &((*ttmp)->next_ctx))
87 /* ttmp is the link that points to t, either
88 * list_head or the next_ctx link in its
91 t->next_ctx = NULL; /* clobber */
94 abort(); /* oops, this talloc was already
95 * clobbered or something else went
100 /** Create a new talloc context. **/
101 static TALLOC_CTX *talloc_init_internal(void)
105 t = (TALLOC_CTX *)SMB_MALLOC(sizeof(TALLOC_CTX));
108 t->total_alloc_size = 0;
119 * Create a new talloc context, with a name specifying its purpose.
122 TALLOC_CTX *talloc_init(char const *fmt, ...)
127 t = talloc_init_internal();
130 * t->name must not be talloced.
131 * as destroying the pool would destroy it. JRA.
135 vasprintf(&t->name, fmt, ap);
147 /** Allocate a bit of memory from the specified pool **/
148 #if defined(PARANOID_MALLOC_CHECKER)
149 void *talloc_(TALLOC_CTX *t, size_t size)
151 void *talloc(TALLOC_CTX *t, size_t size)
155 struct talloc_chunk *tc;
157 if (!t || size == 0) return NULL;
159 p = SMB_MALLOC(size);
161 tc = SMB_MALLOC(sizeof(*tc));
167 t->total_alloc_size += size;
176 /** Allocate an array of count elements of size x */
177 #if defined(PARANOID_MALLOC_CHECKER)
178 void *talloc_array_(TALLOC_CTX *ctx, size_t el_size, unsigned int count)
180 void *talloc_array(TALLOC_CTX *ctx, size_t el_size, unsigned int count)
183 if (count >= MAX_TALLOC_SIZE/el_size) {
186 return TALLOC(ctx, el_size * count);
189 /** A talloc version of realloc */
190 #if defined(PARANOID_MALLOC_CHECKER)
191 void *talloc_realloc_(TALLOC_CTX *t, void *ptr, size_t size)
193 void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size)
196 struct talloc_chunk *tc;
199 /* size zero is equivalent to free() */
203 /* realloc(NULL) is equavalent to malloc() */
205 return TALLOC(t, size);
207 for (tc=t->list; tc; tc=tc->next) {
208 if (tc->ptr == ptr) {
209 new_ptr = SMB_REALLOC(ptr, size);
211 t->total_alloc_size += (size - tc->size);
221 /** Re-allocate an array of count elements of size x */
222 #if defined(PARANOID_MALLOC_CHECKER)
223 void *talloc_realloc_array_(TALLOC_CTX *ctx, void *ptr, size_t el_size, unsigned int count)
225 void *talloc_realloc_array(TALLOC_CTX *ctx, void *ptr, size_t el_size, unsigned int count)
228 if (count >= MAX_TALLOC_SIZE/el_size) {
231 return TALLOC_REALLOC(ctx, ptr, el_size * count);
234 /** Destroy all the memory allocated inside @p t, but not @p t
236 void talloc_destroy_pool(TALLOC_CTX *t)
238 struct talloc_chunk *c;
245 SAFE_FREE(t->list->ptr);
250 t->total_alloc_size = 0;
253 /** Destroy a whole pool including the context */
254 void talloc_destroy(TALLOC_CTX *t)
259 talloc_destroy_pool(t);
262 memset(t, 0, sizeof(TALLOC_CTX));
266 /** Return the current total size of the pool. */
267 size_t talloc_pool_size(TALLOC_CTX *t)
270 return t->total_alloc_size;
275 const char * talloc_pool_name(TALLOC_CTX const *t)
284 /** talloc and zero memory. */
285 #if defined(PARANOID_MALLOC_CHECKER)
286 void *talloc_zero_(TALLOC_CTX *t, size_t size)
288 void *talloc_zero(TALLOC_CTX *t, size_t size)
291 void *p = TALLOC(t, size);
294 memset(p, '\0', size);
299 #if defined(PARANOID_MALLOC_CHECKER)
300 void *talloc_zero_array_(TALLOC_CTX *t, size_t el_size, unsigned int count)
302 void *talloc_zero_array(TALLOC_CTX *t, size_t el_size, unsigned int count)
305 #if defined(PARANOID_MALLOC_CHECKER)
306 void *p = talloc_array_(t, el_size, count);
308 void *p = talloc_array(t, el_size, count);
312 memset(p, '\0', el_size*count);
317 /** memdup with a talloc. */
318 #if defined(PARANOID_MALLOC_CHECKER)
319 void *talloc_memdup_(TALLOC_CTX *t, const void *p, size_t size)
321 void *talloc_memdup(TALLOC_CTX *t, const void *p, size_t size)
324 void *newp = TALLOC(t,size);
327 memcpy(newp, p, size);
332 /** strdup with a talloc */
333 char *talloc_strdup(TALLOC_CTX *t, const char *p)
336 return TALLOC_MEMDUP(t, p, strlen(p) + 1);
341 /* strndup with a talloc */
342 char *talloc_strndup(TALLOC_CTX *mem_ctx, const char *str, size_t maxlen)
344 size_t len = strnlen(str, maxlen);
345 void *ret = TALLOC(mem_ctx, len+1);
348 memcpy(ret, str, len);
349 ((char *)ret)[len] = '\0';
354 /** strdup_upper with a talloc */
355 char *talloc_strdup_upper(TALLOC_CTX *t, const char *p)
359 char *q = strdup_upper(p);
361 r = talloc_strdup(t, q);
372 /** strdup_w with a talloc */
373 smb_ucs2_t *talloc_strdup_w(TALLOC_CTX *t, const smb_ucs2_t *p)
376 return TALLOC_MEMDUP(t, p, (strlen_w(p) + 1) * sizeof(smb_ucs2_t));
382 * Perform string formatting, and return a pointer to newly allocated
383 * memory holding the result, inside a memory pool.
385 char *talloc_asprintf(TALLOC_CTX *t, const char *fmt, ...)
391 ret = talloc_vasprintf(t, fmt, ap);
397 char *talloc_vasprintf(TALLOC_CTX *t, const char *fmt, va_list ap)
405 len = vsnprintf(NULL, 0, fmt, ap2);
407 ret = TALLOC(t, len+1);
410 vsnprintf(ret, len+1, fmt, ap2);
418 * Realloc @p s to append the formatted result of @p fmt and return @p
419 * s, which may have moved. Good for gradually accumulating output
420 * into a string buffer.
422 char *talloc_asprintf_append(TALLOC_CTX *t, char *s,
423 const char *fmt, ...)
428 s = talloc_vasprintf_append(t, s, fmt, ap);
436 * Realloc @p s to append the formatted result of @p fmt and @p ap,
437 * and return @p s, which may have moved. Good for gradually
438 * accumulating output into a string buffer.
440 char *talloc_vasprintf_append(TALLOC_CTX *t, char *s,
441 const char *fmt, va_list ap)
449 len = vsnprintf(NULL, 0, fmt, ap2);
451 s = TALLOC_REALLOC(t, s, s_len + len+1);
456 vsnprintf(s+s_len, len+1, fmt, ap2);
463 * Return a human-readable description of all talloc memory usage.
464 * The result is allocated from @p t.
466 char *talloc_describe_all(TALLOC_CTX *rt)
468 int n_pools = 0, total_chunks = 0;
469 size_t total_bytes = 0;
473 if (!rt) return NULL;
475 s = talloc_asprintf(rt, "global talloc allocations in pid: %u\n",
476 (unsigned) sys_getpid());
477 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
478 "name", "chunks", "bytes");
479 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
480 "----------------------------------------",
484 for (it = list_head; it; it = it->next_ctx) {
491 talloc_get_allocation(it, &bytes, &n_chunks);
494 fstrcpy(what, it->name);
496 slprintf(what, sizeof(what), "@%p", it);
498 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
502 total_bytes += bytes;
503 total_chunks += n_chunks;
506 s = talloc_asprintf_append(rt, s, "%-40s %8s %8s\n",
507 "----------------------------------------",
511 s = talloc_asprintf_append(rt, s, "%-40s %8u %8u\n",
513 (unsigned) total_chunks, (unsigned) total_bytes);
521 * Return an estimated memory usage for the specified pool. This does
522 * not include memory used by the underlying malloc implementation.
524 void talloc_get_allocation(TALLOC_CTX *t,
528 struct talloc_chunk *chunk;
534 for (chunk = t->list; chunk; chunk = chunk->next) {
536 *total_bytes += chunk->size;