Skip to content

Unify global umf_ba_pool_t initialization #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,39 @@
*/

#include <assert.h>
#include <stdlib.h>

#include "base_alloc.h"
#include "utils_concurrency.h"

#define SIZE_BA_POOL_CHUNK 128

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

int umf_ba_create_global(void) {
assert(BA_pool == NULL);

BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
if (!BA_pool) {
return -1;
}

return 0;
}

void umf_ba_destroy_global(void) {
static void umf_ba_destroy_global(void) {
assert(BA_pool);
umf_ba_destroy(BA_pool);
BA_pool = NULL;
}

static void umf_ba_create_global(void) {
assert(BA_pool == NULL);
BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
if (BA_pool) {
atexit(umf_ba_destroy_global);
}
}

umf_ba_pool_t *umf_ba_get_pool(size_t size) {
util_init_once(&ba_is_initialized, umf_ba_create_global);

if (!BA_pool) {
return NULL;
}

// TODO: a specific class-size base allocator can be returned here
assert(BA_pool);
assert(size <= SIZE_BA_POOL_CHUNK);

if (size > SIZE_BA_POOL_CHUNK) {
Expand Down
2 changes: 0 additions & 2 deletions src/base_alloc/base_alloc_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
extern "C" {
#endif

int umf_ba_create_global(void);
void umf_ba_destroy_global(void);
umf_ba_pool_t *umf_ba_get_pool(size_t size);

#ifdef __cplusplus
Expand Down
2 changes: 0 additions & 2 deletions src/libumf_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ umf_memory_tracker_handle_t TRACKER = NULL;

void __attribute__((constructor)) umfCreate(void) {
TRACKER = umfMemoryTrackerCreate();
umf_ba_create_global();
}

void __attribute__((destructor)) umfDestroy(void) {
umf_ba_destroy_global();
umfMemoryTrackerDestroy(TRACKER);
}

Expand Down
10 changes: 2 additions & 8 deletions src/libumf_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@

umf_memory_tracker_handle_t TRACKER = NULL;

static void umfCreate(void) {
TRACKER = umfMemoryTrackerCreate();
umf_ba_create_global();
}
static void umfCreate(void) { TRACKER = umfMemoryTrackerCreate(); }

static void umfDestroy(void) {
umf_ba_destroy_global();
umfMemoryTrackerDestroy(TRACKER);
}
static void umfDestroy(void) { umfMemoryTrackerDestroy(TRACKER); }

#if defined(UMF_SHARED_LIBRARY)
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
Expand Down
12 changes: 0 additions & 12 deletions src/pool/pool_jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@

#define MALLOCX_ARENA_MAX (MALLCTL_ARENAS_ALL - 1)

#ifdef UMF_SHARED_LIBRARY
static UTIL_ONCE_FLAG jemalloc_is_initialized = UTIL_ONCE_FLAG_INIT;
static void jemalloc_global_init(void) {
umf_ba_create_global();
atexit(umf_ba_destroy_global);
}
#endif

typedef struct jemalloc_memory_pool_t {
umf_memory_provider_handle_t provider;
unsigned int arena_index; // index of jemalloc arena
Expand Down Expand Up @@ -362,10 +354,6 @@ static void *je_aligned_alloc(void *pool, size_t size, size_t alignment) {

static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
void *params, void **out_pool) {
#ifdef UMF_SHARED_LIBRARY
util_init_once(&jemalloc_is_initialized, jemalloc_global_init);
#endif

assert(provider);
assert(out_pool);
(void)params; // unused
Expand Down
5 changes: 0 additions & 5 deletions src/pool/pool_scalable.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ static void init_tbb_global_state(void) {
return;
}

#ifdef UMF_SHARED_LIBRARY
umf_ba_create_global();
atexit(umf_ba_destroy_global);
#endif

g_tbb_ops = tbb_ops;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils_windows_concurrency.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ static BOOL CALLBACK initOnceCb(PINIT_ONCE InitOnce, PVOID Parameter,
}

void util_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void)) {
InitOnceExecuteOnce(flag, initOnceCb, (void *)&onceCb, NULL);
InitOnceExecuteOnce(flag, initOnceCb, (void *)onceCb, NULL);
}