Skip to content

Commit 66792b4

Browse files
author
Cruz Monrreal
authored
Merge pull request #9445 from davidsaada/david_nvstore_fix_area_calc
NVStore: fix area calculation function
2 parents 2c5d246 + 765b483 commit 66792b4

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

features/storage/nvstore/TESTS/nvstore/functionality/main.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ static void nvstore_basic_functionality_test()
109109
TEST_SKIP_UNLESS_MESSAGE(max_possible_keys >= max_possible_keys_threshold,
110110
"Max possible keys below threshold. Test skipped.");
111111

112-
nvstore.set_max_keys(max_test_keys);
113-
TEST_ASSERT_EQUAL(max_test_keys, nvstore.get_max_keys());
114-
115112
result = nvstore.reset();
116113
TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result);
117114

115+
nvstore.set_max_keys(max_test_keys);
116+
TEST_ASSERT_EQUAL(max_test_keys, nvstore.get_max_keys());
117+
118118
printf("Max keys %d (out of %d possible ones)\n", nvstore.get_max_keys(), max_possible_keys);
119119

120120
result = nvstore.set(5, 18, nvstore_testing_buf_set);
@@ -503,16 +503,12 @@ static void nvstore_multi_thread_test()
503503
TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, ret);
504504
}
505505

506-
dummy = new (std::nothrow) char[thr_test_num_threads * thr_test_stack_size];
507-
delete[] dummy;
508-
if (!dummy) {
509-
goto mem_fail;
510-
}
511-
512506
for (i = 0; i < thr_test_num_threads; i++) {
513507
threads[i] = new (std::nothrow) rtos::Thread((osPriority_t)((int)osPriorityBelowNormal - thr_test_num_threads + i),
514508
thr_test_stack_size);
515-
if (!threads[i]) {
509+
dummy = new (std::nothrow) char[thr_test_stack_size];
510+
delete[] dummy;
511+
if (!threads[i] || !dummy) {
516512
goto mem_fail;
517513
}
518514
threads[i]->start(mbed::callback(thread_test_worker));

features/storage/nvstore/source/nvstore.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -244,32 +244,29 @@ int NVStore::flash_erase_area(uint8_t area)
244244

245245
void NVStore::calc_validate_area_params()
246246
{
247-
int num_sectors = 0;
248-
249-
size_t flash_addr = _flash->get_flash_start();
247+
size_t flash_start_addr = _flash->get_flash_start();
250248
size_t flash_size = _flash->get_flash_size();
249+
size_t flash_addr;
251250
size_t sector_size;
252-
int max_sectors = flash_size / _flash->get_sector_size(flash_addr) + 1;
253-
size_t *sector_map = new size_t[max_sectors];
254251

255252
int area = 0;
256253
size_t left_size = flash_size;
257254

258255
memcpy(_flash_area_params, initial_area_params, sizeof(_flash_area_params));
259-
int user_config = (_flash_area_params[0].size != 0);
260-
int in_area = 0;
256+
bool user_config = (_flash_area_params[0].size != 0);
257+
bool in_area = false;
261258
size_t area_size = 0;
262259

263-
while (left_size) {
264-
sector_size = _flash->get_sector_size(flash_addr);
265-
sector_map[num_sectors++] = flash_addr;
260+
if (user_config) {
261+
flash_addr = flash_start_addr;
262+
while (left_size) {
263+
sector_size = _flash->get_sector_size(flash_addr);
266264

267-
if (user_config) {
268265
// User configuration - here we validate it
269266
// Check that address is on a sector boundary, that size covers complete sector sizes,
270267
// and that areas don't overlap.
271268
if (_flash_area_params[area].address == flash_addr) {
272-
in_area = 1;
269+
in_area = true;
273270
}
274271
if (in_area) {
275272
area_size += sector_size;
@@ -278,42 +275,34 @@ void NVStore::calc_validate_area_params()
278275
if (area == NVSTORE_NUM_AREAS) {
279276
break;
280277
}
281-
in_area = 0;
278+
in_area = false;
282279
area_size = 0;
283280
}
284281
}
282+
flash_addr += sector_size;
283+
left_size -= sector_size;
285284
}
286-
287-
flash_addr += sector_size;
288-
left_size -= sector_size;
289-
}
290-
sector_map[num_sectors] = flash_addr;
291-
292-
if (user_config) {
293285
// Valid areas were counted. Assert if not the expected number.
294286
MBED_ASSERT(area == NVSTORE_NUM_AREAS);
295287
} else {
296288
// Not user configuration - calculate area parameters.
297289
// Take last two sectors by default. If their sizes aren't big enough, take
298290
// a few consecutive ones.
299291
area = 1;
300-
_flash_area_params[area].size = 0;
301-
int i;
302-
for (i = num_sectors - 1; i >= 0; i--) {
303-
sector_size = sector_map[i + 1] - sector_map[i];
292+
flash_addr = flash_start_addr + flash_size;
293+
_flash_area_params[0].size = 0;
294+
_flash_area_params[1].size = 0;
295+
while (area >= 0) {
296+
MBED_ASSERT(flash_addr > flash_start_addr);
297+
sector_size = _flash->get_sector_size(flash_addr - 1);
298+
flash_addr -= sector_size;
304299
_flash_area_params[area].size += sector_size;
305300
if (_flash_area_params[area].size >= min_area_size) {
306-
_flash_area_params[area].address = sector_map[i];
301+
_flash_area_params[area].address = flash_addr;
307302
area--;
308-
if (area < 0) {
309-
break;
310-
}
311-
_flash_area_params[area].size = 0;
312303
}
313304
}
314305
}
315-
316-
delete[] sector_map;
317306
}
318307

319308

0 commit comments

Comments
 (0)