From 35d29fcb08fadaf006561a058746b0fce76a6a74 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 18 Jul 2002 15:00:07 +0000 Subject: Miles Bader implemented a new mmap based malloc which is much smarter than the old "malloc-simple", and actually works, unlike the old "malloc". So kill the old "malloc-simple" and the old "malloc" and replace them with Miles' new malloc implementation. Update Config files to match. Thanks Miles! --- libc/stdlib/malloc/realloc.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 libc/stdlib/malloc/realloc.c (limited to 'libc/stdlib/malloc/realloc.c') diff --git a/libc/stdlib/malloc/realloc.c b/libc/stdlib/malloc/realloc.c new file mode 100644 index 000000000..faf7ac2f8 --- /dev/null +++ b/libc/stdlib/malloc/realloc.c @@ -0,0 +1,76 @@ +/* + * libc/stdlib/malloc-zarg/realloc.c -- realloc function + * + * Copyright (C) 2002 NEC Corporation + * Copyright (C) 2002 Miles Bader + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License. See the file COPYING.LIB in the main + * directory of this archive for more details. + * + * Written by Miles Bader + */ + +#include +#include +#include + +#include "malloc.h" +#include "heap.h" + + +void *realloc (void *mem, size_t new_size) +{ + if (! mem) + return malloc (new_size); + else + { + void *base_mem = (size_t *)mem - 1; + size_t size = *(size_t *)base_mem; + + MALLOC_DEBUG ("realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)\n", + (long)mem, new_size, (long)base_mem, size); + + if (new_size <= size) + return mem; + else + { + void *new_mem = 0; + size_t ext_size = new_size - size; + void *ext_addr = (char *)base_mem + ext_size; + + if (size >= MALLOC_MMAP_THRESHOLD) + /* Try to extend this block in place using mmap. */ + { + ext_size += MALLOC_ROUND_UP_TO_PAGE_SIZE (ext_size); + + new_mem = mmap (ext_addr, ext_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_SHARED | MAP_ANONYMOUS, 0, 0); + if (new_mem == MAP_FAILED) + /* Can't do it. */ + ext_size = 0; + } + else + ext_size = __heap_alloc_at (&__malloc_heap, ext_addr, ext_size); + + if (! ext_size) + /* Our attempts to extend MEM in place failed, just + allocate-and-copy. */ + { + new_mem = malloc (new_size); + if (new_mem) + { + memcpy (new_mem, mem, size); + free (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; + } + } +} -- cgit v1.2.3