mirror of
https://github.com/samba-team/samba.git
synced 2025-03-05 20:58:40 +03:00
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 commit is contained in:
parent
38b19fad28
commit
1ab63cf3a6
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user