Skip to content

Commit 8f263da

Browse files
committed
Add parameter to destroy upstream_memory_provider in finalize()
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 0c82ae5 commit 8f263da

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
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
@@ -34,6 +34,9 @@ typedef struct coarse_memory_provider_t {
3434
// memory allocation strategy
3535
coarse_memory_provider_strategy_t allocation_strategy;
3636

37+
// destroy upstream_memory_provider in finalize()
38+
bool destroy_upstream_memory_provider;
39+
3740
void *init_buffer;
3841

3942
size_t used_size;
@@ -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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,37 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_wrong_params_4) {
760760
umfMemoryProviderDestroy(malloc_memory_provider);
761761
}
762762

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

0 commit comments

Comments
 (0)