Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
*/
-#define mremap __mremap
-
#include "malloc.h"
-
/* ------------------------------ realloc ------------------------------ */
void* realloc(void* oldmem, size_t bytes)
{
size_t* s; /* copy source */
size_t* d; /* copy destination */
+ void *retval;
/* Check for special cases. */
if (! oldmem)
return malloc(bytes);
if (! bytes) {
free (oldmem);
- return malloc(bytes);
+ return NULL;
}
- LOCK;
- av = get_malloc_state();
checked_request2size(bytes, nb);
+ __MALLOC_LOCK;
+ av = get_malloc_state();
oldp = mem2chunk(oldmem);
oldsize = chunksize(oldp);
set_head_size(oldp, nb);
av->top = chunk_at_offset(oldp, nb);
set_head(av->top, (newsize - nb) | PREV_INUSE);
- UNLOCK;
- return chunk2mem(oldp);
+ retval = chunk2mem(oldp);
+ goto DONE;
}
/* Try to expand forward into next chunk; split off remainder below */
else {
newmem = malloc(nb - MALLOC_ALIGN_MASK);
if (newmem == 0) {
- UNLOCK;
- return 0; /* propagate failure */
+ retval = 0; /* propagate failure */
+ goto DONE;
}
newp = mem2chunk(newmem);
assert(ncopies >= 3);
if (ncopies > 9)
- __memcpy(d, s, copysize);
+ memcpy(d, s, copysize);
else {
*(d+0) = *(s+0);
free(oldmem);
check_inuse_chunk(newp);
- UNLOCK;
- return chunk2mem(newp);
+ retval = chunk2mem(newp);
+ goto DONE;
}
}
}
}
check_inuse_chunk(newp);
- UNLOCK;
- return chunk2mem(newp);
+ retval = chunk2mem(newp);
+ goto DONE;
}
/*
/* don't need to remap if still within same page */
if (oldsize == newsize - offset) {
- UNLOCK;
- return oldmem;
+ retval = oldmem;
+ goto DONE;
}
cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
if (sum > (unsigned long)(av->max_total_mem))
av->max_total_mem = sum;
- UNLOCK;
- return chunk2mem(newp);
+ retval = chunk2mem(newp);
+ goto DONE;
}
/* Note the extra (sizeof(size_t)) overhead. */
/* Must alloc, copy, free. */
newmem = malloc(nb - MALLOC_ALIGN_MASK);
if (newmem != 0) {
- __memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
+ memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
free(oldmem);
}
}
- UNLOCK;
- return newmem;
+ retval = newmem;
}
+
+ DONE:
+ __MALLOC_UNLOCK;
+ return retval;
}