fix to lib/talloc.c

Kenichi Okuyama okuyamak at dd.iij4u.or.jp
Sat Apr 29 15:25:11 GMT 2000


I added little changes to lib/talloc.c.

With this patch, 

1) calling malloc() within talloc.c will become 1/2( well, at least,
   nearly half ).

2) If you are using glibc2's malloc, then entire talloced space will
   be returned to system at talloc_destroy() time.
   # Original code has chance of keeping space for
   # 'struct talloc_chunk'.



--- ./source/lib/talloc.c	2000/04/28 12:04:08	1.1
+++ ./source/lib/talloc.c	2000/04/29 14:23:16
@@ -55,25 +55,22 @@
 void *talloc(TALLOC_CTX *t, size_t size)
 {
 	void *p;
 
-	size = (size + TALLOC_ALIGN) & (~TALLOC_ALIGN-1);
+	size = (size + TALLOC_ALIGN) & (~(TALLOC_ALIGN-1));
 
 	if (!t->list || (t->list->total_size - t->list->alloc_size) < size) {
 		struct talloc_chunk *c;
-		size_t asize = (size + TALLOC_CHUNK_SIZE) & ~(TALLOC_CHUNK_SIZE-1);
+		size_t asize = (sizeof(*c) + size + TALLOC_CHUNK_SIZE) & (~(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;
+		c = (struct talloc_chunk *)malloc( asize );
+		if ( !c ) return NULL;
+		c->next	= t->list;
+		c->ptr	= ((char *)c)
+		  + ((sizeof(*c) + TALLOC_ALIGN) & (~(TALLOC_ALIGN-1)));
+		c->alloc_size	= 0;
+		c->total_size	= asize - (sizeof(*c) + TALLOC_ALIGN) &(~(TALLOC_ALIGN-1));
+		t->list		= c;
 	}
 
 	p = ((char *)t->list->ptr) + t->list->alloc_size;
 	t->list->alloc_size += size;
@@ -86,9 +83,8 @@
 	struct talloc_chunk *c;
 	
 	while (t->list) {
 		c = t->list->next;
-		free(t->list->ptr);
 		free(t->list);
 		t->list = c;
 	}
 


More information about the samba mailing list