From: teodor Date: Tue, 22 Nov 2005 16:07:28 +0000 (+0000) Subject: Optimize mcrealloc X-Git-Url: http://www.sigaev.ru/git/gitweb.cgi?p=tedtools.git;a=commitdiff_plain;h=abc067494646e5e908cb3b6c5818386886a0c81d Optimize mcrealloc --- diff --git a/memtest.c b/memtest.c index 7db7813..9abf087 100644 --- a/memtest.c +++ b/memtest.c @@ -118,7 +118,7 @@ main(int argn, char *argv[]) { printf("mixed:%s free:%d\n", ptr, base->chunk->freesize); ptr = mcrealloc(ptr1, 120); - tassert( ptrchunk->freesize); ptr1 = mcalloc(base, CNTXCHUNK); diff --git a/tmalloc.c b/tmalloc.c index fee18cb..9f96ab9 100644 --- a/tmalloc.c +++ b/tmalloc.c @@ -188,10 +188,10 @@ mcalloc(MemoryContext *cntx, size_t size) { chunk = newchunk; } - chunk->freesize -= size; - alloc = (MCAllocatedSpace*)( chunk->data + chunk->freesize ); + alloc = (MCAllocatedSpace*)( chunk->data + chunk->size - chunk->freesize ); alloc->size = size; alloc->cntx = cntx; + chunk->freesize -= size; if ( cntx->flags & MC_DEBUG ) { *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) = MCMAGICKNUMBER; @@ -223,14 +223,12 @@ mcrealloc(void * ptr, size_t size) { realsize = alloc->size - MCASHDRSZ; if ( size > realsize ) { - if ( (char*)alloc == alloc->cntx->chunk->data + alloc->cntx->chunk->freesize && + MemoryChunk *chunk = alloc->cntx->chunk; + if ( (char*)alloc == chunk->data + chunk->size - chunk->freesize - alloc->size && PTRALIGN(size)-realsize <= alloc->cntx->chunk->freesize ) { /* just enlarge */ alloc->cntx->chunk->freesize -= PTRALIGN(size)-realsize; alloc->size+=PTRALIGN(size)-realsize; - memmove( ((char*)alloc) - (PTRALIGN(size)-realsize), alloc, PTRALIGN(size)+MCASHDRSZ ); - alloc = (MCAllocatedSpace*) ( ((char*)alloc) - (PTRALIGN(size)-realsize) ); - ptr = (void*)alloc->data; if ( alloc->cntx->flags & MC_DEBUG ) { memset( (char*)(alloc->data) + realsize, 0xc3, PTRALIGN(size)-realsize ); *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) = MCMAGICKNUMBER; @@ -251,6 +249,7 @@ mcrealloc(void * ptr, size_t size) { void mcfree(void * ptr) { MCAllocatedSpace *alloc = (MCAllocatedSpace*)( (char*)ptr - MCASHDRSZ ); + MemoryChunk *chunk; if ( ptr==NULL ) tlog(TL_CRIT|TL_EXIT, "mcfree: free null pointer"); @@ -258,7 +257,8 @@ mcfree(void * ptr) { if ( alloc->cntx->flags & MC_DEBUG ) tassert( *(u_int32_t*)((char*)alloc + alloc->size - sizeof(u_int32_t) ) == MCMAGICKNUMBER ); - if ( (char*)alloc == alloc->cntx->chunk->data + alloc->cntx->chunk->freesize ) /* last allocated value */ + chunk = alloc->cntx->chunk; + if ( (char*)alloc == chunk->data + chunk->size - chunk->freesize - alloc->size ) /* last allocated value */ alloc->cntx->chunk->freesize+=alloc->size; alloc->cntx=NULL;