From 0278a9d3fbe1fd7fc4c10af41279de6ad71500e3 Mon Sep 17 00:00:00 2001 From: Gabriel Wang Date: Mon, 24 Jan 2022 19:50:53 +0000 Subject: [PATCH] move type to __arg --- bsp/pico/main.c | 4 +--- src/PikaObj.c | 2 +- src/dataArg.c | 30 +++++++++++++----------------- src/dataArg.h | 11 +++-------- src/dataMemory.c | 9 +++++++++ 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/bsp/pico/main.c b/bsp/pico/main.c index fd7bda0ea..ac3819b10 100644 --- a/bsp/pico/main.c +++ b/bsp/pico/main.c @@ -131,9 +131,7 @@ int main(void) uint32_t n = 0; /* run unit test */ obj_deinit(pikaScriptInit()); - - - + /* benchmark */ uint64_t nCycleUsed_c,nCycleUsed_pika = 0; printf("[----------benchmark----------]\r\n"); diff --git a/src/PikaObj.c b/src/PikaObj.c index e00b0e4c1..71c1f27c9 100644 --- a/src/PikaObj.c +++ b/src/PikaObj.c @@ -390,7 +390,7 @@ PikaObj* obj_getObjDirect(PikaObj* self, char* name) { } PikaObj* obj_getObj(PikaObj* self, char* objPath, int32_t keepDeepth) { - char objPath_buff[64]; + static char objPath_buff[64]; __platform_memcpy(objPath_buff, objPath, sizeof(objPath_buff)); char token_buff[32] = {0}; int32_t token_num = strGetTokenNum(objPath, '.'); diff --git a/src/dataArg.c b/src/dataArg.c index 867628824..9951fe511 100644 --- a/src/dataArg.c +++ b/src/dataArg.c @@ -67,16 +67,14 @@ uint8_t* content_init_hash(Hash nameHash, uint8_t* content, uint32_t size, uint8_t* next) { - const uint8_t nextLength = sizeof(uint8_t*); - const uint8_t sizeLength = sizeof(uint32_t); - uint16_t nameSize = sizeof(Hash); // use hash + uint16_t typeSize = sizeof(ArgType); // use enum - __arg * self = (__arg *)pikaMalloc(nextLength + sizeLength + nameSize + - size + typeSize); + __arg * self = (__arg *)pikaMalloc(sizeof(__arg) + size + typeSize); self->next = (__arg *)next; self->size = size; self->name_hash = nameHash; + self->type = type; if (NULL != content) { __platform_memcpy(self->content, content, size); @@ -84,6 +82,9 @@ uint8_t* content_init_hash(Hash nameHash, __platform_memset(self->content, 0, size); } + /*! Todo: Why remove this cause problem? no API read value from this location + *! please refer to content_setType and content_getType + */ (*(ArgType *)((uintptr_t)(self->content) + size)) = type; return (uint8_t *)self; @@ -150,11 +151,16 @@ uint8_t* content_setType(uint8_t* self, ArgType type) { } __arg * arg = (__arg *)self; - (*(ArgType *)((uintptr_t)(arg->content) + arg->size)) = type; + arg->type = type; return self; } +ArgType content_getType(uint8_t* self) { + __arg * arg = (__arg *)self; + return arg->type; +} + Arg* arg_newContent(Arg* self, uint32_t size) { uint8_t* newContent = content_init("", TYPE_NONE, NULL, size, NULL); arg_freeContent(self); @@ -177,10 +183,7 @@ Arg* arg_setType(Arg* self, ArgType type) { return content_setType(self, type); } -ArgType content_getType(uint8_t* self) { - __arg * arg = (__arg *)self; - return (*(ArgType *)((uintptr_t)(arg->content) + arg->size)); -} + @@ -236,13 +239,6 @@ char* arg_getStr(Arg* self) { return (char*)arg_getContent(self); } -uint16_t content_typeOffset(uint8_t* self) { - const uint8_t nextLength = sizeof(uint8_t*); - const uint8_t sizeLength = sizeof(uint32_t); - uint16_t size = content_getSize(self); - uint16_t nameSize = sizeof(Hash); - return nextLength + sizeLength + nameSize + size; -} Hash arg_getNameHash(Arg* self) { if (NULL == self) { diff --git a/src/dataArg.h b/src/dataArg.h index 6aa4108b0..e23e88587 100644 --- a/src/dataArg.h +++ b/src/dataArg.h @@ -47,19 +47,14 @@ typedef struct __arg __arg; struct __arg { __arg *next; - uint32_t size; + uint16_t size; + uint8_t type; + uint8_t : 8; Hash name_hash; uint8_t content[]; }; -uint16_t content_typeOffset(uint8_t* content); - -#define content_contentOffset(...) offsetof(__arg, content) -#define content_sizeOffset(...) offsetof(__arg, size) -#define content_nextOffset(...) offsetof(__arg, next) -#define content_nameOffset(...) offsetof(__arg, name_hash) - //uint32_t content_getNameHash(uint8_t* content); #define content_getNameHash(__addr) (((__arg *)(__addr))->name_hash) diff --git a/src/dataMemory.c b/src/dataMemory.c index 4ce7ce669..486cdde86 100644 --- a/src/dataMemory.c +++ b/src/dataMemory.c @@ -35,6 +35,15 @@ void* pikaMalloc(uint32_t size) { if (0 != __is_locked_pikaMemory()) { __platform_wait(); } + +//! if you unsure about the __impl_pikaMalloc, uncomment this to force alignment +#if 0 + /* force alignment to avoid unaligned access */ + if (sizeof(int_fast8_t) > 1) { + size = (size + sizeof(int_fast8_t) - 1) & ~(sizeof(int_fast8_t) - 1); + } +#endif + pikaMemInfo.heapUsed += size; if (pikaMemInfo.heapUsedMax < pikaMemInfo.heapUsed) { pikaMemInfo.heapUsedMax = pikaMemInfo.heapUsed;