Skip to content

Add disable_provider_free parameter to pool jemalloc #618

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
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
7 changes: 7 additions & 0 deletions include/umf/pools/pool_jemalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
extern "C" {
#endif

#include <stdbool.h>
#include <umf/memory_pool_ops.h>

/// @brief Configuration of Jemalloc Pool
typedef struct umf_jemalloc_pool_params_t {
/// Set to true if umfMemoryProviderFree() should never be called.
bool disable_provider_free;
} umf_jemalloc_pool_params_t;

umf_memory_pool_ops_t *umfJemallocPoolOps(void);

#ifdef __cplusplus
Expand Down
24 changes: 22 additions & 2 deletions src/pool/pool_jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
typedef struct jemalloc_memory_pool_t {
umf_memory_provider_handle_t provider;
unsigned int arena_index; // index of jemalloc arena
// set to true if umfMemoryProviderFree() should never be called
bool disable_provider_free;
} jemalloc_memory_pool_t;

static __TLS umf_result_t TLS_last_allocation_error;
Expand Down Expand Up @@ -80,7 +82,9 @@ static void *arena_extent_alloc(extent_hooks_t *extent_hooks, void *new_addr,
}

if (new_addr != NULL && ptr != new_addr) {
umfMemoryProviderFree(pool->provider, ptr, size);
if (!pool->disable_provider_free) {
umfMemoryProviderFree(pool->provider, ptr, size);
}
return NULL;
}

Expand Down Expand Up @@ -114,6 +118,10 @@ static void arena_extent_destroy(extent_hooks_t *extent_hooks, void *addr,

jemalloc_memory_pool_t *pool = get_pool_by_arena_index(arena_ind);

if (pool->disable_provider_free) {
return;
}

umf_result_t ret;
ret = umfMemoryProviderFree(pool->provider, addr, size);
if (ret != UMF_RESULT_SUCCESS) {
Expand All @@ -136,6 +144,10 @@ static bool arena_extent_dalloc(extent_hooks_t *extent_hooks, void *addr,

jemalloc_memory_pool_t *pool = get_pool_by_arena_index(arena_ind);

if (pool->disable_provider_free) {
return true; // opt-out from deallocation
}

umf_result_t ret;
ret = umfMemoryProviderFree(pool->provider, addr, size);
if (ret != UMF_RESULT_SUCCESS) {
Expand Down Expand Up @@ -388,7 +400,9 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
void *params, void **out_pool) {
assert(provider);
assert(out_pool);
(void)params; // unused

umf_jemalloc_pool_params_t *je_params =
(umf_jemalloc_pool_params_t *)params;

extent_hooks_t *pHooks = &arena_extent_hooks;
size_t unsigned_size = sizeof(unsigned);
Expand All @@ -402,6 +416,12 @@ static umf_result_t op_initialize(umf_memory_provider_handle_t provider,

pool->provider = provider;

if (je_params) {
pool->disable_provider_free = je_params->disable_provider_free;
} else {
pool->disable_provider_free = false;
}

unsigned arena_index;
err = je_mallctl("arenas.create", (void *)&arena_index, &unsigned_size,
NULL, 0);
Expand Down
Loading