summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc-simple
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-12-30 10:40:49 +0000
committerEric Andersen <andersen@codepoet.org>2003-12-30 10:40:49 +0000
commit8d532c51318bad2436880ecac972c9dfa3996c9b (patch)
tree821863358734242feb99643e9d66ee9b175ad464 /libc/stdlib/malloc-simple
parent4c9086ee4afde4257a4b4a8f55e05932d1b6acfd (diff)
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
Diffstat (limited to 'libc/stdlib/malloc-simple')
-rw-r--r--libc/stdlib/malloc-simple/alloc.c29
1 files changed, 22 insertions, 7 deletions
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