From 7733047c7bbab01d755f649f7b29bd2356c74887 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 2 Sep 2018 19:41:26 -0400 Subject: [PATCH] trigger early initialization in a conservative way --- malloc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/malloc.c b/malloc.c index 459206e..aea65a3 100644 --- a/malloc.c +++ b/malloc.c @@ -651,6 +651,12 @@ static inline void init(void) { init_slow_path(); } +// trigger early initialization to set up pthread_atfork and protect state as soon as possible +COLD __attribute__((constructor(101))) static void trigger_early_init(void) { + // avoid calling init directly to skip it if this isn't the malloc implementation + h_free(h_malloc(16)); +} + static inline void enforce_init(void) { if (!atomic_load_explicit(&ro.initialized, memory_order_acquire)) { fatal_error("invalid uninitialized allocator usage");