armink / FlashDB

An ultra-lightweight database that supports key-value and time series data | 一款支持 KV 数据和时序数据的超轻量级数据库
Apache License 2.0
1.71k stars 399 forks source link

[inc/fdb_low_lvl] fix An error occurred in FDB_ALIGN(size, align) #233

Closed DarryZh closed 11 months ago

DarryZh commented 11 months ago

An error occurred in FDB_ALIGN(size, align) when align is not 2 to the nth power.

The following is the minimal unit test code for the new implementation:

#include <stdio.h>
#define FDB_ALIGN(size, align)                      ((size + align -1) - ((size + align -1) % align))
void main(void)
{
    printf("FDB_ALIGN(0, 8) : %d\n", FDB_ALIGN(0, 8));
    printf("FDB_ALIGN(0, 10) : %d\n", FDB_ALIGN(0, 10));
    printf("FDB_ALIGN(10, 10) : %d\n", FDB_ALIGN(10, 10));
    printf("FDB_ALIGN(10, 20) : %d\n", FDB_ALIGN(10, 20));
    printf("FDB_ALIGN(20, 20) : %d\n", FDB_ALIGN(20, 20));
    printf("FDB_ALIGN(20, 10) : %d\n", FDB_ALIGN(20, 10));
    printf("FDB_ALIGN(10, 8) : %d\n", FDB_ALIGN(10, 8));
    printf("FDB_ALIGN(10, 7) : %d\n", FDB_ALIGN(10, 7));
    printf("FDB_ALIGN(7, 10) : %d\n", FDB_ALIGN(7, 10));
    printf("FDB_ALIGN(7, 20) : %d\n", FDB_ALIGN(7, 20));
    printf("FDB_ALIGN(32, 16) : %d\n", FDB_ALIGN(32, 16));
    printf("FDB_ALIGN(100, 16) : %d\n", FDB_ALIGN(100, 16));
    printf("FDB_ALIGN(100, 17) : %d\n", FDB_ALIGN(100, 17));
}

The following is the test results:

FDB_ALIGN(0, 8) : 0
FDB_ALIGN(0, 10) : 0
FDB_ALIGN(10, 10) : 10
FDB_ALIGN(10, 20) : 20
FDB_ALIGN(20, 20) : 20
FDB_ALIGN(20, 10) : 20
FDB_ALIGN(10, 8) : 16
FDB_ALIGN(10, 7) : 14
FDB_ALIGN(7, 10) : 10
FDB_ALIGN(7, 20) : 20
FDB_ALIGN(32, 16) : 32
FDB_ALIGN(100, 16) : 112
FDB_ALIGN(100, 17) : 102
armink commented 11 months ago

非常感谢的你的贡献,很不错的一个算法,666666

armink commented 11 months ago

也欢迎去给 RT-Thread 提交一个 PR 哈,我是从这里 copy 的 :>

https://github.com/RT-Thread/rt-thread/blob/master/include/rtdef.h#L425

image

DarryZh commented 11 months ago

有看到的,本来想参考一下rtthread,结果发现了FDB def的出处(狗头)

hsSam commented 10 months ago

这个更新后我原来正常使用的程序初始化后,读取数据报错了。

�[32;22m[I/FAL] Flash Abstraction Layer (V0.5.99) initialize success.�[0m [FlashDB] FlashDB V2.0.0 is initialize success. [FlashDB] You can get the latest version on https://github.com/armink/FlashDB . fdb_kvdb_init: 0 [FlashDB][kv][env][kvdb] Error: The KV (@0x00000014) CRC32 check failed!