[uClibc-cvs] uClibc/libc/stdlib/malloc-simple alloc.c,1.12,1.13

Erik Andersen andersen at uclibc.org
Tue Dec 30 10:40:51 UTC 2003


Update of /var/cvs/uClibc/libc/stdlib/malloc-simple
In directory nail:/tmp/cvs-serv5823/libc/stdlib/malloc-simple

Modified Files:
	alloc.c 
Log Message:
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


Index: alloc.c
===================================================================
RCS file: /var/cvs/uClibc/libc/stdlib/malloc-simple/alloc.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- alloc.c	30 Dec 2003 01:41:14 -0000	1.12
+++ alloc.c	30 Dec 2003 10:40:49 -0000	1.13
@@ -31,14 +31,14 @@
 
 #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 @@
 #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
 




More information about the uClibc-cvs mailing list