X-Git-Url: http://gitweb.dragonflybsd.org/dragonfly.git/blobdiff_plain/7a7e10d6267ef12bf6e84d69a9e77fec14d6dccb..e12d3396c777165504d60d2a1408dcd7cb63660d:/lib/libc/stdlib/dmalloc.c diff --git a/lib/libc/stdlib/dmalloc.c b/lib/libc/stdlib/dmalloc.c index 04d60ca..931ac02 100644 --- a/lib/libc/stdlib/dmalloc.c +++ b/lib/libc/stdlib/dmalloc.c @@ -303,6 +303,7 @@ static int opt_cache = 4; static int opt_utrace = 0; static int g_malloc_flags = 0; static int malloc_panic; +static int malloc_started = 0; static const int32_t weirdary[16] = { WEIRD_ADDR, WEIRD_ADDR, WEIRD_ADDR, WEIRD_ADDR, @@ -320,11 +321,7 @@ static void slabterm(slglobaldata_t slgd, slab_t slab); static void *_vmem_alloc(int ri, size_t slab_size); static void _vmem_free(void *ptr, size_t slab_size); static void _mpanic(const char *ctl, ...) __printflike(1, 2); -#ifndef STANDALONE_DEBUG -static void malloc_init(void) __constructor(0); -#else -static void malloc_init(void) __constructor(101); -#endif +static void malloc_init(void); struct nmalloc_utrace { void *p; @@ -353,6 +350,18 @@ static void malloc_init(void) { const char *p = NULL; + static spinlock_t malloc_init_lock; + + if (malloc_started) + return; + + if (__isthreaded) { + _SPINLOCK(&malloc_init_lock); + if (malloc_started) { + _SPINUNLOCK(&malloc_init_lock); + return; + } + } Regions[0].mask = -1; /* disallow activity in lowest region */ @@ -399,6 +408,10 @@ malloc_init(void) UTRACE((void *) -1, 0, NULL); _nmalloc_thr_init(); + malloc_started = 1; + + if (__isthreaded) + _SPINUNLOCK(&malloc_init_lock); } /* @@ -424,6 +437,9 @@ _nmalloc_thr_init(void) TAILQ_INIT(&slglobal.full_zones); slglobal.sldepot = &sldepots[slgi & (NDEPOTS - 1)]; + if (slglobal.masked) + return; + slglobal.masked = 1; if (did_init == 0) { did_init = 1; @@ -712,6 +728,9 @@ memalloc(size_t size, int flags) size_t off; char *obj; + if (!malloc_started) + malloc_init(); + /* * If 0 bytes is requested we have to return a unique pointer, allocate * at least one byte.