Skip to content

Commit 9f6eb80

Browse files
committed
Add parameter to destroy upstream_memory_provider in finalize()
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 5bf1b5e commit 9f6eb80

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

include/umf/providers/provider_coarse.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ typedef struct coarse_memory_provider_params_t {
6969
/// the init_buffer is always used instead
7070
/// (regardless of the value of this parameter).
7171
bool immediate_init_from_upstream;
72+
73+
/// Destroy upstream_memory_provider in finalize().
74+
bool destroy_upstream_memory_provider;
7275
} coarse_memory_provider_params_t;
7376

7477
/// @brief Coarse Memory Provider stats (TODO move to CTL)

src/provider/provider_coarse.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
typedef struct coarse_memory_provider_t {
3232
umf_memory_provider_handle_t upstream_memory_provider;
3333

34+
// destroy upstream_memory_provider in finalize()
35+
bool destroy_upstream_memory_provider;
36+
3437
// memory allocation strategy
3538
coarse_memory_provider_strategy_t allocation_strategy;
3639

@@ -898,6 +901,13 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
898901
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
899902
}
900903

904+
if (coarse_params->destroy_upstream_memory_provider &&
905+
!coarse_params->upstream_memory_provider) {
906+
LOG_ERR("destroy_upstream_memory_provider is true, but an upstream "
907+
"provider is not provided");
908+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
909+
}
910+
901911
coarse_memory_provider_t *coarse_provider =
902912
umf_ba_global_alloc(sizeof(*coarse_provider));
903913
if (!coarse_provider) {
@@ -909,6 +919,8 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
909919

910920
coarse_provider->upstream_memory_provider =
911921
coarse_params->upstream_memory_provider;
922+
coarse_provider->destroy_upstream_memory_provider =
923+
coarse_params->destroy_upstream_memory_provider;
912924
coarse_provider->allocation_strategy = coarse_params->allocation_strategy;
913925
coarse_provider->init_buffer = coarse_params->init_buffer;
914926

@@ -1081,6 +1093,11 @@ static void coarse_memory_provider_finalize(void *provider) {
10811093

10821094
umf_ba_global_free(coarse_provider->name);
10831095

1096+
if (coarse_provider->destroy_upstream_memory_provider &&
1097+
coarse_provider->upstream_memory_provider) {
1098+
umfMemoryProviderDestroy(coarse_provider->upstream_memory_provider);
1099+
}
1100+
10841101
umf_ba_global_free(coarse_provider);
10851102
}
10861103

test/disjointCoarseMallocPool.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ TEST_F(test, disjointCoarseMallocPool_name_upstream) {
5959
sizeof(coarse_memory_provider_params));
6060
coarse_memory_provider_params.upstream_memory_provider =
6161
malloc_memory_provider;
62+
coarse_memory_provider_params.destroy_upstream_memory_provider = true;
6263
coarse_memory_provider_params.immediate_init_from_upstream = true;
6364
coarse_memory_provider_params.init_buffer = nullptr;
6465
coarse_memory_provider_params.init_buffer_size = init_buffer_size;
@@ -75,7 +76,8 @@ TEST_F(test, disjointCoarseMallocPool_name_upstream) {
7576
0);
7677

7778
umfMemoryProviderDestroy(coarse_memory_provider);
78-
umfMemoryProviderDestroy(malloc_memory_provider);
79+
// malloc_memory_provider has been already destroyed, because:
80+
// coarse_memory_provider_params.destroy_upstream_memory_provider = true;
7981
}
8082

8183
TEST_F(test, disjointCoarseMallocPool_name_no_upstream) {
@@ -760,6 +762,37 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_wrong_params_4) {
760762
umfMemoryProviderDestroy(malloc_memory_provider);
761763
}
762764

765+
// wrong parameters: destroy_upstream_memory_provider is true, but an upstream provider is not provided
766+
TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_wrong_params_5) {
767+
umf_result_t umf_result;
768+
769+
const size_t init_buffer_size = 20 * MB;
770+
771+
// Preallocate some memory
772+
std::unique_ptr<char[]> buffer(new char[init_buffer_size]);
773+
void *buf = buffer.get();
774+
ASSERT_NE(buf, nullptr);
775+
memset(buf, 0, init_buffer_size);
776+
777+
coarse_memory_provider_params_t coarse_memory_provider_params;
778+
// make sure there are no undefined members - prevent a UB
779+
memset(&coarse_memory_provider_params, 0,
780+
sizeof(coarse_memory_provider_params));
781+
coarse_memory_provider_params.allocation_strategy = allocation_strategy;
782+
coarse_memory_provider_params.upstream_memory_provider = nullptr;
783+
coarse_memory_provider_params.destroy_upstream_memory_provider = true;
784+
coarse_memory_provider_params.immediate_init_from_upstream = false;
785+
coarse_memory_provider_params.init_buffer = buf;
786+
coarse_memory_provider_params.init_buffer_size = init_buffer_size;
787+
788+
umf_memory_provider_handle_t coarse_memory_provider = nullptr;
789+
umf_result = umfMemoryProviderCreate(umfCoarseMemoryProviderOps(),
790+
&coarse_memory_provider_params,
791+
&coarse_memory_provider);
792+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
793+
ASSERT_EQ(coarse_memory_provider, nullptr);
794+
}
795+
763796
TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_split_merge) {
764797
umf_memory_provider_handle_t malloc_memory_provider;
765798
umf_result_t umf_result;

0 commit comments

Comments
 (0)