From f2ea203e2ccef3f4683d568dc60fd46c5b51d39b Mon Sep 17 00:00:00 2001 From: Lyon Date: Sat, 6 Jul 2024 21:19:40 +0800 Subject: [PATCH] use port_mem port_vprintf for jrpc --- package/jrpc/jrpc.c | 14 ++++++++++---- package/jrpc/jrpc.h | 2 +- .../package/pikascript/pikascript-lib/jrpc/jrpc.c | 14 ++++++++++---- .../package/pikascript/pikascript-lib/jrpc/jrpc.h | 2 +- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/package/jrpc/jrpc.c b/package/jrpc/jrpc.c index 60d4aadee..b33cd34f9 100644 --- a/package/jrpc/jrpc.c +++ b/package/jrpc/jrpc.c @@ -31,9 +31,10 @@ #include // Function pointers for memory management -static char* (*port_mem_strdup)(const char* str) = strdup; +static void* (*port_mem_malloc)(size_t size) = malloc; static void (*port_mem_free)(void* ptr) = free; static int (*port_vprintf)(const char* format, va_list args) = vprintf; + static void jrpc_debug(const char* format, ...) { va_list args; va_start(args, format); @@ -44,9 +45,9 @@ static void jrpc_debug(const char* format, ...) { } // API to set memory management functions -void set_jrpc_memory_functions(char* (*strdup_func)(const char*), +void set_jrpc_memory_functions(void* (*malloc_func)(size_t), void (*free_func)(void*)) { - port_mem_strdup = strdup_func; + port_mem_malloc = malloc_func; port_mem_free = free_func; } @@ -55,7 +56,12 @@ void set_jrpc_vprintf_function(int (*vprintf_func)(const char*, va_list)) { } static char* jrpc_strdup(const char* str) { - return port_mem_strdup(str); + size_t len = strlen(str) + 1; + char* copy = (char*)port_mem_malloc(len); + if (copy) { + memcpy(copy, str, len); + } + return copy; } static void jrpc_free(void* ptr) { diff --git a/package/jrpc/jrpc.h b/package/jrpc/jrpc.h index 9d3e8bd42..c650bd890 100644 --- a/package/jrpc/jrpc.h +++ b/package/jrpc/jrpc.h @@ -113,7 +113,7 @@ cJSON* JRPC_receive_with_id_and_type(JRPC* self, int id, int type); int jrpc_validate_response(const char* expected_response); int jrpc_compare_json_strings(const char* json_str1, const char* json_str2); int jrpc_validate_response(const char* expected_response); -void set_jrpc_memory_functions(char* (*strdup_func)(const char*), +void set_jrpc_memory_functions(void* (*malloc_func)(size_t), void (*free_func)(void*)); void set_jrpc_vprintf_function(int (*vprintf_func)(const char*, va_list)); diff --git a/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.c b/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.c index 60d4aadee..b33cd34f9 100644 --- a/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.c +++ b/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.c @@ -31,9 +31,10 @@ #include // Function pointers for memory management -static char* (*port_mem_strdup)(const char* str) = strdup; +static void* (*port_mem_malloc)(size_t size) = malloc; static void (*port_mem_free)(void* ptr) = free; static int (*port_vprintf)(const char* format, va_list args) = vprintf; + static void jrpc_debug(const char* format, ...) { va_list args; va_start(args, format); @@ -44,9 +45,9 @@ static void jrpc_debug(const char* format, ...) { } // API to set memory management functions -void set_jrpc_memory_functions(char* (*strdup_func)(const char*), +void set_jrpc_memory_functions(void* (*malloc_func)(size_t), void (*free_func)(void*)) { - port_mem_strdup = strdup_func; + port_mem_malloc = malloc_func; port_mem_free = free_func; } @@ -55,7 +56,12 @@ void set_jrpc_vprintf_function(int (*vprintf_func)(const char*, va_list)) { } static char* jrpc_strdup(const char* str) { - return port_mem_strdup(str); + size_t len = strlen(str) + 1; + char* copy = (char*)port_mem_malloc(len); + if (copy) { + memcpy(copy, str, len); + } + return copy; } static void jrpc_free(void* ptr) { diff --git a/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.h b/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.h index 9d3e8bd42..c650bd890 100644 --- a/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.h +++ b/port/linux/package/pikascript/pikascript-lib/jrpc/jrpc.h @@ -113,7 +113,7 @@ cJSON* JRPC_receive_with_id_and_type(JRPC* self, int id, int type); int jrpc_validate_response(const char* expected_response); int jrpc_compare_json_strings(const char* json_str1, const char* json_str2); int jrpc_validate_response(const char* expected_response); -void set_jrpc_memory_functions(char* (*strdup_func)(const char*), +void set_jrpc_memory_functions(void* (*malloc_func)(size_t), void (*free_func)(void*)); void set_jrpc_vprintf_function(int (*vprintf_func)(const char*, va_list));