Skip to content

Commit b25e047

Browse files
authored
Merge pull request #13996 from LDong-Arm/tdb_buf_size
TDBStore: pad program units when writing record_header_t; ensure work buffer is large enough
2 parents 18f6794 + a4907e8 commit b25e047

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

storage/kvstore/include/kvstore/TDBStore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ class TDBStore : public KVStore {
308308
tdbstore_area_data_t _area_params[_num_areas];
309309
uint32_t _prog_size;
310310
uint8_t *_work_buf;
311+
size_t _work_buf_size;
311312
char *_key_buf;
312313
void *_inc_set_handle;
313314
void *_iterator_table[_max_open_iterators];

storage/kvstore/source/TDBStore.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ typedef struct {
7777
uint32_t crc;
7878
} reserved_trailer_t;
7979

80-
static const uint32_t work_buf_size = 64;
80+
static const size_t min_work_buf_size = 64;
8181
static const uint32_t initial_crc = 0xFFFFFFFF;
8282
static const uint32_t initial_max_keys = 16;
8383

@@ -130,7 +130,7 @@ static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data
130130
TDBStore::TDBStore(BlockDevice *bd) : _ram_table(0), _max_keys(0),
131131
_num_keys(0), _bd(bd), _buff_bd(0), _free_space_offset(0), _master_record_offset(0),
132132
_master_record_size(0), _is_initialized(false), _active_area(0), _active_area_version(0), _size(0),
133-
_area_params{}, _prog_size(0), _work_buf(0), _key_buf(0), _inc_set_handle(0)
133+
_area_params{}, _prog_size(0), _work_buf(0), _work_buf_size(0), _key_buf(0), _inc_set_handle(0)
134134
{
135135
for (int i = 0; i < _num_areas; i++) {
136136
_area_params[i] = { 0 };
@@ -196,7 +196,7 @@ int TDBStore::erase_erase_unit(uint8_t area, uint32_t offset)
196196
void TDBStore::calc_area_params()
197197
{
198198
// TDBStore can't exceed 32 bits
199-
bd_size_t bd_size = std::min(_bd->size(), (bd_size_t) 0x80000000L);
199+
bd_size_t bd_size = std::min<bd_size_t>(_bd->size(), 0x80000000L);
200200

201201
memset(_area_params, 0, sizeof(_area_params));
202202
size_t area_0_size = 0;
@@ -293,7 +293,7 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
293293
return MBED_ERROR_INVALID_SIZE;
294294
}
295295

296-
actual_data_size = std::min((size_t)data_buf_size, (size_t)data_size - data_offset);
296+
actual_data_size = std::min<size_t>(data_buf_size, data_size - data_offset);
297297

298298
if (copy_data && actual_data_size && !data_buf) {
299299
return MBED_ERROR_INVALID_ARGUMENT;
@@ -327,7 +327,7 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
327327
user_key_ptr[key_size] = '\0';
328328
} else {
329329
dest_buf = _work_buf;
330-
chunk_size = std::min(key_size, work_buf_size);
330+
chunk_size = std::min<size_t>(key_size, _work_buf_size);
331331
}
332332
} else {
333333
// This means that we're on the data part
@@ -337,13 +337,13 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
337337
// 3. After actual part is finished - read to work buffer
338338
// 4. Copy data flag not set - read to work buffer
339339
if (curr_data_offset < data_offset) {
340-
chunk_size = std::min((size_t)work_buf_size, (size_t)(data_offset - curr_data_offset));
340+
chunk_size = std::min<size_t>(_work_buf_size, (data_offset - curr_data_offset));
341341
dest_buf = _work_buf;
342342
} else if (copy_data && (curr_data_offset < data_offset + actual_data_size)) {
343343
chunk_size = actual_data_size;
344344
dest_buf = static_cast<uint8_t *>(data_buf);
345345
} else {
346-
chunk_size = std::min(work_buf_size, total_size);
346+
chunk_size = std::min<size_t>(_work_buf_size, total_size);
347347
dest_buf = _work_buf;
348348
}
349349
}
@@ -847,7 +847,10 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
847847
}
848848

849849
chunk_size = align_up(sizeof(record_header_t), _prog_size);
850-
ret = write_area(1 - from_area, to_offset, chunk_size, &header);
850+
// The record header takes up whole program units
851+
memset(_work_buf, 0, chunk_size);
852+
memcpy(_work_buf, &header, sizeof(record_header_t));
853+
ret = write_area(1 - from_area, to_offset, chunk_size, _work_buf);
851854
if (ret) {
852855
return ret;
853856
}
@@ -857,7 +860,7 @@ int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_o
857860
total_size -= chunk_size;
858861

859862
while (total_size) {
860-
chunk_size = std::min(total_size, work_buf_size);
863+
chunk_size = std::min<size_t>(total_size, _work_buf_size);
861864
ret = read_area(from_area, from_offset, chunk_size, _work_buf);
862865
if (ret) {
863866
return ret;
@@ -1041,7 +1044,8 @@ int TDBStore::init()
10411044
}
10421045

10431046
_prog_size = _bd->get_program_size();
1044-
_work_buf = new uint8_t[work_buf_size];
1047+
_work_buf_size = std::max<size_t>(_prog_size, min_work_buf_size);
1048+
_work_buf = new uint8_t[_work_buf_size];
10451049
_key_buf = new char[MAX_KEY_SIZE];
10461050
_inc_set_handle = new inc_set_handle_t;
10471051
memset(_inc_set_handle, 0, sizeof(inc_set_handle_t));

storage/kvstore/tests/UNITTESTS/TDBStore/moduletest.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
#include "kvstore/TDBStore.h"
2121
#include <stdlib.h>
2222

23-
#define BLOCK_SIZE (512)
23+
#define BLOCK_SIZE (256)
2424
#define DEVICE_SIZE (BLOCK_SIZE*200)
2525

2626
using namespace mbed;
2727

2828
class TDBStoreModuleTest : public testing::Test {
2929
protected:
30-
HeapBlockDevice heap{DEVICE_SIZE};
30+
HeapBlockDevice heap{DEVICE_SIZE, BLOCK_SIZE};
3131
FlashSimBlockDevice flash{&heap};
3232
TDBStore tdb{&flash};
3333

@@ -77,15 +77,17 @@ TEST_F(TDBStoreModuleTest, erased_set_get)
7777

7878
TEST_F(TDBStoreModuleTest, set_deinit_init_get)
7979
{
80-
char buf[100];
8180
size_t size;
8281
for (int i = 0; i < 100; ++i) {
83-
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
82+
char str[11] = {0};
83+
char buf[100] = {0};
84+
int len = snprintf(str, 11, "data%d", i) + 1;
85+
EXPECT_EQ(tdb.set("key", str, len, 0), MBED_SUCCESS);
8486
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
8587
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
8688
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
87-
EXPECT_EQ(size, 5);
88-
EXPECT_STREQ("data", buf);
89+
EXPECT_EQ(size, len);
90+
EXPECT_STREQ(str, buf);
8991
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
9092
}
9193
}

0 commit comments

Comments
 (0)