Skip to content

Commit 78334c5

Browse files
committed
Unify global umf_ba_pool_t initialization
Initializing the global pool in libumf and in each pool separately led to problems with double initialization when multiple pools were used in a single application (and static libumf). To avoid this problem, use util_init_once inside umf_ba_get_pool instead of having explicit constructor/destructor
1 parent 57751ad commit 78334c5

File tree

6 files changed

+20
-42
lines changed

6 files changed

+20
-42
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,39 @@
66
*/
77

88
#include <assert.h>
9+
#include <stdlib.h>
910

1011
#include "base_alloc.h"
12+
#include "utils_concurrency.h"
1113

1214
#define SIZE_BA_POOL_CHUNK 128
1315

1416
// global base allocator used by all providers and pools
1517
static umf_ba_pool_t *BA_pool = NULL;
18+
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT;
1619

17-
int umf_ba_create_global(void) {
18-
assert(BA_pool == NULL);
19-
20-
BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
21-
if (!BA_pool) {
22-
return -1;
23-
}
24-
25-
return 0;
26-
}
27-
28-
void umf_ba_destroy_global(void) {
20+
static void umf_ba_destroy_global(void) {
2921
assert(BA_pool);
3022
umf_ba_destroy(BA_pool);
3123
BA_pool = NULL;
3224
}
3325

26+
static void umf_ba_create_global(void) {
27+
assert(BA_pool == NULL);
28+
BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
29+
if (BA_pool) {
30+
atexit(umf_ba_destroy_global);
31+
}
32+
}
33+
3434
umf_ba_pool_t *umf_ba_get_pool(size_t size) {
35+
util_init_once(&ba_is_initialized, umf_ba_create_global);
36+
37+
if (!BA_pool) {
38+
return NULL;
39+
}
40+
3541
// TODO: a specific class-size base allocator can be returned here
36-
assert(BA_pool);
3742
assert(size <= SIZE_BA_POOL_CHUNK);
3843

3944
if (size > SIZE_BA_POOL_CHUNK) {

src/base_alloc/base_alloc_global.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
extern "C" {
1515
#endif
1616

17-
int umf_ba_create_global(void);
18-
void umf_ba_destroy_global(void);
1917
umf_ba_pool_t *umf_ba_get_pool(size_t size);
2018

2119
#ifdef __cplusplus

src/libumf_linux.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ umf_memory_tracker_handle_t TRACKER = NULL;
1616

1717
void __attribute__((constructor)) umfCreate(void) {
1818
TRACKER = umfMemoryTrackerCreate();
19-
umf_ba_create_global();
2019
}
2120

2221
void __attribute__((destructor)) umfDestroy(void) {
23-
umf_ba_destroy_global();
2422
umfMemoryTrackerDestroy(TRACKER);
2523
}
2624

src/libumf_windows.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@
1515

1616
umf_memory_tracker_handle_t TRACKER = NULL;
1717

18-
static void umfCreate(void) {
19-
TRACKER = umfMemoryTrackerCreate();
20-
umf_ba_create_global();
21-
}
18+
static void umfCreate(void) { TRACKER = umfMemoryTrackerCreate(); }
2219

23-
static void umfDestroy(void) {
24-
umf_ba_destroy_global();
25-
umfMemoryTrackerDestroy(TRACKER);
26-
}
20+
static void umfDestroy(void) { umfMemoryTrackerDestroy(TRACKER); }
2721

2822
#if defined(UMF_SHARED_LIBRARY)
2923
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {

src/pool/pool_jemalloc.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
2222

2323
#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)
2424

25-
#ifdef UMF_SHARED_LIBRARY
26-
static UTIL_ONCE_FLAG jemalloc_is_initialized = UTIL_ONCE_FLAG_INIT;
27-
static void jemalloc_global_init(void) {
28-
umf_ba_create_global();
29-
atexit(umf_ba_destroy_global);
30-
}
31-
#endif
32-
3325
typedef struct jemalloc_memory_pool_t {
3426
umf_memory_provider_handle_t provider;
3527
unsigned int arena_index; // index of jemalloc arena
@@ -362,10 +354,6 @@ static void *je_aligned_alloc(void *pool, size_t size, size_t alignment) {
362354

363355
static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
364356
void *params, void **out_pool) {
365-
#ifdef UMF_SHARED_LIBRARY
366-
util_init_once(&jemalloc_is_initialized, jemalloc_global_init);
367-
#endif
368-
369357
assert(provider);
370358
assert(out_pool);
371359
(void)params; // unused

src/pool/pool_scalable.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ static void init_tbb_global_state(void) {
104104
return;
105105
}
106106

107-
#ifdef UMF_SHARED_LIBRARY
108-
umf_ba_create_global();
109-
atexit(umf_ba_destroy_global);
110-
#endif
111-
112107
g_tbb_ops = tbb_ops;
113108
}
114109

0 commit comments

Comments
 (0)