diff options
author | phk <phk@FreeBSD.org> | 2004-03-07 20:41:27 +0000 |
---|---|---|
committer | phk <phk@FreeBSD.org> | 2004-03-07 20:41:27 +0000 |
commit | a545780c51218e55facb66312587197d0f2ce9af (patch) | |
tree | 59041b053c92d81f7444838807157f951dc03d8e | |
parent | 4b105bbf3a310353e526d3e6e1c849094480d7e3 (diff) | |
download | FreeBSD-src-a545780c51218e55facb66312587197d0f2ce9af.zip FreeBSD-src-a545780c51218e55facb66312587197d0f2ce9af.tar.gz |
Rearrange (centralize) initialization of mallocs internals to always be
done before the first call, even if this is a malloc(0) call.
PR: 62859
-rw-r--r-- | lib/libc/stdlib/malloc.c | 32 |
1 files changed, 13 insertions, 19 deletions
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 9a2c5cd..6e80284 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -198,9 +198,6 @@ struct pgfree { #define INIT_MMAP() #endif -/* Set when initialization has been done */ -static unsigned malloc_started; - /* Number of free pages we cache */ static unsigned malloc_cache = 16; @@ -491,9 +488,6 @@ malloc_init(void) malloc_ninfo = malloc_pagesize / sizeof *page_dir; - /* Been here, done that */ - malloc_started++; - /* Recalculate the cache size in bytes, and make sure it's nonzero */ if (!malloc_cache) @@ -729,9 +723,6 @@ imalloc(size_t size) { void *result; - if (!malloc_started) - malloc_init(); - if (suicide) abort(); @@ -764,11 +755,6 @@ irealloc(void *ptr, size_t size) if (suicide) abort(); - if (!malloc_started) { - wrtwarning("malloc() has never been called\n"); - return (NULL); - } - index = ptr2index(ptr); if (index < malloc_pageshift) { @@ -1061,11 +1047,6 @@ ifree(void *ptr) if (ptr == NULL) return; - if (!malloc_started) { - wrtwarning("malloc() has never been called\n"); - return; - } - /* If we're already sinking, don't make matters any worse. */ if (suicide) return; @@ -1097,6 +1078,7 @@ pubrealloc(void *ptr, size_t size, const char *func) void *r; int err = 0; static int malloc_active; /* Recusion flag for public interface. */ + static unsigned malloc_started; /* Set when initialization has been done */ /* * If a thread is inside our code with a functional lock held, and then @@ -1115,6 +1097,18 @@ pubrealloc(void *ptr, size_t size, const char *func) return (NULL); } malloc_active = 1; + + if (!malloc_started) { + if (ptr != NULL) { + wrtwarning("malloc() has never been called\n"); + malloc_active = 0; + _MALLOC_UNLOCK(); + errno = EDOOFUS; + return (NULL); + } + malloc_init(); + malloc_started = 1; + } if (ptr == ZEROSIZEPTR) ptr = NULL; |