summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorMiles Bader <miles@lsi.nec.co.jp>2002-07-30 09:47:05 +0000
committerMiles Bader <miles@lsi.nec.co.jp>2002-07-30 09:47:05 +0000
commit9f86edc4a9ef5762c03b7dc42d127c031c16483c (patch)
tree5c91bdf5de19def0d0f0da98a1cb7be05b7e189d /libc
parent906ffafd8be8f5b17638f8529d6298ee086a5804 (diff)
Update the size of grown/shrunk allocations.
MALLOC_SET_SIZE now takes the user-address rather than the base-address.
Diffstat (limited to 'libc')
-rw-r--r--libc/stdlib/malloc/realloc.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/libc/stdlib/malloc/realloc.c b/libc/stdlib/malloc/realloc.c
index 34118ab5a..dbcb19ebe 100644
--- a/libc/stdlib/malloc/realloc.c
+++ b/libc/stdlib/malloc/realloc.c
@@ -38,32 +38,27 @@ realloc (void *mem, size_t new_size)
if (new_size > size)
/* Grow the block. */
{
- void *new_mem = 0;
- size_t ext_size = new_size - size;
- void *ext_addr = base_mem + size;
+ size_t extra = new_size - size;
__malloc_lock ();
- ext_size = __heap_alloc_at (&__malloc_heap, ext_addr, ext_size);
+ extra = __heap_alloc_at (&__malloc_heap, base_mem + size, extra);
__malloc_unlock ();
- if (! ext_size)
+ if (extra)
+ /* Record the changed size. */
+ MALLOC_SET_SIZE (mem, new_size);
+ else
/* Our attempts to extend MEM in place failed, just
allocate-and-copy. */
{
- new_mem = malloc (new_size);
+ void *new_mem = malloc (new_size);
if (new_mem)
{
memcpy (new_mem, mem, size);
free (mem);
}
+ mem = new_mem;
}
-
- if (new_mem)
- MALLOC_DEBUG (" realloc: returning 0x%lx"
- " (base:0x%lx, total_size:%d)\n",
- (long)new_mem, (long)new_mem - sizeof(size_t), size);
-
- return new_mem;
}
else if (new_size + HEAP_MIN_FREE_AREA_SIZE <= size)
/* Shrink the block. */
@@ -71,9 +66,14 @@ realloc (void *mem, size_t new_size)
__malloc_lock ();
__heap_free (&__malloc_heap, base_mem + new_size, new_size - size);
__malloc_unlock ();
+ MALLOC_SET_SIZE (mem, new_size);
}
- else
- /* Do nothing. */
- return mem;
+
+ if (mem)
+ MALLOC_DEBUG (" realloc: returning 0x%lx"
+ " (base:0x%lx, total_size:%d)\n",
+ (long)new_mem, (long)new_mem - sizeof(size_t), size);
+
+ return mem;
}
}