Skip to content

Commit 966125b

Browse files
committed
Increase code coverage of Coarse provider
Increase code coverage of Coarse provider: - add missing tests, - remove unreachable checks. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent f607b03 commit 966125b

File tree

2 files changed

+58
-90
lines changed

2 files changed

+58
-90
lines changed

src/provider/provider_coarse.c

Lines changed: 23 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,36 @@ 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;
941+
// umf_result is already set to UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY
945942
goto err_free_name;
946943
}
947944

948945
coarse_provider->free_blocks =
949946
ravl_new_sized(coarse_ravl_comp, sizeof(ravl_data_t));
950947
if (coarse_provider->free_blocks == NULL) {
951948
LOG_ERR("out of the host memory");
952-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
949+
// umf_result is already set to UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY
953950
goto err_delete_ravl_upstream_blocks;
954951
}
955952

956953
coarse_provider->all_blocks =
957954
ravl_new_sized(coarse_ravl_comp, sizeof(ravl_data_t));
958955
if (coarse_provider->all_blocks == NULL) {
959956
LOG_ERR("out of the host memory");
960-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
957+
// umf_result is already set to UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY
961958
goto err_delete_ravl_free_blocks;
962959
}
963960

@@ -966,6 +963,7 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
966963

967964
if (utils_mutex_init(&coarse_provider->lock) == NULL) {
968965
LOG_ERR("lock initialization failed");
966+
umf_result = UMF_RESULT_ERROR_UNKNOWN;
969967
goto err_delete_ravl_all_blocks;
970968
}
971969

@@ -976,7 +974,7 @@ static umf_result_t coarse_memory_provider_initialize(void *params,
976974
coarse_memory_provider_alloc(
977975
coarse_provider, coarse_params->init_buffer_size, 0, &init_buffer);
978976
if (init_buffer == NULL) {
979-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
977+
// umf_result is already set to UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY
980978
goto err_destroy_mutex;
981979
}
982980

@@ -1069,11 +1067,6 @@ static void coarse_ravl_cb_rm_all_blocks_node(void *data, void *arg) {
10691067
}
10701068

10711069
static void coarse_memory_provider_finalize(void *provider) {
1072-
if (provider == NULL) {
1073-
assert(0);
1074-
return;
1075-
}
1076-
10771070
coarse_memory_provider_t *coarse_provider =
10781071
(struct coarse_memory_provider_t *)provider;
10791072

@@ -1199,21 +1192,16 @@ find_free_block(struct ravl *free_blocks, size_t size, size_t alignment,
11991192
return free_blocks_rm_ge(free_blocks, size + alignment, 0,
12001193
CHECK_ONLY_THE_FIRST_BLOCK);
12011194

1195+
// unknown memory allocation strategy
12021196
default:
1203-
LOG_ERR("unknown memory allocation strategy");
1204-
assert(0);
1205-
return NULL;
1197+
abort();
12061198
}
12071199
}
12081200

12091201
static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12101202
size_t alignment,
12111203
void **resultPtr) {
1212-
umf_result_t umf_result = UMF_RESULT_SUCCESS;
1213-
1214-
if (provider == NULL) {
1215-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1216-
}
1204+
umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN;
12171205

12181206
if (resultPtr == NULL) {
12191207
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
@@ -1252,9 +1240,7 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12521240
umf_result =
12531241
create_aligned_block(coarse_provider, size, alignment, &curr);
12541242
if (umf_result != UMF_RESULT_SUCCESS) {
1255-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1256-
LOG_ERR("unlocking the lock failed");
1257-
}
1243+
utils_mutex_unlock(&coarse_provider->lock);
12581244
return umf_result;
12591245
}
12601246
}
@@ -1263,9 +1249,7 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12631249
// Split the current block and put the new block after the one that we use.
12641250
umf_result = split_current_block(coarse_provider, curr, size);
12651251
if (umf_result != UMF_RESULT_SUCCESS) {
1266-
if (utils_mutex_unlock(&coarse_provider->lock) != 0) {
1267-
LOG_ERR("unlocking the lock failed");
1268-
}
1252+
utils_mutex_unlock(&coarse_provider->lock);
12691253
return umf_result;
12701254
}
12711255

