From fad7141454e0a04cf3a99c9d285b6a1d24edbd45 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Tue, 28 Apr 2009 13:08:38 +0200 Subject: [PATCH] Fix variable shadowing and chunk linking in gcMalloc() gcMalloc() had a local variable len, that shadowed the function parameter with the same name, besides, it is cleaner to first check how long the remaining free memory chunk is before writing to it. Signed-off-by: Guennadi Liakhovetski --- src/alloc.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index 99fc235..aaf22d3 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1666,13 +1666,13 @@ void initialiseGC(InitArgs *args) { /* ------------------------- ALLOCATION ROUTINES ------------------------- */ -void *gcMalloc(int len) { +void *gcMalloc(int length) { /* The state determines what action to take in the event of allocation failure. The states go up in seriousness, and are visible to other threads */ static enum { gc, run_finalizers, throw_oom } state = gc; - int n = (len+HEADER_SIZE+OBJECT_GRAIN-1)&~(OBJECT_GRAIN-1); + int n = (length + HEADER_SIZE + OBJECT_GRAIN - 1) & ~(OBJECT_GRAIN - 1); uintptr_t largest; Chunk *found; Thread *self; @@ -1710,13 +1710,16 @@ void *gcMalloc(int len) { if(len > n) { Chunk *rem; + unsigned int rem_len; found = *chunkpp; - rem = (Chunk*)((char*)found + n); - rem->header = len - n; + + rem_len = len - n; /* Chain the remainder onto the freelist only if it's large enough to hold an object */ - if(rem->header >= MIN_OBJECT_SIZE) { + if(rem_len >= MIN_OBJECT_SIZE) { + rem = (Chunk*)((char*)found + n); + rem->header = rem_len; rem->next = found->next; *chunkpp = rem; } else -- 2.11.4.GIT