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 | 038d3977e2c57f87a4afc44d0d0610ecae20e80d (patch) | |
tree | cf1ef3851dcee2d6e1f5264c1294e2e6ce0acb26 | |
parent | cd4fea0098ce5200d764f51bef16980f972925ff (diff) | |
download | samba-038d3977e2c57f87a4afc44d0d0610ecae20e80d.tar.gz samba-038d3977e2c57f87a4afc44d0d0610ecae20e80d.tar.bz2 samba-038d3977e2c57f87a4afc44d0d0610ecae20e80d.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
(This used to be commit 1ab63cf3a6f62c3e4ca19a549bde78dad0eb4340)
-rw-r--r-- | source3/include/talloc.h | 3 | ||||
-rw-r--r-- | source3/lib/talloc.c | 65 |
2 files changed, 37 insertions, 31 deletions
diff --git a/source3/include/talloc.h b/source3/include/talloc.h index 32b0f28ae6..a4df28c8b0 100644 --- a/source3/include/talloc.h +++ b/source3/include/talloc.h @@ -29,9 +29,8 @@ struct talloc_chunk { struct talloc_chunk *next; + size_t size; void *ptr; - size_t alloc_size; - size_t total_size; }; typedef struct { diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c index 54a3d8ed76..aa9fc3ed75 100644 --- a/source3/lib/talloc.c +++ b/source3/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; } |