@@ -1284,28 +1268,23 @@ static umf_result_t coarse_memory_provider_alloc(void *provider, size_t size,
12841268
coarse_provider->used_size += size;
12851269

12861270
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-
}
1271+
utils_mutex_unlock(&coarse_provider->lock);
12921272

12931273
return UMF_RESULT_SUCCESS;
12941274
}
12951275

12961276
// no suitable block found - try to get more memory from the upstream provider
1277+
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
12971278

12981279
if (coarse_provider->upstream_memory_provider == NULL) {
12991280
LOG_ERR("out of memory - no upstream memory provider given");
1300-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
13011281
goto err_unlock;
13021282
}
13031283

13041284
umfMemoryProviderAlloc(coarse_provider->upstream_memory_provider, size,
13051285
alignment, resultPtr);
13061286
if (*resultPtr == NULL) {
13071287
LOG_ERR("out of memory - upstream memory provider allocation failed");
1308-
umf_result = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
13091288
goto err_unlock;
13101289
}
13111290

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

13281307
err_unlock:
13291308
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-
}
1309+
utils_mutex_unlock(&coarse_provider->lock);
13371310

13381311
return umf_result;
13391312
}
13401313

13411314
static umf_result_t coarse_memory_provider_free(void *provider, void *ptr,
13421315
size_t bytes) {
1343-
if (provider == NULL) {
1344-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1345-
}
1346-
13471316
coarse_memory_provider_t *coarse_provider =
13481317
(struct coarse_memory_provider_t *)provider;
13491318

@@ -1398,11 +1367,7 @@ static umf_result_t coarse_memory_provider_free(void *provider, void *ptr,
13981367
}
13991368

14001369
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-
}
1370+
utils_mutex_unlock(&coarse_provider->lock);
14061371

14071372
return UMF_RESULT_SUCCESS;
14081373
}
@@ -1424,10 +1389,6 @@ static void coarse_memory_provider_get_last_native_error(void *provider,
14241389
static umf_result_t coarse_memory_provider_get_min_page_size(void *provider,
14251390
void *ptr,
14261391
size_t *pageSize) {
1427-
if (provider == NULL) {
1428-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1429-
}
1430-
14311392
coarse_memory_provider_t *coarse_provider =
14321393
(struct coarse_memory_provider_t *)provider;
14331394

@@ -1443,10 +1404,6 @@ static umf_result_t coarse_memory_provider_get_min_page_size(void *provider,
14431404
static umf_result_t
14441405
coarse_memory_provider_get_recommended_page_size(void *provider, size_t size,
14451406
size_t *pageSize) {
1446-
if (provider == NULL) {
1447-
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
1448-
}
1449-
14501407
coarse_memory_provider_t *coarse_provider =
14511408
(struct coarse_memory_provider_t *)provider;
14521409

@@ -1460,10 +1417,6 @@ coarse_memory_provider_get_recommended_page_size(void *provider, size_t size,
14601417
}
14611418

14621419
static const char *coarse_memory_provider_get_name(void *provider) {
1463-
if (provider == NULL) {
1464-
return COARSE_BASE_NAME;
1465-
}
1466-
14671420
coarse_memory_provider_t *coarse_provider =
14681421
(struct coarse_memory_provider_t *)provider;
14691422

@@ -1503,14 +1456,6 @@ static void ravl_cb_count_free(void *data, void *arg) {
15031456
static umf_result_t
15041457
coarse_memory_provider_get_stats(void *provider,
15051458
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-
15141459
coarse_memory_provider_t *coarse_provider =
15151460
(struct coarse_memory_provider_t *)provider;
15161461

@@ -1628,13 +1573,7 @@ static umf_result_t coarse_memory_provider_allocation_split(void *provider,
16281573

16291574
err_mutex_unlock:
16301575
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-
}
1576+
utils_mutex_unlock(&coarse_provider->lock);
16381577

16391578
return umf_result;
16401579
}
@@ -1731,13 +1670,7 @@ static umf_result_t coarse_memory_provider_allocation_merge(void *provider,
17311670

17321671
err_mutex_unlock:
17331672
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-
}
1673+
utils_mutex_unlock(&coarse_provider->lock);
17411674

17421675
return umf_result;
17431676
}

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)