@@ -59,6 +59,7 @@ TEST_F(test, disjointCoarseMallocPool_name_upstream) {
59
59
sizeof (coarse_memory_provider_params));
60
60
coarse_memory_provider_params.upstream_memory_provider =
61
61
malloc_memory_provider;
62
+ coarse_memory_provider_params.destroy_upstream_memory_provider = true ;
62
63
coarse_memory_provider_params.immediate_init_from_upstream = true ;
63
64
coarse_memory_provider_params.init_buffer = nullptr ;
64
65
coarse_memory_provider_params.init_buffer_size = init_buffer_size;
@@ -75,7 +76,9 @@ TEST_F(test, disjointCoarseMallocPool_name_upstream) {
75
76
0 );
76
77
77
78
umfMemoryProviderDestroy (coarse_memory_provider);
78
- umfMemoryProviderDestroy (malloc_memory_provider);
79
+ // malloc_memory_provider has already been destroyed
80
+ // by umfMemoryProviderDestroy(coarse_memory_provider), because:
81
+ // coarse_memory_provider_params.destroy_upstream_memory_provider = true;
79
82
}
80
83
81
84
TEST_F (test, disjointCoarseMallocPool_name_no_upstream) {
@@ -129,6 +132,7 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_basic) {
129
132
coarse_memory_provider_params.allocation_strategy = allocation_strategy;
130
133
coarse_memory_provider_params.upstream_memory_provider =
131
134
malloc_memory_provider;
135
+ coarse_memory_provider_params.destroy_upstream_memory_provider = true ;
132
136
coarse_memory_provider_params.immediate_init_from_upstream = true ;
133
137
coarse_memory_provider_params.init_buffer = nullptr ;
134
138
coarse_memory_provider_params.init_buffer_size = init_buffer_size;
@@ -150,7 +154,7 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_basic) {
150
154
umf_memory_pool_handle_t pool;
151
155
umf_result = umfPoolCreate (umfDisjointPoolOps (), coarse_memory_provider,
152
156
&disjoint_memory_pool_params,
153
- UMF_POOL_CREATE_FLAG_NONE , &pool);
157
+ UMF_POOL_CREATE_FLAG_OWN_PROVIDER , &pool);
154
158
ASSERT_EQ (umf_result, UMF_RESULT_SUCCESS);
155
159
ASSERT_NE (pool, nullptr );
156
160
@@ -282,8 +286,10 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_basic) {
282
286
ASSERT_EQ (GetStats (prov).alloc_size , init_buffer_size);
283
287
284
288
umfPoolDestroy (pool);
285
- umfMemoryProviderDestroy (coarse_memory_provider);
286
- umfMemoryProviderDestroy (malloc_memory_provider);
289
+ // Both coarse_memory_provider and malloc_memory_provider
290
+ // have already been destroyed by umfPoolDestroy(), because:
291
+ // UMF_POOL_CREATE_FLAG_OWN_PROVIDER was set in umfPoolCreate() and
292
+ // coarse_memory_provider_params.destroy_upstream_memory_provider = true;
287
293
}
288
294
289
295
TEST_P (CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_simple1) {
@@ -760,6 +766,37 @@ TEST_P(CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_wrong_params_4) {
760
766
umfMemoryProviderDestroy (malloc_memory_provider);
761
767
}
762
768
769
+ // wrong parameters: destroy_upstream_memory_provider is true, but an upstream provider is not provided
770
+ TEST_P (CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_wrong_params_5) {
771
+ umf_result_t umf_result;
772
+
773
+ const size_t init_buffer_size = 20 * MB;
774
+
775
+ // Preallocate some memory
776
+ std::unique_ptr<char []> buffer (new char [init_buffer_size]);
777
+ void *buf = buffer.get ();
778
+ ASSERT_NE (buf, nullptr );
779
+ memset (buf, 0 , init_buffer_size);
780
+
781
+ coarse_memory_provider_params_t coarse_memory_provider_params;
782
+ // make sure there are no undefined members - prevent a UB
783
+ memset (&coarse_memory_provider_params, 0 ,
784
+ sizeof (coarse_memory_provider_params));
785
+ coarse_memory_provider_params.allocation_strategy = allocation_strategy;
786
+ coarse_memory_provider_params.upstream_memory_provider = nullptr ;
787
+ coarse_memory_provider_params.destroy_upstream_memory_provider = true ;
788
+ coarse_memory_provider_params.immediate_init_from_upstream = false ;
789
+ coarse_memory_provider_params.init_buffer = buf;
790
+ coarse_memory_provider_params.init_buffer_size = init_buffer_size;
791
+
792
+ umf_memory_provider_handle_t coarse_memory_provider = nullptr ;
793
+ umf_result = umfMemoryProviderCreate (umfCoarseMemoryProviderOps (),
794
+ &coarse_memory_provider_params,
795
+ &coarse_memory_provider);
796
+ ASSERT_EQ (umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
797
+ ASSERT_EQ (coarse_memory_provider, nullptr );
798
+ }
799
+
763
800
TEST_P (CoarseWithMemoryStrategyTest, disjointCoarseMallocPool_split_merge) {
764
801
umf_memory_provider_handle_t malloc_memory_provider;
765
802
umf_result_t umf_result;
0 commit comments