Skip to content

Commit 1e8a44a

Browse files
authored
Merge pull request #880 from ldorau/Increase_code_coverage_of_Coarse_provider
Increase code coverage of Coarse provider
2 parents f8bf899 + 7760ebc commit 1e8a44a

File tree

2 files changed

+54
-90
lines changed

2 files changed

+54
-90
lines changed

src/provider/provider_coarse.c

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,9 @@ static block_t *free_blocks_rm_ge(struct ravl *free_blocks, size_t size,
413413
case CHECK_ALL_BLOCKS_OF_SIZE:
414414
block = node_list_rm_with_alignment(head_node, alignment);
415415
break;
416+
// wrong value of check_blocks
416417
default:
417-
LOG_DEBUG("wrong value of check_blocks");
418-
block = NULL;
419-
assert(0);
420-
break;
418+
abort();
421419
}
422420

423421
if (head_node->head == NULL) {
@@ -863,11 +861,7 @@ static umf_result_t coarse_memory_provider_free(void *provider, void *ptr,
863861

864862
static umf_result_t coarse_memory_provider_initialize(void *params,
865863
void **provider) {
866-
umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN;
867-
868-
if (provider == NULL) {
869-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
870-
}
864+
assert(provider);
871865

872866
if (params == NULL) {
873867
LOG_ERR("coarse provider parameters are missing");
@@ -931,33 +925,33 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
931925
coarse_provider->disable_upstream_provider_free = false;
932926
}
933927

934-
umf_result = coarse_memory_provider_set_name(coarse_provider);
928+
umf_result_t umf_result = coarse_memory_provider_set_name(coarse_provider);
935929
if (umf_result != UMF_RESULT_SUCCESS) {
936930
LOG_ERR("name initialization failed");
937931
goto err_free_coarse_provider;
938932
}
939933

934+
// most of the error handling paths below set this error
935+
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
936+
940937
coarse_provider->upstream_blocks =
941938
ravl_new_sized(coarse_ravl_comp, sizeof(ravl_data_t));
942939
if (coarse_provider->upstream_blocks == NULL) {
943940
LOG_ERR("out of the host memory");
944-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
945941
goto err_free_name;
946942
}
947943

948944
coarse_provider->free_blocks =
949945
ravl_new_sized(coarse_ravl_comp, sizeof(ravl_data_t));
950946
if (coarse_provider->free_blocks == NULL) {
951947
LOG_ERR("out of the host memory");
952-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
953948
goto err_delete_ravl_upstream_blocks;
954949
}
955950

956951
coarse_provider->all_blocks =
957952
ravl_new_sized(coarse_ravl_comp, sizeof(ravl_data_t));
958953
if (coarse_provider->all_blocks == NULL) {
959954
LOG_ERR("out of the host memory");
960-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
961955
goto err_delete_ravl_free_blocks;
962956
}
963957

@@ -966,6 +960,7 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
966960

967961
if (utils_mutex_init(&coarse_provider->lock) == NULL) {
968962
LOG_ERR("lock initialization failed");
963+
umf_result = UMF_RESULT_ERROR_UNKNOWN;
969964
goto err_delete_ravl_all_blocks;
970965
}
971966

@@ -976,7 +971,6 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
976971
coarse_memory_provider_alloc(
977972
coarse_provider, coarse_params->init_buffer_size, 0, &init_buffer);
978973
if (init_buffer == NULL) {
979-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
980974
goto err_destroy_mutex;
981975
}
982976

@@ -1069,11 +1063,6 @@ static void coarse_ravl_cb_rm_all_blocks_node(void *data, void *arg) {
10691063
}
10701064

10711065
static void coarse_memory_provider_finalize(void *provider) {
1072-
if (provider == NULL) {
1073-
assert(0);
1074-
return;
1075-
}
1076-
10771066
coarse_memory_provider_t *coarse_provider =
10781067
(struct coarse_memory_provider_t *)provider;
10791068

@@ -1199,21 +1188,16 @@ find_free_block(struct ravl *free_blocks, size_t size, size_t alignment,
11991188
return free_blocks_rm_ge(free_blocks, size + alignment, 0,
12001189
CHECK_ONLY_THE_FIRST_BLOCK);
12011190

1191+
// unknown memory allocation strategy
12021192
default:
1203-
LOG_ERR("unknown memory allocation strategy");
1204-
assert(0);
1205-
return NULL;
1193+
abort();
12061194
}
12071195
}
12081196

12091197
static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12101198
size_t alignment,
12111199
void **resultPtr) {
1212-
umf_result_t umf_result = UMF_RESULT_SUCCESS;
1213-
1214-
if (provider == NULL) {
1215-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1216-
}
1200+
umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN;
12171201

12181202
if (resultPtr == NULL) {
12191203
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -1252,9 +1236,7 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12521236
umf_result =
12531237
create_aligned_block(coarse_provider, size, alignment, &curr);
12541238
if (umf_result != UMF_RESULT_SUCCESS) {
1255-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1256-
LOG_ERR("unlocking the lock failed");
1257-
}
1239+
utils_mutex_unlock(&coarse_provider->lock);
12581240
return umf_result;
12591241
}
12601242
}
@@ -1263,9 +1245,7 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12631245
// Split the current block and put the new block after the one that we use.
12641246
umf_result = split_current_block(coarse_provider, curr, size);
12651247
if (umf_result != UMF_RESULT_SUCCESS) {
1266-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1267-
LOG_ERR("unlocking the lock failed");
1268-
}
1248+
utils_mutex_unlock(&coarse_provider->lock);
12691249
return umf_result;
12701250
}
12711251

@@ -1284,28 +1264,23 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12841264
coarse_provider->used_size += size;
12851265

12861266
assert(debug_check(coarse_provider));
1287-
1288-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1289-
LOG_ERR("unlocking the lock failed");
1290-
return UMF_RESULT_ERROR_UNKNOWN;
1291-
}
1267+
utils_mutex_unlock(&coarse_provider->lock);
12921268

