From 8d532c51318bad2436880ecac972c9dfa3996c9b Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Tue, 30 Dec 2003 10:40:49 +0000 Subject: Rework malloc. The new default implementation is based on dlmalloc from Doug Lea. It is about 2x faster than the old malloc-930716, and behave itself much better -- it will properly release memory back to the system, and it uses a combination of brk() for small allocations and mmap() for larger allocations. -Erik --- libc/stdlib/malloc-simple/alloc.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'libc/stdlib/malloc-simple') diff --git a/libc/stdlib/malloc-simple/alloc.c b/libc/stdlib/malloc-simple/alloc.c index ee0135581..fcac02927 100644 --- a/libc/stdlib/malloc-simple/alloc.c +++ b/libc/stdlib/malloc-simple/alloc.c @@ -31,14 +31,14 @@ void *malloc(size_t size) #ifdef __UCLIBC_HAS_MMU__ result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (result == MAP_FAILED) return 0; * (size_t *) result = size; return(result + sizeof(size_t)); #else result = mmap((void *) 0, size, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_ANONYMOUS, 0, 0); + MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (result == MAP_FAILED) return 0; return(result); @@ -47,12 +47,27 @@ void *malloc(size_t size) #endif #ifdef L_calloc -void *calloc(size_t num, size_t size) +void * calloc(size_t nmemb, size_t lsize) { - void *ptr = malloc(num * size); - if (ptr) - memset(ptr, 0, num * size); - return ptr; + void *result; + size_t size=lsize * nmemb; + + /* guard vs integer overflow, but allow nmemb + * to fall through and call malloc(0) */ + if (nmemb && lsize != (size / nmemb)) { + __set_errno(ENOMEM); + return NULL; + } + result=malloc(size); +#if 0 + /* Standard unix mmap using /dev/zero clears memory so calloc + * doesn't need to actually zero anything.... + */ + if (result != NULL) { + memset(result, 0, size); + } +#endif + return result; } #endif -- cgit v1.2.3