Procházet zdrojové kódy

fix mem lack, get_blob for default_kvs not work

lyon1998 před 2 roky
rodič
revize
eea7dbd0fc

+ 0 - 1
examples/flashdb/flashdb_utest.py

@@ -25,7 +25,6 @@ class FlashDBUnitTest(unittest.TestCase):
         }
         #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)

+ 16 - 14
package/flashdb/_flashdb_FlashDB.c

@@ -1,15 +1,13 @@
 #include "_flashdb_FlashDB.h"
 #include <stdio.h>
+#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 <pthread.h>
-#include <sys/stat.h>
-#include <sys/types.h>
+// #include <pthread.h>
 #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

+ 1 - 1
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 */

+ 4 - 2
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__) */

+ 11 - 0
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" {

+ 13 - 13
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;
     }

+ 1 - 1
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
 

+ 2 - 1
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}",

+ 1 - 1
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
 

+ 7 - 4
port/linux/package/pikascript/pikascript-lib/flashdb/_flashdb_FlashDB.c

@@ -1,13 +1,13 @@
 #include "_flashdb_FlashDB.h"
 #include <stdio.h>
+#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 <pthread.h>
+// #include <pthread.h>
 #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

+ 1 - 1
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 */

+ 3 - 0
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

+ 0 - 1
port/linux/test/python/flashdb/flashdb_utest.py

@@ -25,7 +25,6 @@ class FlashDBUnitTest(unittest.TestCase):
         }
         #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)

+ 10 - 9
src/PikaPlatform.c

@@ -33,6 +33,10 @@
 #include <io.h>
 #endif
 
+#if defined(_WIN32)
+#include <dirent.h>
+#endif
+
 #if defined(__linux) || PIKA_LINUX_COMPATIBLE
 #include <dirent.h>
 #include <sys/stat.h>
@@ -40,6 +44,10 @@
 #include "unistd.h"
 #endif
 
+#if (defined(__linux) || PIKA_LINUX_COMPATIBLE)
+#include <dirent.h>
+#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, "\\*");
 

+ 1 - 1
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"