finish test and fix bugs
This commit is contained in:
parent
1f6eb936bf
commit
8dd824e5d8
4
Inc/fs.h
4
Inc/fs.h
|
@ -71,8 +71,8 @@
|
|||
int fs_init();
|
||||
int fs_commit();
|
||||
|
||||
uint32_t* fs_find_free_entry(uint32_t page, int page_count, int entry_size);
|
||||
uint32_t* fs_find_last_entry(uint32_t page, int page_count, int entry_size);
|
||||
uint32_t* fs_find_free_entry(uint32_t page_num, int page_count, int entry_size);
|
||||
uint32_t* fs_find_last_entry(uint32_t page_num, int page_count, int entry_size);
|
||||
uint32_t* fs_swap_get_free();
|
||||
uint32_t* fs_cache_get_free(uint32_t cache_start, int page_count, int entry_size);
|
||||
|
||||
|
|
43
Src/fs.c
43
Src/fs.c
|
@ -183,11 +183,8 @@ ret:
|
|||
return res;
|
||||
}
|
||||
|
||||
uint32_t* fs_find_free_entry(uint32_t page_id, int page_count, int entry_size) {
|
||||
uint32_t* page = FS_PAGE_ADDR(page_id);
|
||||
int size = page_count * (FLASH_PAGE_SIZE/4);
|
||||
|
||||
for (int i = 2; i < size; i += (entry_size/4)) {
|
||||
uint32_t* _fs_find_free_entry(uint32_t* page, int entry_size) {
|
||||
for (int i = 2; i < (FLASH_PAGE_SIZE/4); i += entry_size) {
|
||||
if (page[i] == FS_CLEAR_ENTRY) {
|
||||
return &page[i];
|
||||
}
|
||||
|
@ -196,11 +193,31 @@ uint32_t* fs_find_free_entry(uint32_t page_id, int page_count, int entry_size) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t* fs_find_last_entry(uint32_t page, int page_count, int entry_size) {
|
||||
uint32_t* free_addr = fs_find_free_entry(page, page_count, entry_size);
|
||||
uint32_t* fs_find_free_entry(uint32_t page_num, int page_count, int entry_size) {
|
||||
for (int i = 0; i < page_count; i++) {
|
||||
uint32_t* page = _fs_find_free_entry(FS_PAGE_IDX_ADDR(page_num, i), entry_size);
|
||||
|
||||
if (page) {
|
||||
return page;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t* fs_find_last_entry(uint32_t page_num, int page_count, int entry_size) {
|
||||
uint32_t* page = FS_PAGE_ADDR(page_num);
|
||||
|
||||
if (page[2] == FS_CLEAR_ENTRY) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t* free_addr = fs_find_free_entry(page_num, page_count, entry_size);
|
||||
|
||||
if (!free_addr) {
|
||||
free_addr = FS_PAGE_ADDR(page) + (page_count * (FLASH_PAGE_SIZE/4));
|
||||
free_addr = &page[(page_count * (FLASH_PAGE_SIZE/4))];
|
||||
} else if (!((((intptr_t) free_addr - (intptr_t) page) - 8) % FLASH_PAGE_SIZE)) {
|
||||
free_addr -= 2;
|
||||
}
|
||||
|
||||
return free_addr - entry_size;
|
||||
|
@ -237,9 +254,9 @@ uint32_t* fs_swap_get_free() {
|
|||
uint32_t * _fs_cache_free_oldest(uint32_t cache_start, int page_count) {
|
||||
uint32_t* addr = NULL;
|
||||
|
||||
int lowwc = -1;
|
||||
int highwc = 0x7fffffff;
|
||||
int page_idx = -1;
|
||||
uint32_t lowwc = 0xffffffff;
|
||||
uint32_t highwc = 0;
|
||||
uint32_t page_idx = 0;
|
||||
|
||||
for (int i = 0; i < page_count; i++) {
|
||||
uint32_t *page = FS_PAGE_IDX_ADDR(cache_start, i);
|
||||
|
@ -248,7 +265,7 @@ uint32_t * _fs_cache_free_oldest(uint32_t cache_start, int page_count) {
|
|||
} else if (page[1] < lowwc) {
|
||||
lowwc = page[1];
|
||||
addr = page;
|
||||
page_idx = 1;
|
||||
page_idx = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -264,7 +281,7 @@ uint32_t * _fs_cache_free_oldest(uint32_t cache_start, int page_count) {
|
|||
|
||||
ret:
|
||||
flash_lock();
|
||||
return addr;
|
||||
return &addr[2];
|
||||
}
|
||||
|
||||
uint32_t* fs_cache_get_free(uint32_t cache_start, int page_count, int entry_size) {
|
||||
|
|
|
@ -48,7 +48,7 @@ The first 8 bytes (a double word) are used for the page header. The header has t
|
|||
| INDEX | VERSION | TYPE | WRITE COUNT |
|
||||
|======================================================|
|
||||
|
||||
Basically the first word is a magic number, which encodes a page type (2 bytes), a version of the page type, needed if format changes (1 byte), the index of the page, needed if this kind of page is present on several pages (1 bytes). The other 32-bit word is the write count of this page, written as a little-endian signed integer (but negative count is not allowed).
|
||||
Basically the first word is a magic number, which encodes a page type (2 bytes), a version of the page type, needed if format changes (1 byte), the index of the page, needed if this kind of page is present on several pages (1 bytes). The other 32-bit word is the write count of this page, written as a little-endian unsigned integer.
|
||||
|
||||
When reading a page the header is verified for correctness. This is useful for detecting partial rewrites (more details will follow).
|
||||
|
||||
|
|
|
@ -147,13 +147,62 @@ void test_fs_commit(void) {
|
|||
void test_fs_find_free_entry(void) {
|
||||
TEST_CHECK(!fs_init());
|
||||
|
||||
TEST_CHECK(0);
|
||||
uint32_t* page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 0);
|
||||
TEST_CHECK(fs_find_free_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[2]);
|
||||
page[2] = 0;
|
||||
|
||||
TEST_CHECK(fs_find_free_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[12]);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 1);
|
||||
TEST_CHECK(fs_find_free_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[2]);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 2);
|
||||
|
||||
for (int i = 2; i < ((FLASH_PAGE_SIZE/4) - 2); i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
TEST_CHECK(fs_find_free_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 2) == &page[510]);
|
||||
TEST_CHECK(fs_find_free_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == NULL);
|
||||
}
|
||||
|
||||
void test_fs_find_last_entry(void) {
|
||||
TEST_CHECK(!fs_init());
|
||||
TEST_CHECK(fs_find_last_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == NULL);
|
||||
|
||||
TEST_CHECK(0);
|
||||
uint32_t* page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 0);
|
||||
page[2] = 0;
|
||||
TEST_CHECK(fs_find_last_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[2]);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
TEST_CHECK(fs_find_last_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[502]);
|
||||
page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 1);
|
||||
page[2] = 0;
|
||||
page[12] = 0;
|
||||
TEST_CHECK(fs_find_last_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[12]);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
page = FS_PAGE_IDX_ADDR(FS_SETTINGS_PAGE, 2);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
TEST_CHECK(fs_find_last_entry(FS_SETTINGS_PAGE, FS_SETTINGS_COUNT, 10) == &page[502]);
|
||||
}
|
||||
|
||||
void test_fs_swap_get_free(void) {
|
||||
|
@ -177,8 +226,28 @@ void test_fs_swap_get_free(void) {
|
|||
|
||||
void test_fs_cache_get_free(void) {
|
||||
TEST_CHECK(!fs_init());
|
||||
uint32_t* page;
|
||||
|
||||
TEST_CHECK(0);
|
||||
for(int i = 0; i < FS_KEY_CACHE_COUNT; i++) {
|
||||
page = FS_PAGE_IDX_ADDR(FS_KEY_CACHE_PAGE, i);
|
||||
for (int j = 2; j < FLASH_PAGE_SIZE/4; j++) {
|
||||
page[j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
page = FS_PAGE_IDX_ADDR(FS_KEY_CACHE_PAGE, 0);
|
||||
TEST_CHECK(fs_cache_get_free(FS_KEY_CACHE_PAGE, FS_KEY_CACHE_COUNT, 34) == &page[2]);
|
||||
TEST_CHECK(page[0] == FS_V1_PAGE_ID(FS_KEY_CACHE_ID, 0));
|
||||
TEST_CHECK(page[1] == FS_KEY_CACHE_COUNT);
|
||||
|
||||
for (int i = 2; i < FLASH_PAGE_SIZE/4; i++) {
|
||||
page[i] = 0;
|
||||
}
|
||||
|
||||
page = FS_PAGE_IDX_ADDR(FS_KEY_CACHE_PAGE, 1);
|
||||
TEST_CHECK(fs_cache_get_free(FS_KEY_CACHE_PAGE, FS_KEY_CACHE_COUNT, 34) == &page[2]);
|
||||
TEST_CHECK(page[0] == FS_V1_PAGE_ID(FS_KEY_CACHE_ID, 1));
|
||||
TEST_CHECK(page[1] == (FS_KEY_CACHE_COUNT + 1));
|
||||
}
|
||||
|
||||
TEST_LIST = {
|
||||
|
|
Loading…
Reference in New Issue