Skip to content

Commit 4a923b9

Browse files
committed
Add function to get pool name
1 parent 25d9074 commit 4a923b9

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-0
lines changed

include/umf/memory_pool.h

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ umf_memory_pool_handle_t umfPoolByPtr(const void *ptr);
165165
umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
166166
umf_memory_provider_handle_t *hProvider);
167167

168+
///
169+
/// @brief Retrieve name of a given memory \p pool.
170+
/// @param pool handle to the memory pool
171+
/// @return pointer to a string containing the name of the \p pool
172+
///
173+
const char *umfPoolGetName(umf_memory_pool_handle_t pool);
174+
168175
///
169176
/// @brief Set a custom tag on the memory pool that can be later retrieved using umfPoolGetTag.
170177
/// @param hPool specified memory pool

include/umf/memory_pool_ops.h

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ typedef struct umf_memory_pool_ops_t {
141141
///
142142
umf_result_t (*ctl)(void *hPool, int operationType, const char *name,
143143
void *arg, umf_ctl_query_type_t queryType);
144+
145+
///
146+
/// @brief Get the name of the memory pool.
147+
/// @param pool pointer to the memory pool
148+
/// @return name of the memory pool
149+
///
150+
const char *(*get_name)(void *pool);
144151
} umf_memory_pool_ops_t;
145152

146153
#ifdef __cplusplus

src/libumf.def

+1
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ EXPORTS
143143
umfJemallocPoolParamsCreate
144144
umfJemallocPoolParamsDestroy
145145
umfJemallocPoolParamsSetNumArenas
146+
umfPoolGetName

src/libumf.map

+1
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,5 @@ UMF_0.12 {
143143
umfJemallocPoolParamsCreate;
144144
umfJemallocPoolParamsDestroy;
145145
umfJemallocPoolParamsSetNumArenas;
146+
umfPoolGetName;
146147
} UMF_0.11;

src/memory_pool.c

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ umf_result_t umfPoolGetMemoryProvider(umf_memory_pool_handle_t hPool,
168168
return UMF_RESULT_SUCCESS;
169169
}
170170

171+
const char *umfPoolGetName(umf_memory_pool_handle_t pool) {
172+
UMF_CHECK((pool != NULL), NULL);
173+
return pool->ops.get_name(pool->pool_priv);
174+
}
175+
171176
umf_result_t umfPoolCreate(const umf_memory_pool_ops_t *ops,
172177
umf_memory_provider_handle_t provider, void *params,
173178
umf_pool_create_flags_t flags,

src/pool/pool_disjoint.c

+6
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,11 @@ void disjoint_pool_finalize(void *pool) {
929929
umf_ba_global_free(hPool);
930930
}
931931

932+
const char *disjoint_pool_get_name(void *pool) {
933+
disjoint_pool_t *hPool = (disjoint_pool_t *)pool;
934+
return hPool->params.name;
935+
}
936+
932937
static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
933938
.version = UMF_VERSION_CURRENT,
934939
.initialize = disjoint_pool_initialize,
@@ -940,6 +945,7 @@ static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS = {
940945
.malloc_usable_size = disjoint_pool_malloc_usable_size,
941946
.free = disjoint_pool_free,
942947
.get_last_allocation_error = disjoint_pool_get_last_allocation_error,
948+
.get_name = disjoint_pool_get_name,
943949
};
944950

945951
umf_memory_pool_ops_t *umfDisjointPoolOps(void) {

test/pools/disjoint_pool.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "provider.hpp"
1313
#include "provider_null.h"
1414
#include "provider_trace.h"
15+
#include "umf/base.h"
16+
#include "umf/memory_pool.h"
1517

1618
using umf_test::test;
1719
using namespace umf_test;
@@ -327,6 +329,54 @@ TEST_F(test, disjointPoolInvalidBucketSize) {
327329
umfDisjointPoolParamsDestroy(params);
328330
}
329331

332+
TEST_F(test, disjointPoolName) {
333+
umf_disjoint_pool_params_handle_t params = nullptr;
334+
umf_result_t res = umfDisjointPoolParamsCreate(&params);
335+
EXPECT_EQ(res, UMF_RESULT_SUCCESS);
336+
umf_memory_provider_handle_t provider_handle = nullptr;
337+
umf_memory_pool_handle_t pool = NULL;
338+
339+
struct memory_provider : public umf_test::provider_base_t {
340+
umf_result_t expectedResult;
341+
umf_result_t alloc(size_t size, size_t alignment, void **ptr) noexcept {
342+
*ptr = umf_ba_global_aligned_alloc(size, alignment);
343+
return UMF_RESULT_SUCCESS;
344+
}
345+
346+
umf_result_t free(void *ptr, [[maybe_unused]] size_t size) noexcept {
347+
// do the actual free only when we expect the success
348+
if (expectedResult == UMF_RESULT_SUCCESS) {
349+
umf_ba_global_free(ptr);
350+
}
351+
return expectedResult;
352+
}
353+
354+
umf_result_t
355+
get_min_page_size([[maybe_unused]] void *ptr,
356+
[[maybe_unused]] size_t *pageSize) noexcept {
357+
*pageSize = 1024;
358+
return UMF_RESULT_SUCCESS;
359+
}
360+
};
361+
umf_memory_provider_ops_t provider_ops =
362+
umf_test::providerMakeCOps<memory_provider, void>();
363+
364+
auto providerUnique =
365+
wrapProviderUnique(createProviderChecked(&provider_ops, nullptr));
366+
367+
provider_handle = providerUnique.get();
368+
369+
res =
370+
umfPoolCreate(umfDisjointPoolOps(), provider_handle, params, 0, &pool);
371+
EXPECT_EQ(res, UMF_RESULT_SUCCESS);
372+
const char *name = umfPoolGetName(pool);
373+
EXPECT_STREQ(name, "disjoint");
374+
375+
EXPECT_EQ(umfPoolGetName(nullptr), nullptr);
376+
umfPoolDestroy(pool);
377+
umfDisjointPoolParamsDestroy(params);
378+
}
379+
330380
INSTANTIATE_TEST_SUITE_P(disjointPoolTests, umfPoolTest,
331381
::testing::Values(poolCreateExtParams{
332382
umfDisjointPoolOps(), defaultDisjointPoolConfig,

0 commit comments

Comments
 (0)