#include <string.h>
#include <stdint.h>
#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
/* use this to force every realloc to change the pointer, to stress test
code that might not cope */
#define ALWAYS_REALLOC 0
-#define MAX_TALLOC_SIZE 0x10000000
+#define MAX_TALLOC_SIZE 0x7FFFFFFF
#define TALLOC_MAGIC 0xe814ec70
#define TALLOC_FLAG_FREE 0x01
#define TALLOC_FLAG_LOOP 0x02
+#define TALLOC_FLAG_EXT_ALLOC 0x04
#define TALLOC_MAGIC_REFERENCE ((const char *)1)
/* by default we abort when given a bad pointer (such as when talloc_free() is called
NULL
*/
static void *null_context;
-static void *autofree_context;
+static pid_t *autofree_context;
+
+static void *(*tc_malloc)(size_t size) = malloc;
+static void (*tc_free)(void *ptr) = free;
+static void *(*tc_realloc)(void *ptr, size_t size) = realloc;
+
+static void *(*tc_external_realloc)(const void *parent, void *ptr, size_t size);
+static void (*tc_lock)(const void *ctx);
+static void (*tc_unlock)(void);
struct talloc_reference_handle {
struct talloc_reference_handle *next, *prev;
if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
} while (0)
+static int locked;
+static inline void lock(const void *p)
+{
+ if (tc_lock && p) {
+ struct talloc_chunk *tc = talloc_chunk_from_ptr(p);
+
+ if (tc->flags & TALLOC_FLAG_EXT_ALLOC) {
+ if (locked)
+ TALLOC_ABORT("nested locking");
+ tc_lock(tc);
+ locked = 1;
+ }
+ }
+}
+
+static inline void unlock(void)
+{
+ if (locked) {
+ tc_unlock();
+ locked = 0;
+ }
+}
/*
return the parent chunk of a pointer
return tc->parent;
}
+/* This version doesn't do locking, so you must already have it. */
+static void *talloc_parent_nolock(const void *ptr)
+{
+ struct talloc_chunk *tc;
+
+ tc = talloc_parent_chunk(ptr);
+ return tc ? TC_PTR_FROM_CHUNK(tc) : NULL;
+}
+
void *talloc_parent(const void *ptr)
{
- struct talloc_chunk *tc = talloc_parent_chunk(ptr);
- return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
+ void *parent;
+
+ lock(ptr);
+ parent = talloc_parent_nolock(ptr);
+ unlock();
+ return parent;
}
/*
*/
const char *talloc_parent_name(const void *ptr)
{
- struct talloc_chunk *tc = talloc_parent_chunk(ptr);
+ struct talloc_chunk *tc;
+
+ lock(ptr);
+ tc = talloc_parent_chunk(ptr);
+ unlock();
+
return tc? tc->name : NULL;
}
-/*
- Allocate a bit of memory as a child of an existing pointer
-*/
-static inline void *__talloc(const void *context, size_t size)
+static void *init_talloc(struct talloc_chunk *parent,
+ struct talloc_chunk *tc,
+ size_t size, int external)
{
- struct talloc_chunk *tc;
-
- if (unlikely(context == NULL)) {
- context = null_context;
- }
-
- if (unlikely(size >= MAX_TALLOC_SIZE)) {
+ if (unlikely(tc == NULL))
return NULL;
- }
-
- tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
- if (unlikely(tc == NULL)) return NULL;
tc->size = size;
tc->flags = TALLOC_MAGIC;
+ if (external)
+ tc->flags |= TALLOC_FLAG_EXT_ALLOC;
tc->destructor = NULL;
tc->child = NULL;
tc->name = NULL;
tc->refs = NULL;
- if (likely(context)) {
- struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
-
+ if (likely(parent)) {
if (parent->child) {
parent->child->parent = NULL;
tc->next = parent->child;
return TC_PTR_FROM_CHUNK(tc);
}
+/*
+ Allocate a bit of memory as a child of an existing pointer
+*/
+static inline void *__talloc(const void *context, size_t size)
+{
+ struct talloc_chunk *tc;
+ struct talloc_chunk *parent = NULL;
+ int external = 0;
+
+ if (unlikely(context == NULL)) {
+ context = null_context;
+ }
+
+ if (unlikely(size >= MAX_TALLOC_SIZE)) {
+ return NULL;
+ }
+
+ if (likely(context)) {
+ parent = talloc_chunk_from_ptr(context);
+ if (unlikely(parent->flags & TALLOC_FLAG_EXT_ALLOC)) {
+ tc = tc_external_realloc(context, NULL,
+ TC_HDR_SIZE+size);
+ external = 1;
+ goto alloc_done;
+ }
+ }
+
+ tc = (struct talloc_chunk *)tc_malloc(TC_HDR_SIZE+size);
+alloc_done:
+ return init_talloc(parent, tc, size, external);
+}
+
/*
setup a destructor to be called on free of a pointer
the destructor should return 0 on success, or -1 on failure.
struct talloc_reference_handle *handle;
if (unlikely(ptr == NULL)) return NULL;
+ lock(context);
tc = talloc_chunk_from_ptr(ptr);
handle = (struct talloc_reference_handle *)_talloc_named_const(context,
sizeof(struct talloc_reference_handle),
TALLOC_MAGIC_REFERENCE);
- if (unlikely(handle == NULL)) return NULL;
+ if (unlikely(handle == NULL)) {
+ unlock();
+ return NULL;
+ }
/* note that we hang the destructor off the handle, not the
main context as that allows the caller to still setup their
talloc_set_destructor(handle, talloc_reference_destructor);
handle->ptr = discard_const_p(void, ptr);
_TLIST_ADD(tc->refs, handle);
+ unlock();
return handle->ptr;
}
+/*
+ return 1 if ptr is a parent of context
+*/
+static int _talloc_is_parent(const void *context, const void *ptr)
+{
+ struct talloc_chunk *tc;
+
+ if (context == NULL) {
+ return 0;
+ }
+
+ tc = talloc_chunk_from_ptr(context);
+ while (tc) {
+ if (TC_PTR_FROM_CHUNK(tc) == ptr) {
+ return 1;
+ }
+ while (tc && tc->prev) tc = tc->prev;
+ if (tc) {
+ tc = tc->parent;
+ }
+ }
+ return 0;
+}
+
+/*
+ move a lump of memory from one talloc context to another return the
+ ptr on success, or NULL if it could not be transferred.
+ passing NULL as ptr will always return NULL with no side effects.
+*/
+static void *__talloc_steal(const void *new_ctx, const void *ptr)
+{
+ struct talloc_chunk *tc, *new_tc;
+
+ if (unlikely(!ptr)) {
+ return NULL;
+ }
+
+ if (unlikely(new_ctx == NULL)) {
+ new_ctx = null_context;
+ }
+
+ tc = talloc_chunk_from_ptr(ptr);
+
+ if (unlikely(new_ctx == NULL)) {
+ if (tc->parent) {
+ _TLIST_REMOVE(tc->parent->child, tc);
+ if (tc->parent->child) {
+ tc->parent->child->parent = tc->parent;
+ }
+ } else {
+ if (tc->prev) tc->prev->next = tc->next;
+ if (tc->next) tc->next->prev = tc->prev;
+ }
+
+ tc->parent = tc->next = tc->prev = NULL;
+ return discard_const_p(void, ptr);
+ }
+
+ new_tc = talloc_chunk_from_ptr(new_ctx);
+
+ if (unlikely(tc == new_tc || tc->parent == new_tc)) {
+ return discard_const_p(void, ptr);
+ }
+
+ if (tc->parent) {
+ _TLIST_REMOVE(tc->parent->child, tc);
+ if (tc->parent->child) {
+ tc->parent->child->parent = tc->parent;
+ }
+ } else {
+ if (tc->prev) tc->prev->next = tc->next;
+ if (tc->next) tc->next->prev = tc->prev;
+ }
+
+ tc->parent = new_tc;
+ if (new_tc->child) new_tc->child->parent = NULL;
+ _TLIST_ADD(new_tc->child, tc);
+
+ return discard_const_p(void, ptr);
+}
/*
internal talloc_free call
*/
-static inline int _talloc_free(void *ptr)
+static inline int _talloc_free(const void *ptr)
{
struct talloc_chunk *tc;
+ void *oldparent = NULL;
if (unlikely(ptr == NULL)) {
return -1;
* call another instance of talloc_free() on the current
* pointer.
*/
- is_child = talloc_is_parent(tc->refs, ptr);
+ is_child = _talloc_is_parent(tc->refs, ptr);
_talloc_free(tc->refs);
if (is_child) {
return _talloc_free(ptr);
return -1;
}
tc->destructor = (talloc_destructor_t)-1;
- if (d(ptr) == -1) {
+ if (d(discard_const_p(void, ptr)) == -1) {
tc->destructor = d;
return -1;
}
tc->destructor = NULL;
}
+ if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
+ oldparent = talloc_parent_nolock(ptr);
+
if (tc->parent) {
_TLIST_REMOVE(tc->parent->child, tc);
if (tc->parent->child) {
final choice is the null context. */
void *child = TC_PTR_FROM_CHUNK(tc->child);
const void *new_parent = null_context;
+ struct talloc_chunk *old_parent = NULL;
if (unlikely(tc->child->refs)) {
struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
if (p) new_parent = TC_PTR_FROM_CHUNK(p);
}
+ /* finding the parent here is potentially quite
+ expensive, but the alternative, which is to change
+ talloc to always have a valid tc->parent pointer,
+ makes realloc more expensive where there are a
+ large number of children.
+
+ The reason we need the parent pointer here is that
+ if _talloc_free_internal() fails due to references
+ or a failing destructor we need to re-parent, but
+ the free call can invalidate the prev pointer.
+ */
+ if (new_parent == null_context && (tc->child->refs || tc->child->destructor)) {
+ old_parent = talloc_parent_chunk(ptr);
+ }
if (unlikely(_talloc_free(child) == -1)) {
if (new_parent == null_context) {
- struct talloc_chunk *p = talloc_parent_chunk(ptr);
+ struct talloc_chunk *p = old_parent;
if (p) new_parent = TC_PTR_FROM_CHUNK(p);
}
- talloc_steal(new_parent, child);
+ __talloc_steal(new_parent, child);
}
}
tc->flags |= TALLOC_FLAG_FREE;
- free(tc);
+
+ if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC))
+ tc_external_realloc(oldparent, tc, 0);
+ else
+ tc_free(tc);
+
return 0;
}
-/*
- move a lump of memory from one talloc context to another return the
- ptr on success, or NULL if it could not be transferred.
- passing NULL as ptr will always return NULL with no side effects.
-*/
void *_talloc_steal(const void *new_ctx, const void *ptr)
{
- struct talloc_chunk *tc, *new_tc;
+ void *p;
- if (unlikely(!ptr)) {
- return NULL;
- }
-
- if (unlikely(new_ctx == NULL)) {
- new_ctx = null_context;
- }
-
- tc = talloc_chunk_from_ptr(ptr);
-
- if (unlikely(new_ctx == NULL)) {
- if (tc->parent) {
- _TLIST_REMOVE(tc->parent->child, tc);
- if (tc->parent->child) {
- tc->parent->child->parent = tc->parent;
- }
- } else {
- if (tc->prev) tc->prev->next = tc->next;
- if (tc->next) tc->next->prev = tc->prev;
- }
-
- tc->parent = tc->next = tc->prev = NULL;
- return discard_const_p(void, ptr);
- }
-
- new_tc = talloc_chunk_from_ptr(new_ctx);
-
- if (unlikely(tc == new_tc || tc->parent == new_tc)) {
- return discard_const_p(void, ptr);
- }
-
- if (tc->parent) {
- _TLIST_REMOVE(tc->parent->child, tc);
- if (tc->parent->child) {
- tc->parent->child->parent = tc->parent;
- }
- } else {
- if (tc->prev) tc->prev->next = tc->next;
- if (tc->next) tc->next->prev = tc->prev;
- }
-
- tc->parent = new_tc;
- if (new_tc->child) new_tc->child->parent = NULL;
- _TLIST_ADD(new_tc->child, tc);
-
- return discard_const_p(void, ptr);
+ lock(new_ctx);
+ p = __talloc_steal(new_ctx, ptr);
+ unlock();
+ return p;
}
-
-
/*
remove a secondary reference to a pointer. This undo's what
talloc_reference() has done. The context and pointer arguments
context = null_context;
}
+ lock(context);
if (talloc_unreference(context, ptr) == 0) {
+ unlock();
return 0;
}
if (context == NULL) {
if (talloc_parent_chunk(ptr) != NULL) {
+ unlock();
return -1;
}
} else {
if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
+ unlock();
return -1;
}
}
tc_p = talloc_chunk_from_ptr(ptr);
if (tc_p->refs == NULL) {
- return _talloc_free(ptr);
+ int ret = _talloc_free(ptr);
+ unlock();
+ return ret;
}
new_p = talloc_parent_chunk(tc_p->refs);
}
if (talloc_unreference(new_parent, ptr) != 0) {
+ unlock();
return -1;
}
- talloc_steal(new_parent, ptr);
+ __talloc_steal(new_parent, ptr);
+ unlock();
return 0;
}
/*
add a name to an existing pointer - va_list version
*/
-static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
+static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_FMT(2,0);
static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
{
void *ptr;
const char *name;
+ lock(context);
ptr = __talloc(context, size);
+ unlock();
if (unlikely(ptr == NULL)) return NULL;
va_start(ap, fmt);
va_end(ap);
if (unlikely(name == NULL)) {
- _talloc_free(ptr);
+ talloc_free(ptr);
return NULL;
}
va_end(ap);
if (unlikely(name == NULL)) {
- _talloc_free(ptr);
+ talloc_free(ptr);
return NULL;
}
return ptr;
}
-/*
- this is a replacement for the Samba3 talloc_destroy_pool functionality. It
- should probably not be used in new code. It's in here to keep the talloc
- code consistent across Samba 3 and 4.
-*/
-void talloc_free_children(void *ptr)
-{
- struct talloc_chunk *tc;
-
- if (unlikely(ptr == NULL)) {
- return;
- }
-
- tc = talloc_chunk_from_ptr(ptr);
-
- while (tc->child) {
- /* we need to work out who will own an abandoned child
- if it cannot be freed. In priority order, the first
- choice is owner of any remaining reference to this
- pointer, the second choice is our parent, and the
- final choice is the null context. */
- void *child = TC_PTR_FROM_CHUNK(tc->child);
- const void *new_parent = null_context;
- if (unlikely(tc->child->refs)) {
- struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
- if (p) new_parent = TC_PTR_FROM_CHUNK(p);
- }
- if (unlikely(_talloc_free(child) == -1)) {
- if (new_parent == null_context) {
- struct talloc_chunk *p = talloc_parent_chunk(ptr);
- if (p) new_parent = TC_PTR_FROM_CHUNK(p);
- }
- talloc_steal(new_parent, child);
- }
- }
-}
-
/*
Allocate a bit of memory as a child of an existing pointer
*/
return __talloc(context, size);
}
+static int talloc_destroy_pointer(void ***pptr)
+{
+ if ((uintptr_t)**pptr < getpagesize())
+ TALLOC_ABORT("Double free or invalid talloc_set?");
+ /* Invalidate pointer so it can't be used again. */
+ **pptr = (void *)1;
+ return 0;
+}
+
+void _talloc_set(void *ptr, const void *ctx, size_t size, const char *name)
+{
+ void ***child;
+ void *p;
+
+ p = talloc_named_const(ctx, size, name);
+ if (unlikely(!p))
+ goto set_ptr;
+
+ child = talloc(p, void **);
+ if (unlikely(!child)) {
+ talloc_free(p);
+ p = NULL;
+ goto set_ptr;
+ }
+ *child = ptr;
+ talloc_set_name_const(child, "talloc_set destructor");
+ talloc_set_destructor(child, talloc_destroy_pointer);
+
+set_ptr:
+ /* memcpy rather than cast avoids aliasing problems. */
+ memcpy(ptr, &p, sizeof(p));
+}
+
/*
externally callable talloc_set_name_const()
*/
*/
void *talloc_named_const(const void *context, size_t size, const char *name)
{
- return _talloc_named_const(context, size, name);
+ void *p;
+ lock(context);
+ p = _talloc_named_const(context, size, name);
+ unlock();
+ return p;
}
/*
will not be freed if the ref_count is > 1 or the destructor (if
any) returns non-zero
*/
-int talloc_free(void *ptr)
+int talloc_free(const void *ptr)
{
int saved_errno = errno, ret;
- ret = _talloc_free(ptr);
+
+ lock(ptr);
+ ret = _talloc_free(discard_const_p(void, ptr));
+ unlock();
if (ret == 0)
errno = saved_errno;
return ret;
/* size zero is equivalent to free() */
if (unlikely(size == 0)) {
- _talloc_free(ptr);
+ talloc_free(ptr);
return NULL;
}
/* realloc(NULL) is equivalent to malloc() */
if (ptr == NULL) {
- return _talloc_named_const(context, size, name);
+ return talloc_named_const(context, size, name);
}
tc = talloc_chunk_from_ptr(ptr);
return NULL;
}
- /* by resetting magic we catch users of the old memory */
- tc->flags |= TALLOC_FLAG_FREE;
+ lock(ptr);
+ if (unlikely(tc->flags & TALLOC_FLAG_EXT_ALLOC)) {
+ /* need to get parent before setting free flag. */
+ void *parent = talloc_parent_nolock(ptr);
+ tc->flags |= TALLOC_FLAG_FREE;
+ new_ptr = tc_external_realloc(parent, tc, size + TC_HDR_SIZE);
+ } else {
+ /* by resetting magic we catch users of the old memory */
+ tc->flags |= TALLOC_FLAG_FREE;
#if ALWAYS_REALLOC
- new_ptr = malloc(size + TC_HDR_SIZE);
- if (new_ptr) {
- memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
- free(tc);
- }
+ new_ptr = tc_malloc(size + TC_HDR_SIZE);
+ if (new_ptr) {
+ memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
+ tc_free(tc);
+ }
#else
- new_ptr = realloc(tc, size + TC_HDR_SIZE);
+ new_ptr = tc_realloc(tc, size + TC_HDR_SIZE);
#endif
+ }
+
if (unlikely(!new_ptr)) {
tc->flags &= ~TALLOC_FLAG_FREE;
+ unlock();
return NULL;
}
tc->size = size;
_talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
+ unlock();
return TC_PTR_FROM_CHUNK(tc);
}
/*
return the total size of a talloc pool (subtree)
*/
-size_t talloc_total_size(const void *ptr)
+static size_t _talloc_total_size(const void *ptr)
{
size_t total = 0;
struct talloc_chunk *c, *tc;
- if (ptr == NULL) {
- ptr = null_context;
- }
- if (ptr == NULL) {
- return 0;
- }
-
tc = talloc_chunk_from_ptr(ptr);
if (tc->flags & TALLOC_FLAG_LOOP) {
total = tc->size;
for (c=tc->child;c;c=c->next) {
- total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
+ total += _talloc_total_size(TC_PTR_FROM_CHUNK(c));
}
tc->flags &= ~TALLOC_FLAG_LOOP;
return total;
}
+size_t talloc_total_size(const void *ptr)
+{
+ size_t total;
+
+ if (ptr == NULL) {
+ ptr = null_context;
+ }
+ if (ptr == NULL) {
+ return 0;
+ }
+
+ lock(ptr);
+ total = _talloc_total_size(ptr);
+ unlock();
+ return total;
+}
+
/*
return the total number of blocks in a talloc pool (subtree)
*/
-size_t talloc_total_blocks(const void *ptr)
+static size_t _talloc_total_blocks(const void *ptr)
{
size_t total = 0;
struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
total++;
for (c=tc->child;c;c=c->next) {
- total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
+ total += _talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
}
tc->flags &= ~TALLOC_FLAG_LOOP;
return total;
}
-/*
- return the number of external references to a pointer
-*/
-size_t talloc_reference_count(const void *ptr)
+size_t talloc_total_blocks(const void *ptr)
+{
+ size_t total;
+
+ lock(ptr);
+ total = _talloc_total_blocks(ptr);
+ unlock();
+
+ return total;
+}
+
+static size_t _talloc_reference_count(const void *ptr)
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
struct talloc_reference_handle *h;
return ret;
}
+/*
+ return the number of external references to a pointer
+*/
+size_t talloc_reference_count(const void *ptr)
+{
+ size_t ret;
+
+ lock(talloc_chunk_from_ptr(ptr));
+ ret = _talloc_reference_count(ptr);
+ unlock();
+ return ret;
+}
+
/*
report on memory usage by all children of a pointer, giving a full tree view
*/
-void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
+static void _talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
void (*callback)(const void *ptr,
int depth, int max_depth,
int is_ref,
{
struct talloc_chunk *c, *tc;
- if (ptr == NULL) {
- ptr = null_context;
- }
- if (ptr == NULL) return;
-
tc = talloc_chunk_from_ptr(ptr);
if (tc->flags & TALLOC_FLAG_LOOP) {
struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
callback(h->ptr, depth + 1, max_depth, 1, private_data);
} else {
- talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
+ _talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
}
}
tc->flags &= ~TALLOC_FLAG_LOOP;
}
+void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
+ void (*callback)(const void *ptr,
+ int depth, int max_depth,
+ int is_ref,
+ void *private_data),
+ void *private_data)
+{
+ if (ptr == NULL) {
+ ptr = null_context;
+ }
+ if (ptr == NULL) return;
+
+ lock(ptr);
+ _talloc_report_depth_cb(ptr, depth, max_depth, callback, private_data);
+ unlock();
+}
+
static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
{
const char *name = talloc_get_name(ptr);
if (depth == 0) {
fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
(max_depth < 0 ? "full " :""), name,
- (unsigned long)talloc_total_size(ptr),
- (unsigned long)talloc_total_blocks(ptr));
+ (unsigned long)_talloc_total_size(ptr),
+ (unsigned long)_talloc_total_blocks(ptr));
return;
}
fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
depth*4, "",
name,
- (unsigned long)talloc_total_size(ptr),
- (unsigned long)talloc_total_blocks(ptr),
- (int)talloc_reference_count(ptr), ptr);
+ (unsigned long)_talloc_total_size(ptr),
+ (unsigned long)_talloc_total_blocks(ptr),
+ (int)_talloc_reference_count(ptr), ptr);
#if 0
fprintf(f, "content: ");
*/
void *_talloc_zero(const void *ctx, size_t size, const char *name)
{
- void *p = _talloc_named_const(ctx, size, name);
+ void *p;
+
+ lock(ctx);
+ p = _talloc_named_const(ctx, size, name);
+ unlock();
if (p) {
memset(p, '\0', size);
*/
void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
{
- void *newp = _talloc_named_const(t, size, name);
+ void *newp;
+
+ lock(t);
+ newp = _talloc_named_const(t, size, name);
+ unlock();
if (likely(newp)) {
memcpy(newp, p, size);
for (len=0; len<n && p[len]; len++) ;
+ lock(t);
ret = (char *)__talloc(t, len + 1);
+ unlock();
if (!ret) { return NULL; }
memcpy(ret, p, len);
ret[len] = 0;
return NULL;
}
+ lock(t);
ret = (char *)__talloc(t, len+1);
+ unlock();
if (ret) {
va_copy(ap2, ap);
vsnprintf(ret, len+1, fmt, ap2);
*/
void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
{
+ void *p;
+
if (count >= MAX_TALLOC_SIZE/el_size) {
return NULL;
}
- return _talloc_named_const(ctx, el_size * count, name);
+ lock(ctx);
+ p = _talloc_named_const(ctx, el_size * count, name);
+ unlock();
+ return p;
}
/*
*/
void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
{
+ void *p;
+
if (count >= MAX_TALLOC_SIZE/el_size) {
return NULL;
}
- return _talloc_zero(ctx, el_size * count, name);
+ p = _talloc_zero(ctx, el_size * count, name);
+ return p;
}
/*
static void talloc_autofree(void)
{
- _talloc_free(autofree_context);
+ if (autofree_context && *autofree_context == getpid())
+ talloc_free(autofree_context);
}
/*
*/
void *talloc_autofree_context(void)
{
- if (autofree_context == NULL) {
- autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
+ if (autofree_context == NULL || *autofree_context != getpid()) {
+ autofree_context = talloc(NULL, pid_t);
+ *autofree_context = getpid();
+ talloc_set_name_const(autofree_context, "autofree_context");
+
talloc_set_destructor(autofree_context, talloc_autofree_destructor);
atexit(talloc_autofree);
}
return NULL;
}
+ lock(context);
tc = talloc_chunk_from_ptr(context);
while (tc) {
if (tc->name && strcmp(tc->name, name) == 0) {
+ unlock();
return TC_PTR_FROM_CHUNK(tc);
}
while (tc && tc->prev) tc = tc->prev;
tc = tc->parent;
}
}
+ unlock();
return NULL;
}
return;
}
+ lock(context);
tc = talloc_chunk_from_ptr(context);
fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
while (tc) {
tc = tc->parent;
}
}
+ unlock();
fflush(file);
}
-/*
- return 1 if ptr is a parent of context
-*/
int talloc_is_parent(const void *context, const void *ptr)
{
- struct talloc_chunk *tc;
+ int ret;
+ lock(context);
+ ret = _talloc_is_parent(context, ptr);
+ unlock();
+ return ret;
+}
- if (context == NULL) {
- return 0;
- }
+void talloc_set_allocator(void *(*malloc)(size_t size),
+ void (*free)(void *ptr),
+ void *(*realloc)(void *ptr, size_t size))
+{
+ tc_malloc = malloc;
+ tc_free = free;
+ tc_realloc = realloc;
+}
- tc = talloc_chunk_from_ptr(context);
- while (tc) {
- if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
- while (tc && tc->prev) tc = tc->prev;
- if (tc) {
- tc = tc->parent;
- }
- }
- return 0;
+void *talloc_add_external(const void *ctx,
+ void *(*realloc)(const void *, void *, size_t),
+ void (*lock)(const void *p),
+ void (*unlock)(void))
+{
+ struct talloc_chunk *tc, *parent;
+ void *p;
+
+ if (tc_external_realloc && tc_external_realloc != realloc)
+ TALLOC_ABORT("talloc_add_external realloc replaced");
+ tc_external_realloc = realloc;
+
+ if (unlikely(ctx == NULL)) {
+ ctx = null_context;
+ parent = NULL;
+ } else
+ parent = talloc_chunk_from_ptr(ctx);
+
+ tc = tc_external_realloc(ctx, NULL, TC_HDR_SIZE);
+ p = init_talloc(parent, tc, 0, 1);
+ tc_lock = lock;
+ tc_unlock = unlock;
+
+ return p;
}