From eea7dbd0fc22e24f019c51d89fd8f8ef550b5db1 Mon Sep 17 00:00:00 2001 From: lyon1998 Date: Wed, 6 Dec 2023 23:57:48 +0800 Subject: [PATCH] fix mem lack, get_blob for default_kvs not work --- examples/flashdb/flashdb_utest.py | 1 - package/flashdb/_flashdb_FlashDB.c | 30 ++++++++++--------- package/flashdb/fdb.c | 2 +- package/flashdb/fdb_cfg.h | 6 ++-- package/flashdb/fdb_def.h | 11 +++++++ package/flashdb/fdb_file.c | 26 ++++++++-------- package/flashdb/flashdb.py | 2 +- port/linux/.vscode/launch.json | 3 +- port/linux/package/pikascript/flashdb.py | 2 +- .../pikascript-lib/flashdb/_flashdb_FlashDB.c | 11 ++++--- .../pikascript-lib/flashdb/fdb_cfg.h | 2 +- port/linux/test/module-test.cpp | 3 ++ .../test/python/flashdb/flashdb_utest.py | 1 - src/PikaPlatform.c | 19 ++++++------ src/PikaVersion.h | 2 +- 15 files changed, 71 insertions(+), 50 deletions(-) diff --git a/examples/flashdb/flashdb_utest.py b/examples/flashdb/flashdb_utest.py index 91f57b563..6b13c52e6 100644 --- a/examples/flashdb/flashdb_utest.py +++ b/examples/flashdb/flashdb_utest.py @@ -25,7 +25,6 @@ def test_boot_count1(self): } #print(default_kv) kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None) - return res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob)) self.assertIsNotNone(res) diff --git a/package/flashdb/_flashdb_FlashDB.c b/package/flashdb/_flashdb_FlashDB.c index ed15fb898..639e53b3c 100644 --- a/package/flashdb/_flashdb_FlashDB.c +++ b/package/flashdb/_flashdb_FlashDB.c @@ -1,15 +1,13 @@ #include "_flashdb_FlashDB.h" #include +#include "_flashdb_kvdb_t.h" #include "flashdb.h" #include "pikaScript.h" -#include "_flashdb_kvdb_t.h" -//#include "fdb_def.h" +// #include "fdb_def.h" #define PIKA_USING_FLASHDB1 1 #if PIKA_USING_FLASHDB1 -//#include -#include -#include +// #include #include "flashdb.h" #define FDB_LOG_TAG "[main]" @@ -40,7 +38,8 @@ static void unlock(fdb_db_t db) } */ static fdb_time_t get_time(void) { - return time(NULL); + // ms to s + return pika_platform_get_tick() / 1000; } #endif @@ -81,13 +80,13 @@ PikaObj* _flashdb_FlashDB_kv_get_blob(PikaObj* self, blob.size = size; uint8_t* buf = (uint8_t*)pikaMalloc(size + 1); if (!buf) { - printf("alloc fail\n"); + pika_platform_printf("alloc fail\n"); return NULL; } blob.buf = buf; size_t len = fdb_kv_get_blob(kvdb, key, &blob); if (len != size) { - printf("size error\n"); + pika_platform_printf("size error\n"); pikaFree(buf, size + 1); return NULL; } @@ -129,7 +128,7 @@ int _flashdb_FlashDB_kv_set_blob(PikaObj* self, ArgType argt_blob_in = arg_getType(blob_in); if (argt_blob_in != ARG_TYPE_BYTES) { - printf("blob must be bytes but got:%d", argt_blob_in); + pika_platform_printf("blob must be bytes but got:%d", argt_blob_in); } size_t len = arg_getBytesSize(blob_in); uint8_t* bytes = (uint8_t*)arg_getBytes(blob_in); @@ -187,9 +186,9 @@ int32_t _flashdb_foreach(PikaObj* self_dict, memcpy(pbytes, bytes, bytes_size); /* for (size_t i=0; i < bytes_size; i++) { - printf("%02x", bytes[i]); + pika_platform_printf("%02x", bytes[i]); } - printf("\n"); + pika_platform_printf("\n"); */ def_kv_table[g_def_kv_table_idx].key = strdup(key); def_kv_table[g_def_kv_table_idx].value = pbytes; @@ -204,7 +203,7 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self, char* path, PikaObj* default_kv_in, Arg* user_data) { - printf("kvdb_init \n"); + pika_platform_printf("kvdb_init \n"); fdb_err_t result; if (!g_kvdb_path_inited) { @@ -216,7 +215,7 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self, /* enable file mode */ fdb_kvdb_control(&g_kvdb, FDB_KVDB_CTRL_SET_FILE_MODE, &file_mode); /* create database directory */ - mkdir(path, 0777); + pika_platform_mkdir(path, 0777); g_kvdb_path_inited = true; } // int len =pikaDict_getSize(default_kv_in); @@ -252,7 +251,10 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self, } void _flashdb_FlashDB___del__(PikaObj* self) { - args_deinit(_FDBBUFFS); + Args* buffs = _FDBBUFFS; + if (NULL != buffs) { + args_deinit(_FDBBUFFS); + } } #undef strudp diff --git a/package/flashdb/fdb.c b/package/flashdb/fdb.c index 099809771..f8ca44fce 100644 --- a/package/flashdb/fdb.c +++ b/package/flashdb/fdb.c @@ -141,7 +141,7 @@ void _fdb_deinit(fdb_db_t db) { } #else if (db->cur_file != 0) { - fclose(db->cur_file); + pika_platform_fclose(db->cur_file); } #endif /* FDB_USING_FILE_POSIX_MODE */ #endif /* FDB_USING_FILE_MODE */ diff --git a/package/flashdb/fdb_cfg.h b/package/flashdb/fdb_cfg.h index b100873ca..4128a7458 100644 --- a/package/flashdb/fdb_cfg.h +++ b/package/flashdb/fdb_cfg.h @@ -18,14 +18,16 @@ #ifdef FDB_USING_KVDB /* Auto update KV to latest default when current KVDB version number is changed. * @see fdb_kvdb.ver_num */ -/* #define FDB_KV_AUTO_UPDATE */ +#define FDB_KV_AUTO_UPDATE #endif /* using TSDB (Time series database) feature */ #define FDB_USING_TSDB /* Using file storage mode by POSIX file API, like open/read/write/close */ -#define FDB_USING_FILE_POSIX_MODE +// #define FDB_USING_FILE_POSIX_MODE + +#define FDB_USING_FILE_LIBC_MODE /* log print macro. default EF_PRINT macro is printf() */ /* #define FDB_PRINT(...) my_printf(__VA_ARGS__) */ diff --git a/package/flashdb/fdb_def.h b/package/flashdb/fdb_def.h index e8c05d039..84ba3638f 100644 --- a/package/flashdb/fdb_def.h +++ b/package/flashdb/fdb_def.h @@ -13,6 +13,17 @@ #define _FDB_DEF_H_ #include "PikaObj.h" +#ifndef bool +#define bool pika_bool +#endif + +#ifndef true +#define true pika_true +#endif + +#ifndef false +#define false pika_false +#endif #ifdef __cplusplus extern "C" { diff --git a/package/flashdb/fdb_file.c b/package/flashdb/fdb_file.c index 7e0a29e63..d319749e6 100644 --- a/package/flashdb/fdb_file.c +++ b/package/flashdb/fdb_file.c @@ -144,21 +144,21 @@ static FILE* open_db_file(fdb_db_t db, uint32_t addr, bool clean) { get_db_file_path(db, addr, path, DB_PATH_MAX); if (db->cur_file) { - fclose(db->cur_file); + pika_platform_fclose(db->cur_file); } if (clean) { /* clean the old file */ - db->cur_file = fopen(path, "wb+"); + db->cur_file = pika_platform_fopen(path, "wb+"); if (db->cur_file == NULL) { FDB_INFO("Error: open (%s) file failed.\n", path); } else { - fclose(db->cur_file); + pika_platform_fclose(db->cur_file); } } /* open the database file */ - db->cur_file = fopen(path, "rb+"); + db->cur_file = pika_platform_fopen(path, "rb+"); db->cur_sec = sec_addr; } @@ -170,8 +170,8 @@ fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void* buf, size_t size) { FILE* fp = open_db_file(db, addr, false); if (fp) { addr = addr % db->sec_size; - if ((fseek(fp, addr, SEEK_SET) != 0) || - (fread(buf, size, 1, fp) != size)) + if ((pika_platform_fseek(fp, addr, SEEK_SET) != 0) || + (pika_platform_fread(buf, size, 1, fp) != size)) result = FDB_READ_ERR; } else { result = FDB_READ_ERR; @@ -188,11 +188,11 @@ fdb_err_t _fdb_file_write(fdb_db_t db, FILE* fp = open_db_file(db, addr, false); if (fp) { addr = addr % db->sec_size; - if ((fseek(fp, addr, SEEK_SET) != 0) || - (fwrite(buf, size, 1, fp) != size)) + if ((pika_platform_fseek(fp, addr, SEEK_SET) != 0) || + (pika_platform_fwrite(buf, size, 1, fp) != size)) result = FDB_READ_ERR; if (sync) { - fflush(fp); + pika_platform_fflush(fp); } } else { result = FDB_READ_ERR; @@ -209,14 +209,14 @@ fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size) { #define BUF_SIZE 32 uint8_t buf[BUF_SIZE]; size_t i; - fseek(fp, 0, SEEK_SET); + pika_platform_fseek(fp, 0, SEEK_SET); for (i = 0; i * BUF_SIZE < size; i++) { memset(buf, 0xFF, BUF_SIZE); - fwrite(buf, BUF_SIZE, 1, fp); + pika_platform_fwrite(buf, BUF_SIZE, 1, fp); } memset(buf, 0xFF, BUF_SIZE); - fwrite(buf, size - i * BUF_SIZE, 1, fp); - fflush(fp); + pika_platform_fwrite(buf, size - i * BUF_SIZE, 1, fp); + pika_platform_fflush(fp); } else { result = FDB_ERASE_ERR; } diff --git a/package/flashdb/flashdb.py b/package/flashdb/flashdb.py index 667e44416..6fc09814d 100644 --- a/package/flashdb/flashdb.py +++ b/package/flashdb/flashdb.py @@ -6,7 +6,7 @@ class FDB(_flashdb.FlashDB): def kv_get_blob(self, kvdb, key, size): res = super().kv_get_blob(kvdb, key, size) - if type(res) == type([]): + if type(res) == list: return bytes(res) return None diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index 445404d23..ec5b7f78d 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -18,7 +18,8 @@ // "--gtest_filter=vm.run_file_subsrc" // "--gtest_filter=vm.run_file" // "--gtest_filter=stddata.encode_decode" - "--gtest_filter=packtool.packfiles_txt" + // "--gtest_filter=packtool.packfiles_txt" + "--gtest_filter=flashdb.*" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/package/pikascript/flashdb.py b/port/linux/package/pikascript/flashdb.py index 667e44416..6fc09814d 100644 --- a/port/linux/package/pikascript/flashdb.py +++ b/port/linux/package/pikascript/flashdb.py @@ -6,7 +6,7 @@ class FDB(_flashdb.FlashDB): def kv_get_blob(self, kvdb, key, size): res = super().kv_get_blob(kvdb, key, size) - if type(res) == type([]): + if type(res) == list: return bytes(res) return None diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c b/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c index 2917e5043..639e53b3c 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c @@ -1,13 +1,13 @@ #include "_flashdb_FlashDB.h" #include +#include "_flashdb_kvdb_t.h" #include "flashdb.h" #include "pikaScript.h" -#include "_flashdb_kvdb_t.h" -//#include "fdb_def.h" +// #include "fdb_def.h" #define PIKA_USING_FLASHDB1 1 #if PIKA_USING_FLASHDB1 -//#include +// #include #include "flashdb.h" #define FDB_LOG_TAG "[main]" @@ -251,7 +251,10 @@ PikaObj* _flashdb_FlashDB_kvdb_init(PikaObj* self, } void _flashdb_FlashDB___del__(PikaObj* self) { - args_deinit(_FDBBUFFS); + Args* buffs = _FDBBUFFS; + if (NULL != buffs) { + args_deinit(_FDBBUFFS); + } } #undef strudp diff --git a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_cfg.h b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_cfg.h index b62367fb7..4128a7458 100644 --- a/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_cfg.h +++ b/port/linux/package/pikascript/pikascript-lib/flashdb/fdb_cfg.h @@ -18,7 +18,7 @@ #ifdef FDB_USING_KVDB /* Auto update KV to latest default when current KVDB version number is changed. * @see fdb_kvdb.ver_num */ -/* #define FDB_KV_AUTO_UPDATE */ +#define FDB_KV_AUTO_UPDATE #endif /* using TSDB (Time series database) feature */ diff --git a/port/linux/test/module-test.cpp b/port/linux/test/module-test.cpp index b3b2c1b68..7bcc5ebe6 100644 --- a/port/linux/test/module-test.cpp +++ b/port/linux/test/module-test.cpp @@ -771,6 +771,9 @@ int test_flashdb(void) { /* run TSDB sample */ tsdb_sample(&tsdb); + + /* close TSDB */ + fdb_tsdb_deinit(&tsdb); } #endif /* FDB_USING_TSDB */ #else diff --git a/port/linux/test/python/flashdb/flashdb_utest.py b/port/linux/test/python/flashdb/flashdb_utest.py index 91f57b563..6b13c52e6 100644 --- a/port/linux/test/python/flashdb/flashdb_utest.py +++ b/port/linux/test/python/flashdb/flashdb_utest.py @@ -25,7 +25,6 @@ def test_boot_count1(self): } #print(default_kv) kvdb = fdb.kvdb_init("env", DB_PATH, default_kv, None) - return res = fdb.kv_get_blob(kvdb, "boot_count", len(boot_count_blob)) self.assertIsNotNone(res) diff --git a/src/PikaPlatform.c b/src/PikaPlatform.c index c66c8a0b8..0fff63e99 100644 --- a/src/PikaPlatform.c +++ b/src/PikaPlatform.c @@ -33,6 +33,10 @@ #include #endif +#if defined(_WIN32) +#include +#endif + #if defined(__linux) || PIKA_LINUX_COMPATIBLE #include #include @@ -40,6 +44,10 @@ #include "unistd.h" #endif +#if (defined(__linux) || PIKA_LINUX_COMPATIBLE) +#include +#endif + void pikaFree(void* mem, uint32_t size); void* pikaMalloc(uint32_t size); int pika_pvsprintf(char** buff, const char* fmt, va_list args); @@ -279,15 +287,8 @@ PIKA_WEAK long pika_platform_ftell(FILE* stream) { } PIKA_WEAK char* pika_platform_getcwd(char* buf, size_t size) { -#if defined(__linux) +#if defined(__linux) || defined(_WIN32) return getcwd(buf, size); -#elif defined(_WIN32) - if (!GetCurrentDirectoryA(size, buf)) { - return NULL; - } - else { - return buf; - } #else WEAK_FUNCTION_NEED_OVERRIDE_ERROR_LOWLEVEL(_); #endif @@ -478,7 +479,7 @@ PIKA_WEAK char** pika_platform_listdir(const char* path, int* count) { struct _finddata_t fb; intptr_t handle = 0; char dirpath[256] = {0}; - char* currentPath = pika_platform_getcwd(dirpath, sizeof dirpath); + char* currentPath = _getcwd(dirpath, 256); strcat(dirpath, path); strcat(dirpath, "\\*"); diff --git a/src/PikaVersion.h b/src/PikaVersion.h index 0ff726573..e744b9a36 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 13 #define PIKA_VERSION_MICRO 0 -#define PIKA_EDIT_TIME "2023/12/03 01:48:58" +#define PIKA_EDIT_TIME "2023/12/06 23:57:32"