12931269
return UMF_RESULT_SUCCESS;
12941270
}
12951271

12961272
// no suitable block found - try to get more memory from the upstream provider
1273+
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
12971274

12981275
if (coarse_provider->upstream_memory_provider == NULL) {
12991276
LOG_ERR("out of memory - no upstream memory provider given");
1300-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
13011277
goto err_unlock;
13021278
}
13031279

13041280
umfMemoryProviderAlloc(coarse_provider->upstream_memory_provider, size,
13051281
alignment, resultPtr);
13061282
if (*resultPtr == NULL) {
13071283
LOG_ERR("out of memory - upstream memory provider allocation failed");
1308-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
13091284
goto err_unlock;
13101285
}
13111286

@@ -1327,23 +1302,13 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
13271302

13281303
err_unlock:
13291304
assert(debug_check(coarse_provider));
1330-
1331-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1332-
LOG_ERR("unlocking the lock failed");
1333-
if (umf_result == UMF_RESULT_SUCCESS) {
1334-
umf_result = UMF_RESULT_ERROR_UNKNOWN;
1335-
}
1336-
}
1305+
utils_mutex_unlock(&coarse_provider->lock);
13371306

13381307
return umf_result;
13391308
}
13401309

13411310
static umf_result_t coarse_memory_provider_free(void *provider, void *ptr,
13421311
size_t bytes) {
1343-
if (provider == NULL) {
1344-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1345-
}
1346-
13471312
coarse_memory_provider_t *coarse_provider =
13481313
(struct coarse_memory_provider_t *)provider;
13491314

@@ -1398,11 +1363,7 @@ static umf_result_t coarse_memory_provider_free(void *provider, void *ptr,
13981363
}
13991364

14001365
assert(debug_check(coarse_provider));
1401-
1402-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1403-
LOG_ERR("unlocking the lock failed");
1404-
return UMF_RESULT_ERROR_UNKNOWN;
1405-
}
1366+
utils_mutex_unlock(&coarse_provider->lock);
14061367

