From d10bcdf3999b9b9baf235135dbc13a8e7b878d2e Mon Sep 17 00:00:00 2001 From: lyon Date: Sun, 14 Nov 2021 10:43:15 +0800 Subject: [PATCH] use memcpy in args_getStruct --- port/linux/test/args-test.cpp | 16 ++++++++++++++++ src/PikaPlatform.c | 14 +++++++------- src/dataArgs.c | 12 +++++++----- src/dataArgs.h | 2 +- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/port/linux/test/args-test.cpp b/port/linux/test/args-test.cpp index 207ffd5eb..c0d81e96a 100644 --- a/port/linux/test/args-test.cpp +++ b/port/linux/test/args-test.cpp @@ -145,6 +145,22 @@ TEST(args, test12) { EXPECT_EQ(pikaMemNow(), 0); } +struct test { + int a; + float b; +}; +TEST(args, struct_) { + Args* args = New_args(NULL); + struct test test_struct = {1, 2.2}; + args_setStruct(args, (char*)"test", &test_struct, sizeof(struct test)); + struct test test_struct_out; + args_getStruct(args, (char*)"test", &test_struct_out); + EXPECT_EQ(test_struct_out.a, 1); + EXPECT_FLOAT_EQ(test_struct_out.b, 2.2); + args_deinit(args); + EXPECT_EQ(pikaMemNow(), 0); +} + TEST(args, mem) { EXPECT_EQ(pikaMemNow(), mem); EXPECT_EQ(pikaMemNow(), 0); diff --git a/src/PikaPlatform.c b/src/PikaPlatform.c index 44c3678f1..a7301d61f 100644 --- a/src/PikaPlatform.c +++ b/src/PikaPlatform.c @@ -1,8 +1,8 @@ #include "PikaPlatform.h" -#include -#include #include +#include #include +#include PIKA_WEAK void __platformDisableIrqHandle() { /* disable irq to support thread */ @@ -17,7 +17,7 @@ PIKA_WEAK void* __platformMalloc(size_t size) { } PIKA_WEAK void __platformFree(void* ptr) { - return free(ptr); + free(ptr); } PIKA_WEAK void __platformPrintf(char* fmt, ...) { @@ -28,21 +28,21 @@ PIKA_WEAK void __platformPrintf(char* fmt, ...) { va_end(args); } -PIKA_WEAK char* __platformLoadPikaAsm(){ +PIKA_WEAK char* __platformLoadPikaAsm() { /* faild */ return NULL; } -PIKA_WEAK int32_t __platformSavePikaAsm(char *PikaAsm){ +PIKA_WEAK int32_t __platformSavePikaAsm(char* PikaAsm) { /* faild */ return 1; } -PIKA_WEAK uint8_t __platformAsmIsToFlash(char *pyMultiLine){ +PIKA_WEAK uint8_t __platformAsmIsToFlash(char* pyMultiLine) { /* not to flash */ return 0; } -PIKA_WEAK int32_t __platformSavePikaAsmEOF(){ +PIKA_WEAK int32_t __platformSavePikaAsmEOF() { return 1; } diff --git a/src/dataArgs.c b/src/dataArgs.c index 972d30cec..a80a278a0 100644 --- a/src/dataArgs.c +++ b/src/dataArgs.c @@ -129,14 +129,16 @@ int32_t args_setStruct(Args* self, if (NULL == struct_ptr) { return 1; } - Arg* structArg = arg_setContent(NULL, (uint8_t*)struct_ptr, struct_size); - structArg = arg_setName(structArg, name); - args_setArg(self, structArg); + Arg* struct_arg = arg_setContent(NULL, (uint8_t*)struct_ptr, struct_size); + struct_arg = arg_setName(struct_arg, name); + args_setArg(self, struct_arg); return 0; } -void* args_getStruct(Args* self, char* name) { - return arg_getContent(args_getArg(self, name)); +void* args_getStruct(Args* self, char* name, void* struct_out) { + Arg* struct_arg = args_getArg(self, name); + uint32_t struct_size = arg_getContentSize(struct_arg); + return memcpy(struct_out, arg_getContent(struct_arg), struct_size); } int32_t args_copyArgByName(Args* self, char* name, Args* directArgs) { diff --git a/src/dataArgs.h b/src/dataArgs.h index 656670476..cf7f1ccd2 100644 --- a/src/dataArgs.h +++ b/src/dataArgs.h @@ -57,7 +57,7 @@ int32_t args_setStruct(Args* self, char* name, void* struct_ptr, uint32_t struct_size); -void* args_getStruct(Args* self, char* name); +void* args_getStruct(Args* self, char* name, void* struct_out); int32_t args_set(Args* self, char* name, char* valueStr); int32_t args_setObjectWithClass(Args* self,