-/*
+/*
Samba Unix SMB/CIFS implementation.
Samba trivial allocation library - new interface
Copyright (C) Andrew Tridgell 2004
Copyright (C) Stefan Metzmacher 2006
-
+
** NOTE! The following LGPL license applies to the talloc
** library. This does NOT imply that all of Samba is released
** under the LGPL
-
+
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
#define TALLOC_MAGIC_REFERENCE ((const char *)1)
-/* by default we abort when given a bad pointer (such as when talloc_free() is called
+/* by default we abort when given a bad pointer (such as when talloc_free() is called
on a pointer that came from malloc() */
#ifndef TALLOC_ABORT
#define TALLOC_ABORT(reason) abort()
TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
} while (0)
-#define TALLOC_MEMLIMIT_CHECK(limit, size) do { \
- struct talloc_memlimit *l; \
- for (l = limit; l != NULL; l = l->upper) { \
- if (l->max_size != 0 && \
- ((l->max_size <= l->cur_size) || \
- (l->max_size - l->cur_size < TC_HDR_SIZE+size))) { \
- errno = ENOMEM; \
- return NULL; \
- } \
- } \
-} while(0)
-
-#define TALLOC_MEMLIMIT_UPDATE(limit, o_size, n_size) do { \
- struct talloc_memlimit *l; \
- ssize_t d; \
- if (o_size == 0) { \
- d = n_size + TC_HDR_SIZE; \
- } else { \
- d = n_size - o_size; \
- } \
- for (l = limit; l != NULL; l = l->upper) { \
- ssize_t new_size = l->cur_size + d; \
- if (new_size < 0) { \
- talloc_abort("cur_size memlimit counter not correct!"); \
- errno = EINVAL; \
- return NULL; \
- } \
- l->cur_size = new_size; \
- } \
-} while(0)
-
struct talloc_reference_handle {
struct talloc_reference_handle *next, *prev;
void *ptr;
size_t cur_size;
};
+static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
+static bool talloc_memlimit_update(struct talloc_memlimit *limit,
+ size_t old_size, size_t new_size);
+static void talloc_memlimit_grow(struct talloc_memlimit *limit,
+ size_t size);
+static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
+ size_t size);
+static void talloc_memlimit_update_on_free(struct talloc_chunk *tc);
+
typedef int (*talloc_destructor_t)(void *);
struct talloc_chunk {
* if 'limit' is set it means all *new* children of the context will
* be limited to a total aggregate size ox max_size for memory
* allocations.
- * cur_size is used to kep track of the current use
+ * cur_size is used to keep track of the current use
*/
struct talloc_memlimit *limit;
{
const char *pp = (const char *)ptr;
struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
- if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
+ if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
talloc_abort_magic(tc->flags & (~0xF));
return NULL;
return result;
}
-/*
+/*
Allocate a bit of memory as a child of an existing pointer
*/
static inline void *__talloc(const void *context, size_t size)
limit = ptc->limit;
}
- TALLOC_MEMLIMIT_CHECK(limit, (TC_HDR_SIZE+size));
+ if (!talloc_memlimit_check(limit, (TC_HDR_SIZE+size))) {
+ errno = ENOMEM;
+ return NULL;
+ }
tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size);
}
}
/*
- increase the reference count on a piece of memory.
+ increase the reference count on a piece of memory.
*/
_PUBLIC_ int talloc_increase_ref_count(const void *ptr)
{
}
/*
- more efficient way to add a name to a pointer - the name must point to a
+ more efficient way to add a name to a pointer - the name must point to a
true string constant
*/
static inline void _talloc_set_name_const(const void *ptr, const char *name)
make a secondary reference to a pointer, hanging off the given context.
the pointer remains valid until both the original caller and this given
context are freed.
-
- the major use for this is when two different structures need to reference the
+
+ the major use for this is when two different structures need to reference the
same underlying data, and you want to be able to free the two instances separately,
and in either order
*/
*/
pool->hdr.c.pool = tc_pool_first_chunk(pool);
tc_invalidate_pool(pool);
- } else if (unlikely(pool->hdr.object_count == 0)) {
+ return;
+ }
+
+ if (unlikely(pool->hdr.object_count == 0)) {
/*
* we mark the freed memory with where we called the free
* from. This means on a double free error we can report where
TC_INVALIDATE_FULL_CHUNK(&pool->hdr.c);
free(pool);
- } else if (pool->hdr.c.pool == next_tc) {
+ return;
+ }
+
+ if (pool->hdr.c.pool == next_tc) {
/*
* if pool->pool still points to end of
* 'tc' (which is stored in the 'next_tc' variable),
* we can reclaim the memory of 'tc'.
*/
pool->hdr.c.pool = tc;
+ return;
}
+
+ /*
+ * Do nothing. The memory is just "wasted", waiting for the pool
+ * itself to be freed.
+ */
}
static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
void *ptr,
const char *location);
-/*
+/*
internal talloc_free call
*/
static inline int _talloc_free_internal(void *ptr, const char *location)
/* we mark the freed memory with where we called the free
* from. This means on a double free error we can report where
- * the first free came from
- */
+ * the first free came from
+ */
tc->name = location;
if (tc->flags & TALLOC_FLAG_POOL) {
}
pool->hdr.object_count--;
- if (unlikely(pool->hdr.object_count == 0)) {
- TC_INVALIDATE_FULL_CHUNK(tc);
- free(tc);
+
+ if (likely(pool->hdr.object_count != 0)) {
+ return 0;
}
- } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
- _talloc_free_poolmem(tc, location);
- } else {
+
TC_INVALIDATE_FULL_CHUNK(tc);
free(tc);
+ return 0;
}
+
+ if (tc->flags & TALLOC_FLAG_POOLMEM) {
+ _talloc_free_poolmem(tc, location);
+ return 0;
+ }
+
+ TC_INVALIDATE_FULL_CHUNK(tc);
+ free(tc);
return 0;
}
struct talloc_memlimit *old_limit,
struct talloc_memlimit *new_limit);
-/*
+/*
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.
ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
- TALLOC_MEMLIMIT_UPDATE(tc->limit->upper, ctx_size, 0);
+ if (!talloc_memlimit_update(tc->limit->upper, ctx_size, 0)) {
+ talloc_abort("cur_size memlimit counter not correct!");
+ errno = EINVAL;
+ return NULL;
+ }
if (tc->limit->parent == tc) {
tc->limit->upper = NULL;
return discard_const_p(void, ptr);
}
-/*
+/*
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.
if (unlikely(ptr == NULL)) {
return NULL;
}
-
+
tc = talloc_chunk_from_ptr(ptr);
-
+
if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
struct talloc_reference_handle *h;
talloc_log("WARNING: stealing into talloc child at %s\n", location);
}
#endif
-
+
return _talloc_steal_internal(new_ctx, ptr);
}
-/*
+/*
this is like a talloc_steal(), but you must supply the old
parent. This resolves the ambiguity in a talloc_steal() which is
called on a context that has more than one parent (via references)
}
return discard_const_p(void, ptr);
}
- }
+ }
/* it wasn't a parent */
return NULL;
}
}
-/*
+/*
Allocate a bit of memory as a child of an existing pointer
*/
_PUBLIC_ void *_talloc(const void *context, size_t size)
return _talloc_named_const(context, size, name);
}
-/*
- free a talloc pointer. This also frees all child pointers of this
+/*
+ free a talloc pointer. This also frees all child pointers of this
pointer recursively
return 0 if the memory is actually freed, otherwise -1. The memory
if (unlikely(ptr == NULL)) {
return -1;
}
-
+
tc = talloc_chunk_from_ptr(ptr);
-
+
if (unlikely(tc->refs != NULL)) {
struct talloc_reference_handle *h;
}
return -1;
}
-
+
return _talloc_free_internal(ptr, location);
}
}
if (tc->limit && (size - tc->size > 0)) {
- TALLOC_MEMLIMIT_CHECK(tc->limit, (size - tc->size));
+ if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
+ errno = ENOMEM;
+ return NULL;
+ }
}
/* handle realloc inside a talloc_pool */
if (new_chunk_size == old_chunk_size) {
TC_UNDEFINE_GROW_CHUNK(tc, size);
tc->flags &= ~TALLOC_FLAG_FREE;
- TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+ if (!talloc_memlimit_update(tc->limit,
+ tc->size, size)) {
+ talloc_abort("cur_size memlimit counter not"
+ " correct!");
+ errno = EINVAL;
+ return NULL;
+ }
+
tc->size = size;
return ptr;
}
if (space_left >= space_needed) {
TC_UNDEFINE_GROW_CHUNK(tc, size);
tc->flags &= ~TALLOC_FLAG_FREE;
- TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+ if (!talloc_memlimit_update(tc->limit,
+ tc->size, size)) {
+ talloc_abort("cur_size memlimit "
+ "counter not correct!");
+ errno = EINVAL;
+ return NULL;
+ }
tc->size = size;
pool_tc->hdr.c.pool = tc_next_chunk(tc);
return ptr;
}
got_new_ptr:
#endif
- if (unlikely(!new_ptr)) {
- tc->flags &= ~TALLOC_FLAG_FREE;
- return NULL;
+ if (unlikely(!new_ptr)) {
+ tc->flags &= ~TALLOC_FLAG_FREE;
+ return NULL;
}
tc = (struct talloc_chunk *)new_ptr;
tc->next->prev = tc;
}
- TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+ if (!talloc_memlimit_update(tc->limit, tc->size, size)) {
+ talloc_abort("cur_size memlimit counter not correct!");
+ errno = EINVAL;
+ return NULL;
+ }
tc->size = size;
_talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
break;
case TOTAL_MEM_LIMIT:
if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
- total = tc->size + TC_HDR_SIZE;
+ /*
+ * Don't count memory allocated from a pool
+ * when calculating limits. Only count the
+ * pool itself.
+ */
+ if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
+ total = tc->size + TC_HDR_SIZE;
+ }
}
break;
}
}
if (depth == 0) {
- fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
+ 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));
return;
}
- fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
+ fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
depth*4, "",
name,
(unsigned long)talloc_total_size(ptr),
atexit(talloc_report_null_full);
}
-/*
- talloc and zero memory.
+/*
+ talloc and zero memory.
*/
_PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
{
}
/*
- memdup with a talloc.
+ memdup with a talloc.
*/
_PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
{
old_limit, new_limit);
}
+static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
+{
+ struct talloc_memlimit *l;
+
+ for (l = limit; l != NULL; l = l->upper) {
+ if (l->max_size != 0 &&
+ ((l->max_size <= l->cur_size) ||
+ (l->max_size - l->cur_size < size))) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/*
+ Update memory limits when freeing a talloc_chunk.
+*/
+static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
+{
+ if (!tc->limit) {
+ return;
+ }
+
+ /*
+ * Pool entries don't count. Only the pools
+ * themselves are counted as part of the memory
+ * limits.
+ */
+ if (tc->flags & TALLOC_FLAG_POOLMEM) {
+ return;
+ }
+
+ /*
+ * If we are part of a memory limited context hierarchy
+ * we need to subtract the memory used from the counters
+ */
+
+ talloc_memlimit_shrink(tc->limit, tc->size+TC_HDR_SIZE);
+
+ if (tc->limit->parent == tc) {
+ free(tc->limit);
+ }
+
+ tc->limit = NULL;
+}
+
+/*
+ Increase memory limit accounting after a malloc/realloc.
+*/
+static void talloc_memlimit_grow(struct talloc_memlimit *limit,
+ size_t size)
+{
+ struct talloc_memlimit *l;
+
+ for (l = limit; l != NULL; l = l->upper) {
+ size_t new_cur_size = l->cur_size + size;
+ if (new_cur_size < l->cur_size) {
+ talloc_abort("logic error in talloc_memlimit_grow\n");
+ return;
+ }
+ l->cur_size = new_cur_size;
+ }
+}
+
+/*
+ Decrease memory limit accounting after a free/realloc.
+*/
+static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
+ size_t size)
+{
+ struct talloc_memlimit *l;
+
+ for (l = limit; l != NULL; l = l->upper) {
+ if (l->cur_size < size) {
+ talloc_abort("logic error in talloc_memlimit_shrink\n");
+ return;
+ }
+ l->cur_size = l->cur_size - size;
+ }
+}
+
+static bool talloc_memlimit_update(struct talloc_memlimit *limit,
+ size_t old_size, size_t new_size)
+{
+ struct talloc_memlimit *l;
+ ssize_t d;
+
+ if (old_size == 0) {
+ d = new_size + TC_HDR_SIZE;
+ } else {
+ d = new_size - old_size;
+ }
+ for (l = limit; l != NULL; l = l->upper) {
+ ssize_t new_cur_size = l->cur_size + d;
+ if (new_cur_size < 0) {
+ return false;
+ }
+ l->cur_size = new_cur_size;
+ }
+
+ return true;
+}
+
_PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
{
struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);