14071368
return UMF_RESULT_SUCCESS;
14081369
}
@@ -1424,10 +1385,6 @@ static void coarse_memory_provider_get_last_native_error(void *provider,
14241385
static umf_result_t coarse_memory_provider_get_min_page_size(void *provider,
14251386
void *ptr,
14261387
size_t *pageSize) {
1427-
if (provider == NULL) {
1428-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1429-
}
1430-
14311388
coarse_memory_provider_t *coarse_provider =
14321389
(struct coarse_memory_provider_t *)provider;
14331390

@@ -1443,10 +1400,6 @@ static umf_result_t coarse_memory_provider_get_min_page_size(void *provider,
14431400
static umf_result_t
14441401
coarse_memory_provider_get_recommended_page_size(void *provider, size_t size,
14451402
size_t *pageSize) {
1446-
if (provider == NULL) {
1447-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1448-
}
1449-
14501403
coarse_memory_provider_t *coarse_provider =
14511404
(struct coarse_memory_provider_t *)provider;
14521405

@@ -1460,10 +1413,6 @@ coarse_memory_provider_get_recommended_page_size(void *provider, size_t size,
14601413
}
14611414

14621415
static const char *coarse_memory_provider_get_name(void *provider) {
1463-
if (provider == NULL) {
1464-
return COARSE_BASE_NAME;
1465-
}
1466-
14671416
coarse_memory_provider_t *coarse_provider =
14681417
(struct coarse_memory_provider_t *)provider;
14691418

@@ -1503,14 +1452,6 @@ static void ravl_cb_count_free(void *data, void *arg) {
15031452
static umf_result_t
15041453
coarse_memory_provider_get_stats(void *provider,
15051454
coarse_memory_provider_stats_t *stats) {
1506-
if (provider == NULL) {
1507-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1508-
}
1509-
1510-
if (stats == NULL) {
1511-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1512-
}
1513-
15141455
coarse_memory_provider_t *coarse_provider =
15151456
(struct coarse_memory_provider_t *)provider;
15161457

@@ -1628,13 +1569,7 @@ static umf_result_t coarse_memory_provider_allocation_split(void *provider,
16281569

16291570
err_mutex_unlock:
16301571
assert(debug_check(coarse_provider));
1631-
1632-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1633-
LOG_ERR("unlocking the lock failed");
1634-
if (umf_result == UMF_RESULT_SUCCESS) {
1635-
umf_result = UMF_RESULT_ERROR_UNKNOWN;
1636-
}
1637-
}
1572+
utils_mutex_unlock(&coarse_provider->lock);
16381573

16391574
return umf_result;
16401575
}
@@ -1731,13 +1666,7 @@ static umf_result_t coarse_memory_provider_allocation_merge(void *provider,
17311666

17321667
err_mutex_unlock:
17331668
assert(debug_check(coarse_provider));
1734-
1735-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1736-
LOG_ERR("unlocking the lock failed");
1737-
if (umf_result == UMF_RESULT_SUCCESS) {
1738-
umf_result = UMF_RESULT_ERROR_UNKNOWN;
1739-
}
1740-
}
1669+
utils_mutex_unlock(&coarse_provider->lock);
17411670

17421671
return umf_result;
17431672
}

test/provider_coarse.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ TEST_F(test, coarseProvider_name_upstream) {
7070
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
7171
ASSERT_NE(coarse_memory_provider, nullptr);
7272

73+
size_t minPageSize = 0;
74+
umf_result = umfMemoryProviderGetMinPageSize(coarse_memory_provider,
75+
nullptr, &minPageSize);
76+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_UNKNOWN);
77+
ASSERT_EQ(minPageSize, 0);
78+
79+
size_t pageSize = 0;
80+
umf_result = umfMemoryProviderGetRecommendedPageSize(
81+
coarse_memory_provider, minPageSize, &pageSize);
82+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_UNKNOWN);
83+
ASSERT_EQ(pageSize, minPageSize);
84+
7385
ASSERT_EQ(
7486
strcmp(umfMemoryProviderGetName(coarse_memory_provider), COARSE_NAME),
7587
0);
@@ -106,6 +118,18 @@ TEST_F(test, coarseProvider_name_no_upstream) {
106118
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
107119
ASSERT_NE(coarse_memory_provider, nullptr);
108120

121+
size_t minPageSize = 0;
122+
umf_result = umfMemoryProviderGetMinPageSize(coarse_memory_provider,
123+
nullptr, &minPageSize);
124+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
125+
ASSERT_GT(minPageSize, 0);
126+
127+
size_t pageSize = 0;
128+
umf_result = umfMemoryProviderGetRecommendedPageSize(
129+
coarse_memory_provider, minPageSize, &pageSize);
130+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
131+
ASSERT_GE(pageSize, minPageSize);
132+
109133
ASSERT_EQ(
110134
strcmp(umfMemoryProviderGetName(coarse_memory_provider), BASE_NAME), 0);
111135

@@ -122,6 +146,17 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseProvider_null_stats) {
122146
ASSERT_EQ(GetStats(nullptr).num_free_blocks, 0);
123147
}
124148

149+
// wrong NULL parameters
150+
TEST_P(CoarseWithMemoryStrategyTest, coarseProvider_NULL_params) {
151+
umf_result_t umf_result;
152+
153+
umf_memory_provider_handle_t coarse_memory_provider = nullptr;
154+
umf_result = umfMemoryProviderCreate(umfCoarseMemoryProviderOps(), nullptr,
155+
&coarse_memory_provider);
156+
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
157+
ASSERT_EQ(coarse_memory_provider, nullptr);
158+
}
159+
125160
// wrong parameters: given no upstream_memory_provider
126161
// nor init_buffer while exactly one of them must be set
127162
TEST_P(CoarseWithMemoryStrategyTest, coarseProvider_wrong_params_0) {

0 commit comments

Comments
 (0)