diff options
author | Andrew Tridgell <tridge@samba.org> | 2001-03-23 21:37:30 +0000 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2001-03-23 21:37:30 +0000 |
commit | 1ab63cf3a6f62c3e4ca19a549bde78dad0eb4340 (patch) | |
tree | 1c1a9fa5e462d5e190d17f2a8ff462e0fef93311 /source/lib/talloc.c | |
parent | 38b19fad2851a65268b31c7e0240ed36a8407be4 (diff) | |
download | samba-1ab63cf3a6f62c3e4ca19a549bde78dad0eb4340.tar.gz samba-1ab63cf3a6f62c3e4ca19a549bde78dad0eb4340.tar.xz samba-1ab63cf3a6f62c3e4ca19a549bde78dad0eb4340.zip |
a much simpler talloc() implementation. This version has the following
advantages:
- memory is trackable by insure
- a very simple talloc_realloc() is possible (I've added it)
It is slower than the previous talloc code, but I don't think that
is going to be a problem. If it is a problem then there are
some ways we can make it faster but I'd like to leave those
until we have tested this a bit and can see what performance
problems might show up in profiling
Diffstat (limited to 'source/lib/talloc.c')
-rw-r--r-- | source/lib/talloc.c | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/source/lib/talloc.c b/source/lib/talloc.c index 54a3d8ed769..aa9fc3ed758 100644 --- a/source/lib/talloc.c +++ b/source/lib/talloc.c @@ -35,9 +35,6 @@ #include "includes.h" -#define TALLOC_ALIGN 32 -#define TALLOC_CHUNK_SIZE (0x2000) - /* initialissa talloc context. */ TALLOC_CTX *talloc_init(void) { @@ -56,8 +53,9 @@ TALLOC_CTX *talloc_init(void) void *talloc(TALLOC_CTX *t, size_t size) { void *p; - if (size == 0) - { + struct talloc_chunk *tc; + + if (size == 0) { /* debugging value used to track down memory problems. BAD_PTR is defined in talloc.h */ @@ -65,34 +63,44 @@ void *talloc(TALLOC_CTX *t, size_t size) return p; } - /* normal code path */ - size = (size + (TALLOC_ALIGN-1)) & ~(TALLOC_ALIGN-1); + p = malloc(size); + if (!p) return p; - if (!t->list || (t->list->total_size - t->list->alloc_size) < size) { - struct talloc_chunk *c; - size_t asize = (size + (TALLOC_CHUNK_SIZE-1)) & ~(TALLOC_CHUNK_SIZE-1); - - c = (struct talloc_chunk *)malloc(sizeof(*c)); - if (!c) return NULL; - c->next = t->list; - c->ptr = (void *)malloc(asize); - if (!c->ptr) { - free(c); - return NULL; - } - c->alloc_size = 0; - c->total_size = asize; - t->list = c; - t->total_alloc_size += asize; + tc = malloc(sizeof(*tc)); + if (!tc) { + free(p); + return NULL; } - p = ((char *)t->list->ptr) + t->list->alloc_size; - t->list->alloc_size += size; + tc->ptr = p; + tc->size = size; + tc->next = t->list; + t->list = tc; + t->total_alloc_size += size; - return p; } +/* a talloc version of realloc */ +void *talloc_realloc(TALLOC_CTX *t, void *ptr, size_t size) +{ + void *p; + struct talloc_chunk *tc; + + for (tc=t->list; tc; tc=tc->next) { + if (tc->ptr == ptr) { + ptr = realloc(ptr, size); + if (ptr) { + t->total_alloc_size += (size - tc->size); + tc->size = size; + tc->ptr = ptr; + } + return ptr; + } + } + return NULL; +} + /* destroy a whole pool */ void talloc_destroy_pool(TALLOC_CTX *t) { @@ -103,7 +111,7 @@ void talloc_destroy_pool(TALLOC_CTX *t) while (t->list) { c = t->list->next; - free(t->list->ptr); + if (t->list->ptr) free(t->list->ptr); free(t->list); t->list = c; } @@ -118,14 +126,13 @@ void talloc_destroy(TALLOC_CTX *t) if (!t) return; talloc_destroy_pool(t); + memset(t, 0, sizeof(*t)); free(t); } /* return the current total size of the pool. */ size_t talloc_pool_size(TALLOC_CTX *t) { - if (!t->list) - return 0; return t->total_alloc_size; } |