mirror of
https://github.com/armink/FlashDB.git
synced 2026-09-22 23:15:57 +08:00
Merge pull request #86 from Tennn11/master
【修改】write函数增加sync参数,KVDB和TSDB存储数据时减少sync次数
This commit is contained in:
+2
-2
@@ -48,10 +48,10 @@ uint32_t _fdb_continue_ff_addr(fdb_db_t db, uint32_t start, uint32_t end);
|
||||
fdb_err_t _fdb_init_ex(fdb_db_t db, const char *name, const char *part_name, fdb_db_type type, void *user_data);
|
||||
void _fdb_init_finish(fdb_db_t db, fdb_err_t result);
|
||||
void _fdb_deinit(fdb_db_t db);
|
||||
fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index);
|
||||
fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index, bool sync);
|
||||
size_t _fdb_read_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t total_num);
|
||||
fdb_err_t _fdb_flash_read(fdb_db_t db, uint32_t addr, void *buf, size_t size);
|
||||
fdb_err_t _fdb_flash_erase(fdb_db_t db, uint32_t addr, size_t size);
|
||||
fdb_err_t _fdb_flash_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size);
|
||||
fdb_err_t _fdb_flash_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync);
|
||||
|
||||
#endif /* _FDB_LOW_LVL_H_ */
|
||||
|
||||
@@ -111,7 +111,7 @@ void _fdb_deinit(fdb_db_t db)
|
||||
}
|
||||
#else
|
||||
if (db->cur_file != 0) {
|
||||
fclose(db->cur_file)
|
||||
fclose(db->cur_file);
|
||||
}
|
||||
#endif /* FDB_USING_FILE_POSIX_MODE */
|
||||
#endif /* FDB_USING_FILE_MODE */
|
||||
|
||||
+8
-4
@@ -88,7 +88,7 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
|
||||
return result;
|
||||
}
|
||||
|
||||
fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size)
|
||||
fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync)
|
||||
{
|
||||
fdb_err_t result = FDB_NO_ERR;
|
||||
int fd = open_db_file(db, addr, false);
|
||||
@@ -96,7 +96,9 @@ fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t si
|
||||
addr = addr % db->sec_size;
|
||||
lseek(fd, addr, SEEK_SET);
|
||||
write(fd, buf, size);
|
||||
fsync(fd);
|
||||
if(sync) {
|
||||
fsync(fd);
|
||||
}
|
||||
} else {
|
||||
result = FDB_READ_ERR;
|
||||
}
|
||||
@@ -172,7 +174,7 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
|
||||
return result;
|
||||
}
|
||||
|
||||
fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size)
|
||||
fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync)
|
||||
{
|
||||
fdb_err_t result = FDB_NO_ERR;
|
||||
FILE *fp = open_db_file(db, addr, false);
|
||||
@@ -180,7 +182,9 @@ fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t si
|
||||
addr = addr % db->sec_size;
|
||||
fseek(fp, addr, SEEK_SET);
|
||||
fwrite(buf, size, 1, fp);
|
||||
fflush(fp);
|
||||
if(sync) {
|
||||
fflush(fp);
|
||||
}
|
||||
} else {
|
||||
result = FDB_READ_ERR;
|
||||
}
|
||||
|
||||
+17
-17
@@ -325,7 +325,7 @@ static fdb_err_t read_kv(fdb_kvdb_t db, fdb_kv_t kv)
|
||||
if (kv->status != FDB_KV_ERR_HDR) {
|
||||
kv->status = FDB_KV_ERR_HDR;
|
||||
FDB_DEBUG("Error: The KV @0x%08" PRIX32 " length has an error.\n", kv->addr.start);
|
||||
_fdb_write_status((fdb_db_t)db, kv->addr.start, kv_hdr.status_table, FDB_KV_STATUS_NUM, FDB_KV_ERR_HDR);
|
||||
_fdb_write_status((fdb_db_t)db, kv->addr.start, kv_hdr.status_table, FDB_KV_STATUS_NUM, FDB_KV_ERR_HDR, true);
|
||||
}
|
||||
kv->crc_is_ok = false;
|
||||
return FDB_READ_ERR;
|
||||
@@ -701,12 +701,12 @@ static fdb_err_t write_kv_hdr(fdb_kvdb_t db, uint32_t addr, kv_hdr_data_t kv_hdr
|
||||
{
|
||||
fdb_err_t result = FDB_NO_ERR;
|
||||
/* write the status will by write granularity */
|
||||
result = _fdb_write_status((fdb_db_t)db, addr, kv_hdr->status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_WRITE);
|
||||
result = _fdb_write_status((fdb_db_t)db, addr, kv_hdr->status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_WRITE, false);
|
||||
if (result != FDB_NO_ERR) {
|
||||
return result;
|
||||
}
|
||||
/* write other header data */
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr + KV_MAGIC_OFFSET, &kv_hdr->magic, sizeof(struct kv_hdr_data) - KV_MAGIC_OFFSET);
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr + KV_MAGIC_OFFSET, &kv_hdr->magic, sizeof(struct kv_hdr_data) - KV_MAGIC_OFFSET, false);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -728,7 +728,7 @@ static fdb_err_t format_sector(fdb_kvdb_t db, uint32_t addr, uint32_t combined_v
|
||||
sec_hdr.combined = combined_value;
|
||||
sec_hdr.reserved = 0xFFFFFFFF;
|
||||
/* save the header */
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, (uint32_t *)&sec_hdr, sizeof(struct sector_hdr_data));
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, (uint32_t *)&sec_hdr, sizeof(struct sector_hdr_data), true);
|
||||
|
||||
#ifdef FDB_KV_USING_CACHE
|
||||
/* delete the sector cache */
|
||||
@@ -746,12 +746,12 @@ static fdb_err_t update_sec_status(fdb_kvdb_t db, kv_sec_info_t sector, size_t n
|
||||
/* change the current sector status */
|
||||
if (sector->status.store == FDB_SECTOR_STORE_EMPTY) {
|
||||
/* change the sector status to using */
|
||||
result = _fdb_write_status((fdb_db_t)db, sector->addr, status_table, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_USING);
|
||||
result = _fdb_write_status((fdb_db_t)db, sector->addr, status_table, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_USING, true);
|
||||
} else if (sector->status.store == FDB_SECTOR_STORE_USING) {
|
||||
/* check remain size */
|
||||
if (sector->remain < FDB_SEC_REMAIN_THRESHOLD || sector->remain - new_kv_len < FDB_SEC_REMAIN_THRESHOLD) {
|
||||
/* change the sector status to full */
|
||||
result = _fdb_write_status((fdb_db_t)db, sector->addr, status_table, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_FULL);
|
||||
result = _fdb_write_status((fdb_db_t)db, sector->addr, status_table, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_FULL, true);
|
||||
|
||||
#ifdef FDB_KV_USING_CACHE
|
||||
/* delete the sector cache */
|
||||
@@ -869,10 +869,10 @@ static fdb_err_t del_kv(fdb_kvdb_t db, const char *key, fdb_kv_t old_kv, bool co
|
||||
}
|
||||
/* change and save the new status */
|
||||
if (!complete_del) {
|
||||
result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_DELETE);
|
||||
result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_DELETE, false);
|
||||
db->last_is_complete_del = true;
|
||||
} else {
|
||||
result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_DELETED);
|
||||
result = _fdb_write_status((fdb_db_t)db, old_kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_DELETED, true);
|
||||
|
||||
if (!db->last_is_complete_del && result == FDB_NO_ERR) {
|
||||
#ifdef FDB_KV_USING_CACHE
|
||||
@@ -894,7 +894,7 @@ static fdb_err_t del_kv(fdb_kvdb_t db, const char *key, fdb_kv_t old_kv, bool co
|
||||
/* read and change the sector dirty status */
|
||||
if (result == FDB_NO_ERR
|
||||
&& _fdb_read_status((fdb_db_t)db, dirty_status_addr, status_table, FDB_SECTOR_DIRTY_STATUS_NUM) == FDB_SECTOR_DIRTY_FALSE) {
|
||||
result = _fdb_write_status((fdb_db_t)db, dirty_status_addr, status_table, FDB_SECTOR_DIRTY_STATUS_NUM, FDB_SECTOR_DIRTY_TRUE);
|
||||
result = _fdb_write_status((fdb_db_t)db, dirty_status_addr, status_table, FDB_SECTOR_DIRTY_STATUS_NUM, FDB_SECTOR_DIRTY_TRUE, true);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -938,7 +938,7 @@ static fdb_err_t move_kv(fdb_kvdb_t db, fdb_kv_t kv)
|
||||
/* update the new KV sector status first */
|
||||
update_sec_status(db, §or, kv->len, NULL);
|
||||
|
||||
_fdb_write_status((fdb_db_t)db, kv_addr, status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_WRITE);
|
||||
_fdb_write_status((fdb_db_t)db, kv_addr, status_table, FDB_KV_STATUS_NUM, FDB_KV_PRE_WRITE, false);
|
||||
kv_len -= KV_MAGIC_OFFSET;
|
||||
for (len = 0, size = 0; len < kv_len; len += size) {
|
||||
if (len + sizeof(buf) < kv_len) {
|
||||
@@ -947,9 +947,9 @@ static fdb_err_t move_kv(fdb_kvdb_t db, fdb_kv_t kv)
|
||||
size = kv_len - len;
|
||||
}
|
||||
_fdb_flash_read((fdb_db_t)db, kv->addr.start + KV_MAGIC_OFFSET + len, (uint32_t *) buf, FDB_WG_ALIGN(size));
|
||||
result = _fdb_flash_write((fdb_db_t)db, kv_addr + KV_MAGIC_OFFSET + len, (uint32_t *) buf, size);
|
||||
result = _fdb_flash_write((fdb_db_t)db, kv_addr + KV_MAGIC_OFFSET + len, (uint32_t *) buf, size, true);
|
||||
}
|
||||
_fdb_write_status((fdb_db_t)db, kv_addr, status_table, FDB_KV_STATUS_NUM, FDB_KV_WRITE);
|
||||
_fdb_write_status((fdb_db_t)db, kv_addr, status_table, FDB_KV_STATUS_NUM, FDB_KV_WRITE, true);
|
||||
|
||||
#ifdef FDB_KV_USING_CACHE
|
||||
update_sector_cache(db, FDB_ALIGN_DOWN(kv_addr, db_sec_size(db)),
|
||||
@@ -1010,7 +1010,7 @@ static bool do_gc(kv_sec_info_t sector, void *arg1, void *arg2)
|
||||
if (sector->check_ok && (sector->status.dirty == FDB_SECTOR_DIRTY_TRUE || sector->status.dirty == FDB_SECTOR_DIRTY_GC)) {
|
||||
uint8_t status_table[FDB_DIRTY_STATUS_TABLE_SIZE];
|
||||
/* change the sector status to GC */
|
||||
_fdb_write_status((fdb_db_t)db, sector->addr + SECTOR_DIRTY_OFFSET, status_table, FDB_SECTOR_DIRTY_STATUS_NUM, FDB_SECTOR_DIRTY_GC);
|
||||
_fdb_write_status((fdb_db_t)db, sector->addr + SECTOR_DIRTY_OFFSET, status_table, FDB_SECTOR_DIRTY_STATUS_NUM, FDB_SECTOR_DIRTY_GC, true);
|
||||
/* search all KV */
|
||||
kv.addr.start = sector->addr + SECTOR_HDR_DATA_SIZE;
|
||||
do {
|
||||
@@ -1066,12 +1066,12 @@ static fdb_err_t align_write(fdb_kvdb_t db, uint32_t addr, const uint32_t *buf,
|
||||
#endif
|
||||
|
||||
memset(align_data, 0xFF, align_data_size);
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, buf, FDB_WG_ALIGN_DOWN(size));
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, buf, FDB_WG_ALIGN_DOWN(size), false);
|
||||
|
||||
align_remain = size - FDB_WG_ALIGN_DOWN(size);
|
||||
if (result == FDB_NO_ERR && align_remain) {
|
||||
memcpy(align_data, (uint8_t *)buf + FDB_WG_ALIGN_DOWN(size), align_remain);
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr + FDB_WG_ALIGN_DOWN(size), (uint32_t *) align_data, align_data_size);
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr + FDB_WG_ALIGN_DOWN(size), (uint32_t *) align_data, align_data_size, false);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -1143,7 +1143,7 @@ static fdb_err_t create_kv_blob(fdb_kvdb_t db, kv_sec_info_t sector, const char
|
||||
}
|
||||
/* change the KV status to KV_WRITE */
|
||||
if (result == FDB_NO_ERR) {
|
||||
result = _fdb_write_status((fdb_db_t)db, kv_addr, kv_hdr.status_table, FDB_KV_STATUS_NUM, FDB_KV_WRITE);
|
||||
result = _fdb_write_status((fdb_db_t)db, kv_addr, kv_hdr.status_table, FDB_KV_STATUS_NUM, FDB_KV_WRITE, true);
|
||||
}
|
||||
/* trigger GC collect when current sector is full */
|
||||
if (result == FDB_NO_ERR && is_full) {
|
||||
@@ -1474,7 +1474,7 @@ static bool check_and_recovery_kv_cb(fdb_kv_t kv, void *arg1, void *arg2)
|
||||
uint8_t status_table[KV_STATUS_TABLE_SIZE];
|
||||
/* the KV has not write finish, change the status to error */
|
||||
//TODO 绘制异常处理的状态装换图
|
||||
_fdb_write_status((fdb_db_t)db, kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_ERR_HDR);
|
||||
_fdb_write_status((fdb_db_t)db, kv->addr.start, status_table, FDB_KV_STATUS_NUM, FDB_KV_ERR_HDR, true);
|
||||
return true;
|
||||
} else if (kv->crc_is_ok && kv->status == FDB_KV_WRITE) {
|
||||
/* update the cache when first load */
|
||||
|
||||
+22
-22
@@ -60,15 +60,15 @@
|
||||
if (((fdb_db_t)db)->unlock) ((fdb_db_t)db)->unlock((fdb_db_t)db); \
|
||||
} while(0);
|
||||
|
||||
#define _FDB_WRITE_STATUS(db, addr, status_table, status_num, status_index) \
|
||||
#define _FDB_WRITE_STATUS(db, addr, status_table, status_num, status_index, sync) \
|
||||
do { \
|
||||
result = _fdb_write_status((fdb_db_t)db, addr, status_table, status_num, status_index);\
|
||||
result = _fdb_write_status((fdb_db_t)db, addr, status_table, status_num, status_index, sync);\
|
||||
if (result != FDB_NO_ERR) return result; \
|
||||
} while(0);
|
||||
|
||||
#define FLASH_WRITE(db, addr, buf, size) \
|
||||
#define FLASH_WRITE(db, addr, buf, size, sync) \
|
||||
do { \
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, buf, size); \
|
||||
result = _fdb_flash_write((fdb_db_t)db, addr, buf, size, sync); \
|
||||
if (result != FDB_NO_ERR) return result; \
|
||||
} while(0);
|
||||
|
||||
@@ -232,10 +232,10 @@ static fdb_err_t format_sector(fdb_tsdb_t db, uint32_t addr)
|
||||
|
||||
result = _fdb_flash_erase((fdb_db_t)db, addr, db_sec_size(db));
|
||||
if (result == FDB_NO_ERR) {
|
||||
_FDB_WRITE_STATUS(db, addr, sec_hdr.status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_EMPTY);
|
||||
_FDB_WRITE_STATUS(db, addr, sec_hdr.status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_EMPTY, true);
|
||||
/* set the magic */
|
||||
sec_hdr.magic = SECTOR_MAGIC_WORD;
|
||||
FLASH_WRITE(db, addr + SECTOR_MAGIC_OFFSET, &sec_hdr.magic, sizeof(sec_hdr.magic));
|
||||
FLASH_WRITE(db, addr + SECTOR_MAGIC_OFFSET, &sec_hdr.magic, sizeof(sec_hdr.magic), true);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -272,13 +272,13 @@ static fdb_err_t write_tsl(fdb_tsdb_t db, fdb_blob_t blob, fdb_time_t time)
|
||||
idx.time = time;
|
||||
idx.log_addr = db->cur_sec.empty_data - FDB_WG_ALIGN(idx.log_len);
|
||||
/* write the status will by write granularity */
|
||||
_FDB_WRITE_STATUS(db, idx_addr, idx.status_table, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE);
|
||||
_FDB_WRITE_STATUS(db, idx_addr, idx.status_table, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE, false);
|
||||
/* write other index info */
|
||||
FLASH_WRITE(db, idx_addr + LOG_IDX_TS_OFFSET, &idx.time, sizeof(struct log_idx_data) - LOG_IDX_TS_OFFSET);
|
||||
FLASH_WRITE(db, idx_addr + LOG_IDX_TS_OFFSET, &idx.time, sizeof(struct log_idx_data) - LOG_IDX_TS_OFFSET, false);
|
||||
/* write blob data */
|
||||
FLASH_WRITE(db, idx.log_addr, blob->buf, blob->size);
|
||||
FLASH_WRITE(db, idx.log_addr, blob->buf, blob->size, false);
|
||||
/* write the status will by write granularity */
|
||||
_FDB_WRITE_STATUS(db, idx_addr, idx.status_table, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE);
|
||||
_FDB_WRITE_STATUS(db, idx_addr, idx.status_table, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -293,18 +293,18 @@ static fdb_err_t update_sec_status(fdb_tsdb_t db, tsdb_sec_info_t sector, fdb_bl
|
||||
uint32_t end_index = sector->empty_idx - LOG_IDX_DATA_SIZE, new_sec_addr, cur_sec_addr = sector->addr;
|
||||
/* save the end node index and timestamp */
|
||||
if (sector->end_info_stat[0] == FDB_TSL_UNUSED) {
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END0_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_TIME_OFFSET, (uint32_t * )&db->last_time, sizeof(fdb_time_t));
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_IDX_OFFSET, &end_index, sizeof(end_index));
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END0_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE);
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END0_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE, false);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_TIME_OFFSET, (uint32_t * )&db->last_time, sizeof(fdb_time_t), false);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END0_IDX_OFFSET, &end_index, sizeof(end_index), false);
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END0_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE, true);
|
||||
} else if (sector->end_info_stat[1] == FDB_TSL_UNUSED) {
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END1_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_TIME_OFFSET, (uint32_t * )&db->last_time, sizeof(fdb_time_t));
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_IDX_OFFSET, &end_index, sizeof(end_index));
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END1_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE);
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END1_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_PRE_WRITE, false);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_TIME_OFFSET, (uint32_t * )&db->last_time, sizeof(fdb_time_t), false);
|
||||
FLASH_WRITE(db, cur_sec_addr + SECTOR_END1_IDX_OFFSET, &end_index, sizeof(end_index), false);
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr + SECTOR_END1_STATUS_OFFSET, end_status, FDB_TSL_STATUS_NUM, FDB_TSL_WRITE, true);
|
||||
}
|
||||
/* change current sector to full */
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr, status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_FULL);
|
||||
_FDB_WRITE_STATUS(db, cur_sec_addr, status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_FULL, true);
|
||||
/* calculate next sector address */
|
||||
if (sector->addr + db_sec_size(db) < db_max_size(db)) {
|
||||
new_sec_addr = sector->addr + db_sec_size(db);
|
||||
@@ -332,9 +332,9 @@ static fdb_err_t update_sec_status(fdb_tsdb_t db, tsdb_sec_info_t sector, fdb_bl
|
||||
/* change the sector to using */
|
||||
sector->status = FDB_SECTOR_STORE_USING;
|
||||
sector->start_time = cur_time;
|
||||
_FDB_WRITE_STATUS(db, sector->addr, status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_USING);
|
||||
_FDB_WRITE_STATUS(db, sector->addr, status, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_USING, true);
|
||||
/* save the start timestamp */
|
||||
FLASH_WRITE(db, sector->addr + SECTOR_START_TIME_OFFSET, (uint32_t *)&cur_time, sizeof(fdb_time_t));
|
||||
FLASH_WRITE(db, sector->addr + SECTOR_START_TIME_OFFSET, (uint32_t *)&cur_time, sizeof(fdb_time_t), true);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -571,7 +571,7 @@ fdb_err_t fdb_tsl_set_status(fdb_tsdb_t db, fdb_tsl_t tsl, fdb_tsl_status_t stat
|
||||
uint8_t status_table[TSL_STATUS_TABLE_SIZE];
|
||||
|
||||
/* write the status will by write granularity */
|
||||
_FDB_WRITE_STATUS(db, tsl->addr.index, status_table, FDB_TSL_STATUS_NUM, status);
|
||||
_FDB_WRITE_STATUS(db, tsl->addr.index, status_table, FDB_TSL_STATUS_NUM, status, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+6
-6
@@ -133,7 +133,7 @@ size_t _fdb_get_status(uint8_t status_table[], size_t status_num)
|
||||
return status_num_bak - i;
|
||||
}
|
||||
|
||||
fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index)
|
||||
fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index, bool sync)
|
||||
{
|
||||
fdb_err_t result = FDB_NO_ERR;
|
||||
size_t byte_index;
|
||||
@@ -149,11 +149,11 @@ fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[],
|
||||
return FDB_NO_ERR;
|
||||
}
|
||||
#if (FDB_WRITE_GRAN == 1)
|
||||
result = _fdb_flash_write(db, addr + byte_index, (uint32_t *)&status_table[byte_index], 1);
|
||||
result = _fdb_flash_write(db, addr + byte_index, (uint32_t *)&status_table[byte_index], 1, sync);
|
||||
#else /* (FDB_WRITE_GRAN == 8) || (FDB_WRITE_GRAN == 32) || (FDB_WRITE_GRAN == 64) */
|
||||
/* write the status by write granularity
|
||||
* some flash (like stm32 onchip) NOT supported repeated write before erase */
|
||||
result = _fdb_flash_write(db, addr + byte_index, (uint32_t *) &status_table[byte_index], FDB_WRITE_GRAN / 8);
|
||||
result = _fdb_flash_write(db, addr + byte_index, (uint32_t *) &status_table[byte_index], FDB_WRITE_GRAN / 8, sync);
|
||||
#endif /* FDB_WRITE_GRAN == 1 */
|
||||
|
||||
return result;
|
||||
@@ -237,7 +237,7 @@ size_t fdb_blob_read(fdb_db_t db, fdb_blob_t blob)
|
||||
|
||||
#ifdef FDB_USING_FILE_MODE
|
||||
extern fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size);
|
||||
extern fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size);
|
||||
extern fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync);
|
||||
extern fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size);
|
||||
#endif /* FDB_USING_FILE_LIBC */
|
||||
|
||||
@@ -281,13 +281,13 @@ fdb_err_t _fdb_flash_erase(fdb_db_t db, uint32_t addr, size_t size)
|
||||
return result;
|
||||
}
|
||||
|
||||
fdb_err_t _fdb_flash_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size)
|
||||
fdb_err_t _fdb_flash_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync)
|
||||
{
|
||||
fdb_err_t result = FDB_NO_ERR;
|
||||
|
||||
if (db->file_mode) {
|
||||
#ifdef FDB_USING_FILE_MODE
|
||||
return _fdb_file_write(db, addr, buf, size);
|
||||
return _fdb_file_write(db, addr, buf, size, sync);
|
||||
#else
|
||||
return FDB_READ_ERR;
|
||||
#endif /* FDB_USING_FILE_MODE */
|
||||
|
||||
Reference in New Issue
Block a user