Skip to content

Commit c867ef6

Browse files
committed
Verify if provider supports the split() operation
Verify in the initialization of the jemalloc pool if a memory provider supports the split() operation, because the jemalloc pool requires that. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 2e1992e commit c867ef6

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/pool/pool_jemalloc.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <string.h>
1313

1414
#include "base_alloc_global.h"
15+
#include "memory_provider_internal.h"
16+
#include "provider_tracking.h"
1517
#include "utils_common.h"
1618
#include "utils_concurrency.h"
1719
#include "utils_log.h"
@@ -283,8 +285,15 @@ static bool arena_extent_split(extent_hooks_t *extent_hooks, void *addr,
283285

284286
jemalloc_memory_pool_t *pool = get_pool_by_arena_index(arena_ind);
285287
assert(pool);
286-
return umfMemoryProviderAllocationSplit(pool->provider, addr, size,
287-
size_a) != UMF_RESULT_SUCCESS;
288+
289+
umf_result_t ret =
290+
umfMemoryProviderAllocationSplit(pool->provider, addr, size, size_a);
291+
if (ret != UMF_RESULT_SUCCESS) {
292+
LOG_ERR("memory provider failed to split a memory region, while "
293+
"jemalloc requires that");
294+
}
295+
296+
return ret != UMF_RESULT_SUCCESS;
288297
}
289298

290299
// arena_extent_merge - an extent merge function conforms to the extent_merge_t type and optionally
@@ -435,11 +444,45 @@ static void *op_aligned_alloc(void *pool, size_t size, size_t alignment) {
435444
return ptr;
436445
}
437446

447+
// Verify if the memory provider supports the split() operation,
448+
// because jemalloc pool requires that.
449+
static umf_result_t verify_split(umf_memory_provider_handle_t provider) {
450+
// Retrieve the upstream memory provider
451+
umf_memory_provider_handle_t upstream_provider = NULL;
452+
umfTrackingMemoryProviderGetUpstreamProvider(
453+
umfMemoryProviderGetPriv(provider), &upstream_provider);
454+
455+
size_t page_size;
456+
umf_result_t ret =
457+
umfMemoryProviderGetMinPageSize(upstream_provider, NULL, &page_size);
458+
if (ret != UMF_RESULT_SUCCESS) {
459+
return ret;
460+
}
461+
462+
size_t size = 2 * page_size; // use double the page size for the split test
463+
if (UMF_RESULT_ERROR_NOT_SUPPORTED ==
464+
umfMemoryProviderAllocationSplit(upstream_provider, (void *)size, size,
465+
page_size)) {
466+
LOG_ERR("memory provider does not support the split operation, while "
467+
"jemalloc pool requires that");
468+
return UMF_RESULT_ERROR_NOT_SUPPORTED;
469+
}
470+
471+
return UMF_RESULT_SUCCESS;
472+
}
473+
438474
static umf_result_t op_initialize(umf_memory_provider_handle_t provider,
439475
const void *params, void **out_pool) {
440476
assert(provider);
441477
assert(out_pool);
442478

479+
// Verify if the memory provider supports the split() operation,
480+
// because jemalloc pool requires that.
481+
umf_result_t ret = verify_split(provider);
482+
if (ret != UMF_RESULT_SUCCESS) {
483+
return ret;
484+
}
485+
443486
extent_hooks_t *pHooks = &arena_extent_hooks;
444487
size_t unsigned_size = sizeof(unsigned);
445488
int n_arenas_set_from_params = 0;

test/pools/jemalloc_pool.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,22 @@ TEST(JemallocPoolOps, default_name_null_handle) {
237237
UMF_RESULT_SUCCESS);
238238
EXPECT_STREQ(name, "jemalloc");
239239
}
240+
241+
TEST_F(test, jemallocProviderDoesNotSupportSplit) {
242+
umf_jemalloc_pool_params_handle_t params = nullptr;
243+
umf_result_t res = umfJemallocPoolParamsCreate(&params);
244+
EXPECT_EQ(res, UMF_RESULT_SUCCESS);
245+
246+
umf_memory_provider_handle_t ba_provider;
247+
umf_result_t ret =
248+
umfMemoryProviderCreate(&BA_GLOBAL_PROVIDER_OPS, nullptr, &ba_provider);
249+
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
250+
251+
umf_memory_pool_handle_t pool = nullptr;
252+
res = umfPoolCreate(umfJemallocPoolOps(), ba_provider, params, 0, &pool);
253+
EXPECT_EQ(res, UMF_RESULT_ERROR_NOT_SUPPORTED);
254+
EXPECT_EQ(pool, nullptr);
255+
256+
umfMemoryProviderDestroy(ba_provider);
257+
umfJemallocPoolParamsDestroy(params);
258+
}

0 commit comments

Comments
 (0)