瀏覽代碼

reintroduce calloc elison of memset for direct-mmapped allocations

a new weak predicate function replacable by the malloc implementation,
__malloc_allzerop, is introduced. by default it's always false; the
default version will be used when static linking if the bump allocator
was used (in which case performance doesn't matter) or if malloc was
replaced by the application. only if the real internal malloc is
linked (always the case with dynamic linking) does the real version
get used.

if malloc was replaced dynamically, as indicated by __malloc_replaced,
the predicate function is ignored and conditional-memset is always
performed.
Rich Felker 4 年之前
父節點
當前提交
25cef5c591
共有 3 個文件被更改,包括 15 次插入1 次删除
  1. 1 0
      src/internal/dynlink.h
  2. 9 1
      src/malloc/calloc.c
  3. 5 0
      src/malloc/oldmalloc/malloc.c

+ 1 - 0
src/internal/dynlink.h

@@ -107,5 +107,6 @@ hidden ptrdiff_t __tlsdesc_static(), __tlsdesc_dynamic();
 
 hidden extern int __malloc_replaced;
 hidden void __malloc_donate(char *, char *);
+hidden int __malloc_allzerop(void *);
 
 #endif

+ 9 - 1
src/malloc/calloc.c

@@ -2,6 +2,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <errno.h>
+#include "dynlink.h"
 
 static size_t mal0_clear(char *p, size_t n)
 {
@@ -23,6 +24,12 @@ static size_t mal0_clear(char *p, size_t n)
 	}
 }
 
+static int allzerop(void *p)
+{
+	return 0;
+}
+weak_alias(allzerop, __malloc_allzerop);
+
 void *calloc(size_t m, size_t n)
 {
 	if (n && m > (size_t)-1/n) {
@@ -31,7 +38,8 @@ void *calloc(size_t m, size_t n)
 	}
 	n *= m;
 	void *p = malloc(n);
-	if (!p) return p;
+	if (!p || (!__malloc_replaced && __malloc_allzerop(p)))
+		return p;
 	n = mal0_clear(p, n);
 	return memset(p, 0, n);
 }

+ 5 - 0
src/malloc/oldmalloc/malloc.c

@@ -339,6 +339,11 @@ void *malloc(size_t n)
 	return CHUNK_TO_MEM(c);
 }
 
+int __malloc_allzerop(void *p)
+{
+	return IS_MMAPPED(MEM_TO_CHUNK(p));
+}
+
 void *realloc(void *p, size_t n)
 {
 	struct chunk *self, *next;