Skip to content

Commit 3f5a946

Browse files
authored
Merge pull request #212 from ldorau/Use_base_allocator_in_jemalloc_pool
Use base allocator in jemalloc pool
2 parents 0d7f242 + 8b71c87 commit 3f5a946

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/pool/pool_jemalloc.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <jemalloc/jemalloc.h>
1515

16+
#include "base_alloc_global.h"
1617
#include "utils_common.h"
1718
#include <umf/memory_pool.h>
1819
#include <umf/memory_pool_ops.h>
@@ -23,6 +24,9 @@
2324
typedef struct jemalloc_memory_pool_t {
2425
umf_memory_provider_handle_t provider;
2526
unsigned int arena_index; // index of jemalloc arena
27+
28+
// saved pointer to the global base allocator
29+
umf_ba_pool_t *base_allocator;
2630
} jemalloc_memory_pool_t;
2731

2832
static __TLS umf_result_t TLS_last_allocation_error;
@@ -357,11 +361,18 @@ static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
357361
size_t unsigned_size = sizeof(unsigned);
358362
int err;
359363

360-
jemalloc_memory_pool_t *pool = malloc(sizeof(jemalloc_memory_pool_t));
364+
umf_ba_pool_t *base_allocator =
365+
umf_ba_get_pool(sizeof(jemalloc_memory_pool_t));
366+
if (!base_allocator) {
367+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
368+
}
369+
370+
jemalloc_memory_pool_t *pool = umf_ba_alloc(base_allocator);
361371
if (!pool) {
362372
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
363373
}
364374

375+
pool->base_allocator = base_allocator;
365376
pool->provider = provider;
366377

367378
unsigned arena_index;
@@ -392,7 +403,7 @@ static umf_result_t je_initialize(umf_memory_provider_handle_t provider,
392403
return UMF_RESULT_SUCCESS;
393404

394405
err_free_pool:
395-
free(pool);
406+
umf_ba_free(base_allocator, pool);
396407
return UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC;
397408
}
398409

@@ -403,7 +414,7 @@ static void je_finalize(void *pool) {
403414
snprintf(cmd, sizeof(cmd), "arena.%u.destroy", je_pool->arena_index);
404415
mallctl(cmd, NULL, 0, NULL, 0);
405416
pool_by_arena_index[je_pool->arena_index] = NULL;
406-
free(je_pool);
417+
umf_ba_free(je_pool->base_allocator, je_pool);
407418
}
408419

409420
static size_t je_malloc_usable_size(void *pool, void *ptr) {

0 commit comments

Comments
 (0)