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.
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.
64 static TALLOC_CTX
*list_head
= NULL
;
68 * Add to the global list
70 static void talloc_enroll(TALLOC_CTX
*t
)
72 t
->next_ctx
= list_head
;
77 static void talloc_disenroll(TALLOC_CTX
*t
)
81 /* Use a double-* so that no special case is required for the
83 for (ttmp
= &list_head
; *ttmp
; ttmp
= &((*ttmp
)->next_ctx
))
85 /* ttmp is the link that points to t, either
86 * list_head or the next_ctx link in its
89 t
->next_ctx
= NULL
; /* clobber */
92 abort(); /* oops, this talloc was already
93 * clobbered or something else went
98 /** Create a new talloc context. **/
99 static TALLOC_CTX
*talloc_init_internal(void)
103 t
= (TALLOC_CTX
*)malloc(sizeof(TALLOC_CTX
));
106 t
->total_alloc_size
= 0;
117 * Create a new talloc context, with a name specifying its purpose.
120 TALLOC_CTX
*talloc_init(char const *fmt
, ...)
125 t
= talloc_init_internal();
128 * t->name must not be talloced.
129 * as destroying the pool would destroy it. JRA.
133 vasprintf(&t
->name
, fmt
, ap
);
145 /** Allocate a bit of memory from the specified pool **/
146 void *talloc(TALLOC_CTX
*t
, size_t size
)
149 struct talloc_chunk
*tc
;
151 if (!t
|| size
== 0) return NULL
;
155 tc
= malloc(sizeof(*tc
));
161 t
->total_alloc_size
+= size
;
170 /** A talloc version of realloc */
171 void *talloc_realloc(TALLOC_CTX
*t
, void *ptr
, size_t size
)
173 struct talloc_chunk
*tc
;
176 /* size zero is equivalent to free() */
180 /* realloc(NULL) is equavalent to malloc() */
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
);
188 t
->total_alloc_size
+= (size
- tc
->size
);
198 /** Destroy all the memory allocated inside @p t, but not @p t
200 void talloc_destroy_pool(TALLOC_CTX
*t
)
202 struct talloc_chunk
*c
;
209 SAFE_FREE(t
->list
->ptr
);
214 t
->total_alloc_size
= 0;
217 /** Destroy a whole pool including the context */
218 void talloc_destroy(TALLOC_CTX
*t
)
223 talloc_destroy_pool(t
);
226 memset(t
, 0, sizeof(TALLOC_CTX
));
230 /** Return the current total size of the pool. */
231 size_t talloc_pool_size(TALLOC_CTX
*t
)
234 return t
->total_alloc_size
;
239 const char * talloc_pool_name(TALLOC_CTX
const *t
)
248 /** talloc and zero memory. */
249 void *talloc_zero(TALLOC_CTX
*t
, size_t size
)
251 void *p
= talloc(t
, size
);
254 memset(p
, '\0', size
);
259 /** memdup with a talloc. */
260 void *talloc_memdup(TALLOC_CTX
*t
, const void *p
, size_t size
)
262 void *newp
= talloc(t
,size
);
265 memcpy(newp
, p
, size
);
270 /** strdup with a talloc */
271 char *talloc_strdup(TALLOC_CTX
*t
, const char *p
)
274 return talloc_memdup(t
, p
, strlen(p
) + 1);
279 /** strdup_upper with a talloc */
280 char *talloc_strdup_upper(TALLOC_CTX
*t
, const char *p
)
284 char *q
= strdup_upper(p
);
286 r
= talloc_strdup(t
, q
);
297 /** strdup_w with a talloc */
298 smb_ucs2_t
*talloc_strdup_w(TALLOC_CTX
*t
, const smb_ucs2_t
*p
)
301 return talloc_memdup(t
, p
, (strlen_w(p
) + 1) * sizeof(smb_ucs2_t
));
307 * Perform string formatting, and return a pointer to newly allocated
308 * memory holding the result, inside a memory pool.
310 char *talloc_asprintf(TALLOC_CTX
*t
, const char *fmt
, ...)
316 ret
= talloc_vasprintf(t
, fmt
, ap
);
322 char *talloc_vasprintf(TALLOC_CTX
*t
, const char *fmt
, va_list ap
)
330 len
= vsnprintf(NULL
, 0, fmt
, ap2
);
332 ret
= talloc(t
, len
+1);
335 vsnprintf(ret
, len
+1, fmt
, ap2
);
343 * Realloc @p s to append the formatted result of @p fmt and return @p
344 * s, which may have moved. Good for gradually accumulating output
345 * into a string buffer.
347 char *talloc_asprintf_append(TALLOC_CTX
*t
, char *s
,
348 const char *fmt
, ...)
353 s
= talloc_vasprintf_append(t
, s
, fmt
, ap
);
361 * Realloc @p s to append the formatted result of @p fmt and @p ap,
362 * and return @p s, which may have moved. Good for gradually
363 * accumulating output into a string buffer.
365 char *talloc_vasprintf_append(TALLOC_CTX
*t
, char *s
,
366 const char *fmt
, va_list ap
)
374 len
= vsnprintf(NULL
, 0, fmt
, ap2
);
376 s
= talloc_realloc(t
, s
, s_len
+ len
+1);
381 vsnprintf(s
+s_len
, len
+1, fmt
, ap2
);
388 * Return a human-readable description of all talloc memory usage.
389 * The result is allocated from @p t.
391 char *talloc_describe_all(TALLOC_CTX
*rt
)
393 int n_pools
= 0, total_chunks
= 0;
394 size_t total_bytes
= 0;
398 if (!rt
) return NULL
;
400 s
= talloc_asprintf(rt
, "global talloc allocations in pid: %u\n",
401 (unsigned) sys_getpid());
402 s
= talloc_asprintf_append(rt
, s
, "%-40s %8s %8s\n",
403 "name", "chunks", "bytes");
404 s
= talloc_asprintf_append(rt
, s
, "%-40s %8s %8s\n",
405 "----------------------------------------",
409 for (it
= list_head
; it
; it
= it
->next_ctx
) {
416 talloc_get_allocation(it
, &bytes
, &n_chunks
);
419 fstrcpy(what
, it
->name
);
421 slprintf(what
, sizeof(what
), "@%p", it
);
423 s
= talloc_asprintf_append(rt
, s
, "%-40s %8u %8u\n",
427 total_bytes
+= bytes
;
428 total_chunks
+= n_chunks
;
431 s
= talloc_asprintf_append(rt
, s
, "%-40s %8s %8s\n",
432 "----------------------------------------",
436 s
= talloc_asprintf_append(rt
, s
, "%-40s %8u %8u\n",
438 (unsigned) total_chunks
, (unsigned) total_bytes
);
446 * Return an estimated memory usage for the specified pool. This does
447 * not include memory used by the underlying malloc implementation.
449 void talloc_get_allocation(TALLOC_CTX
*t
,
453 struct talloc_chunk
*chunk
;
459 for (chunk
= t
->list
; chunk
; chunk
= chunk
->next
) {
461 *total_bytes
+= chunk
->size
;