mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-29 17:22:56 +08:00
optimize speed for range
This commit is contained in:
parent
312e883ac9
commit
018a3d579f
@ -1,20 +1,14 @@
|
||||
#include "PikaObj.h"
|
||||
|
||||
Arg* PikaStdLib_RangeObj___next__(PikaObj* self) {
|
||||
int start = args_getInt(self->list, "_start");
|
||||
int end = args_getInt(self->list, "_end");
|
||||
int step = args_getInt(self->list, "_step");
|
||||
/* only start and end */
|
||||
/* start */
|
||||
int iter_i = args_getInt(self->list, "iter_i");
|
||||
/* iter_i is not inited */
|
||||
if (-999999999 == iter_i) {
|
||||
iter_i = start;
|
||||
}
|
||||
RangeData* _ = (RangeData*)args_getStruct(self->list, "_");
|
||||
int end = _->end;
|
||||
int step = _->step;
|
||||
/* exit */
|
||||
if (iter_i >= end) {
|
||||
if (_->i >= end) {
|
||||
return arg_newNull();
|
||||
}
|
||||
args_setInt(self->list, "iter_i", iter_i + step);
|
||||
return arg_newInt(iter_i);
|
||||
Arg* res = arg_newInt(_->i);
|
||||
_->i += step;
|
||||
return res;
|
||||
}
|
||||
|
@ -180,26 +180,29 @@ Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) {
|
||||
/* set template arg to create rangeObj */
|
||||
Arg* obj_arg = arg_newDirectObj(New_PikaStdLib_RangeObj);
|
||||
PikaObj* range_obj = arg_getPtr(obj_arg);
|
||||
RangeData range_data = {0};
|
||||
if (tuple_getSize(ax) == 1) {
|
||||
int start = 0;
|
||||
int end = arg_getInt(tuple_getArg(ax, 0));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", 1);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = 1;
|
||||
} else if (tuple_getSize(ax) == 2) {
|
||||
int start = arg_getInt(tuple_getArg(ax, 0));
|
||||
int end = arg_getInt(tuple_getArg(ax, 1));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", 1);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = 1;
|
||||
} else if (tuple_getSize(ax) == 3) {
|
||||
int start = arg_getInt(tuple_getArg(ax, 0));
|
||||
int end = arg_getInt(tuple_getArg(ax, 1));
|
||||
int step = arg_getInt(tuple_getArg(ax, 2));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", step);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = step;
|
||||
}
|
||||
range_data.i = range_data.start;
|
||||
obj_setStruct(range_obj, "_", range_data);
|
||||
return obj_arg;
|
||||
}
|
||||
|
||||
|
2
port/linux/.vscode/launch.json
vendored
2
port/linux/.vscode/launch.json
vendored
@ -11,7 +11,7 @@
|
||||
"program": "${workspaceFolder}/build/test/pikascript_test",
|
||||
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
|
||||
"args": [
|
||||
// "--gtest_filter=arg_test.*"
|
||||
"--gtest_filter=vm.range_1"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
|
@ -1,20 +1,14 @@
|
||||
#include "PikaObj.h"
|
||||
|
||||
Arg* PikaStdLib_RangeObj___next__(PikaObj* self) {
|
||||
int start = args_getInt(self->list, "_start");
|
||||
int end = args_getInt(self->list, "_end");
|
||||
int step = args_getInt(self->list, "_step");
|
||||
/* only start and end */
|
||||
/* start */
|
||||
int iter_i = args_getInt(self->list, "iter_i");
|
||||
/* iter_i is not inited */
|
||||
if (-999999999 == iter_i) {
|
||||
iter_i = start;
|
||||
}
|
||||
RangeData* _ = (RangeData*)args_getStruct(self->list, "_");
|
||||
int end = _->end;
|
||||
int step = _->step;
|
||||
/* exit */
|
||||
if (iter_i >= end) {
|
||||
if (_->i >= end) {
|
||||
return arg_newNull();
|
||||
}
|
||||
args_setInt(self->list, "iter_i", iter_i + step);
|
||||
return arg_newInt(iter_i);
|
||||
Arg* res = arg_newInt(_->i);
|
||||
_->i += step;
|
||||
return res;
|
||||
}
|
||||
|
@ -180,26 +180,29 @@ Arg* PikaStdLib_SysObj_range(PikaObj* self, PikaTuple* ax) {
|
||||
/* set template arg to create rangeObj */
|
||||
Arg* obj_arg = arg_newDirectObj(New_PikaStdLib_RangeObj);
|
||||
PikaObj* range_obj = arg_getPtr(obj_arg);
|
||||
RangeData range_data = {0};
|
||||
if (tuple_getSize(ax) == 1) {
|
||||
int start = 0;
|
||||
int end = arg_getInt(tuple_getArg(ax, 0));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", 1);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = 1;
|
||||
} else if (tuple_getSize(ax) == 2) {
|
||||
int start = arg_getInt(tuple_getArg(ax, 0));
|
||||
int end = arg_getInt(tuple_getArg(ax, 1));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", 1);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = 1;
|
||||
} else if (tuple_getSize(ax) == 3) {
|
||||
int start = arg_getInt(tuple_getArg(ax, 0));
|
||||
int end = arg_getInt(tuple_getArg(ax, 1));
|
||||
int step = arg_getInt(tuple_getArg(ax, 2));
|
||||
obj_setInt(range_obj, "_start", start);
|
||||
obj_setInt(range_obj, "_end", end);
|
||||
obj_setInt(range_obj, "_step", step);
|
||||
range_data.start = start;
|
||||
range_data.end = end;
|
||||
range_data.step = step;
|
||||
}
|
||||
range_data.i = range_data.start;
|
||||
obj_setStruct(range_obj, "_", range_data);
|
||||
return obj_arg;
|
||||
}
|
||||
|
||||
|
@ -550,29 +550,24 @@ PikaObj* obj_getHostObjWithIsTemp(PikaObj* self,
|
||||
}
|
||||
|
||||
Method methodArg_getPtr(Arg* method_arg) {
|
||||
uint32_t size_ptr = sizeof(void*);
|
||||
void* info = arg_getContent(method_arg);
|
||||
void* ptr = NULL;
|
||||
__platform_memcpy(&ptr, info, size_ptr);
|
||||
return (Method)ptr;
|
||||
MethodInfoStore* method_store =
|
||||
(MethodInfoStore*)arg_getContent(method_arg);
|
||||
return method_store->ptr;
|
||||
}
|
||||
|
||||
char* methodArg_getTypeList(Arg* method_arg, char* buffs, size_t size) {
|
||||
char* method_dec = strCopy(buffs, methodArg_getDec(method_arg));
|
||||
if (strGetSize(method_dec) > size) {
|
||||
return NULL;
|
||||
}
|
||||
return strCut(buffs, method_dec, '(', ')');
|
||||
char* method_dec = methodArg_getDec(method_arg);
|
||||
pika_assert(strGetSize(method_dec) <= size);
|
||||
char* res = strCut(buffs, method_dec, '(', ')');
|
||||
pika_assert(NULL != res);
|
||||
return res;
|
||||
}
|
||||
|
||||
char* methodArg_getName(Arg* method_arg, char* buffs, size_t size) {
|
||||
char* method_dec = strCopy(buffs, methodArg_getDec(method_arg));
|
||||
if (strGetSize(method_dec) > size) {
|
||||
return NULL;
|
||||
}
|
||||
char* res = strPopFirstToken(&method_dec, '(');
|
||||
strCopy(buffs, res);
|
||||
return buffs;
|
||||
char* method_dec = methodArg_getDec(method_arg);
|
||||
pika_assert(strGetSize(method_dec) <= size);
|
||||
char* res = strGetFirstToken(buffs, method_dec, '(');
|
||||
return res;
|
||||
}
|
||||
|
||||
Method obj_getNativeMethod(PikaObj* self, char* method_name) {
|
||||
@ -586,25 +581,20 @@ Method obj_getNativeMethod(PikaObj* self, char* method_name) {
|
||||
}
|
||||
|
||||
ByteCodeFrame* methodArg_getBytecodeFrame(Arg* method_arg) {
|
||||
uint32_t size_ptr = sizeof(void*);
|
||||
void* info = arg_getContent(method_arg) + size_ptr;
|
||||
ByteCodeFrame* ptr = NULL;
|
||||
__platform_memcpy(&ptr, info, size_ptr);
|
||||
return ptr;
|
||||
MethodInfoStore* method_store =
|
||||
(MethodInfoStore*)arg_getContent(method_arg);
|
||||
return method_store->bytecode_frame;
|
||||
}
|
||||
|
||||
char* methodArg_getDec(Arg* method_arg) {
|
||||
uint32_t size_ptr = sizeof(void*);
|
||||
void* info = arg_getContent(method_arg);
|
||||
return (char*)((uintptr_t)info + 3 * size_ptr);
|
||||
return (char*)(info + sizeof(MethodInfoStore));
|
||||
}
|
||||
|
||||
PikaObj* methodArg_getDefContext(Arg* method_arg) {
|
||||
uint32_t size_ptr = sizeof(void*);
|
||||
void* info = arg_getContent(method_arg) + 2 * size_ptr;
|
||||
PikaObj* context = NULL;
|
||||
__platform_memcpy(&context, info, size_ptr);
|
||||
return context;
|
||||
MethodInfoStore* method_store =
|
||||
(MethodInfoStore*)arg_getContent(method_arg);
|
||||
return method_store->def_context;
|
||||
}
|
||||
|
||||
void _update_proxy(PikaObj* self, char* name) {
|
||||
@ -638,19 +628,17 @@ static void obj_saveMethodInfo(PikaObj* self, MethodInfo* method_info) {
|
||||
method_info->pars = method_info->dec;
|
||||
Arg* arg = New_arg(NULL);
|
||||
uint32_t size_pars = strGetSize(method_info->pars);
|
||||
uintptr_t method_info_bytecode_frame =
|
||||
(uintptr_t)method_info->bytecode_frame;
|
||||
uintptr_t method_info_def_context = (uintptr_t)method_info->def_context;
|
||||
MethodInfoStore method_store = {
|
||||
.ptr = method_info->ptr,
|
||||
.bytecode_frame = method_info->bytecode_frame,
|
||||
.def_context = method_info->def_context,
|
||||
};
|
||||
/* the first arg_value */
|
||||
arg =
|
||||
arg_setPtr(arg, method_info->name, method_info->type, method_info->ptr);
|
||||
/* the seconed */
|
||||
arg = arg_append(arg, &(method_info_bytecode_frame),
|
||||
sizeof(method_info_bytecode_frame));
|
||||
arg = arg_append(arg, &(method_info_def_context),
|
||||
sizeof(method_info_def_context));
|
||||
arg = arg_setStruct(arg, method_info->name, &method_store,
|
||||
sizeof(method_store));
|
||||
arg = arg_append(arg, method_info->pars, size_pars + 1);
|
||||
|
||||
pika_assert(NULL != arg);
|
||||
arg_setType(arg, method_info->type);
|
||||
_update_proxy(self, method_info->name);
|
||||
args_setArg(self->list, arg);
|
||||
}
|
||||
@ -1078,23 +1066,23 @@ void args_setSysOut(Args* args, char* str) {
|
||||
}
|
||||
|
||||
void method_returnBytes(Args* args, uint8_t* val) {
|
||||
args_setBytes(args, "@rt", val, PIKA_BYTES_DEFAULT_SIZE);
|
||||
args_pushArg_name(args, "@rt", arg_newBytes(val, PIKA_BYTES_DEFAULT_SIZE));
|
||||
}
|
||||
|
||||
void method_returnStr(Args* args, char* val) {
|
||||
args_setStr(args, "@rt", val);
|
||||
args_pushArg_name(args, "@rt", arg_newStr(val));
|
||||
}
|
||||
|
||||
void method_returnInt(Args* args, int64_t val) {
|
||||
args_setInt(args, "@rt", val);
|
||||
args_pushArg_name(args, "@rt", arg_newInt(val));
|
||||
}
|
||||
|
||||
void method_returnFloat(Args* args, pika_float val) {
|
||||
args_setFloat(args, "@rt", val);
|
||||
args_pushArg_name(args, "@rt", arg_newFloat(val));
|
||||
}
|
||||
|
||||
void method_returnPtr(Args* args, void* val) {
|
||||
args_setPtr(args, "@rt", val);
|
||||
args_pushArg_name(args, "@rt", arg_newPtr(ARG_TYPE_POINTER, val));
|
||||
}
|
||||
|
||||
void method_returnObj(Args* args, void* val) {
|
||||
@ -1108,12 +1096,11 @@ void method_returnObj(Args* args, void* val) {
|
||||
} else {
|
||||
type = ARG_TYPE_OBJECT_NEW;
|
||||
}
|
||||
args_setPtrWithType(args, "@rt", type, val);
|
||||
args_pushArg_name(args, "@rt", arg_newPtr(type, val));
|
||||
}
|
||||
|
||||
void method_returnArg(Args* args, Arg* arg) {
|
||||
arg = arg_setName(arg, "@rt");
|
||||
args_setArg(args, arg);
|
||||
args_pushArg_name(args, "@rt", arg);
|
||||
}
|
||||
|
||||
int64_t method_getInt(Args* args, char* argName) {
|
||||
@ -1151,7 +1138,7 @@ Arg* arg_setRef(Arg* self, char* name, PikaObj* obj) {
|
||||
int32_t obj_newDirectObj(PikaObj* self, char* objName, NewFun newFunPtr) {
|
||||
Arg* new_obj = arg_newDirectObj(newFunPtr);
|
||||
new_obj = arg_setName(new_obj, objName);
|
||||
new_obj = arg_setType(new_obj, ARG_TYPE_OBJECT);
|
||||
arg_setType(new_obj, ARG_TYPE_OBJECT);
|
||||
args_setArg(self->list, new_obj);
|
||||
return 0;
|
||||
}
|
||||
|
@ -74,6 +74,14 @@ struct PikaObj {
|
||||
uint8_t flag;
|
||||
};
|
||||
|
||||
typedef struct RangeData RangeData;
|
||||
struct RangeData{
|
||||
int64_t start;
|
||||
int64_t end;
|
||||
int64_t step;
|
||||
int64_t i;
|
||||
};
|
||||
|
||||
#define OBJ_FLAG_PROXY_GETATTRIBUTE 0x01
|
||||
#define OBJ_FLAG_PROXY_GETATTR 0x02
|
||||
#define OBJ_FLAG_PROXY_SETATTR 0x04
|
||||
@ -101,6 +109,12 @@ struct MethodInfo {
|
||||
ByteCodeFrame* bytecode_frame;
|
||||
};
|
||||
|
||||
typedef struct MethodInfoStore {
|
||||
void* ptr;
|
||||
ByteCodeFrame* bytecode_frame;
|
||||
PikaObj* def_context;
|
||||
} MethodInfoStore;
|
||||
|
||||
typedef PikaObj LibObj;
|
||||
typedef PikaObj PikaMaker;
|
||||
|
||||
|
@ -478,7 +478,7 @@ static Arg* VM_instruction_handler_NEW(PikaObj* self,
|
||||
Arg* arg_ret_reg) {
|
||||
Arg* origin_arg = obj_getArg(vm->locals, data);
|
||||
Arg* new_arg = arg_copy(origin_arg);
|
||||
origin_arg = arg_setType(origin_arg, ARG_TYPE_OBJECT);
|
||||
arg_setType(origin_arg, ARG_TYPE_OBJECT);
|
||||
arg_setType(new_arg, ARG_TYPE_OBJECT_NEW);
|
||||
return new_arg;
|
||||
}
|
||||
@ -876,7 +876,7 @@ static int VMState_loadArgsFromMethodArg(VMState* vm,
|
||||
/* skip type hint */
|
||||
strPopLastToken(arg_name, ':');
|
||||
/* load normal arg */
|
||||
args_setArg(locals, arg_setName(call_arg, arg_name));
|
||||
args_pushArg_name(locals, arg_name, call_arg);
|
||||
}
|
||||
|
||||
if (strIsContain(type_list, '=')) {
|
||||
@ -1260,6 +1260,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
&sub_run_state, arg_ret_reg);
|
||||
if (skip_init) {
|
||||
if (arg_getType(return_arg) == ARG_TYPE_OBJECT_NEW) {
|
||||
pika_assert(NULL != return_arg);
|
||||
arg_setType(return_arg, ARG_TYPE_OBJECT);
|
||||
}
|
||||
}
|
||||
@ -1271,6 +1272,7 @@ static Arg* VM_instruction_handler_RUN(PikaObj* self,
|
||||
|
||||
/* __init__() */
|
||||
if (NULL != return_arg && ARG_TYPE_OBJECT_NEW == arg_getType(return_arg)) {
|
||||
pika_assert(NULL != return_arg);
|
||||
arg_setType(return_arg, ARG_TYPE_OBJECT);
|
||||
/* init object */
|
||||
PikaObj* new_obj = arg_getPtr(return_arg);
|
||||
@ -1477,6 +1479,7 @@ static Arg* VM_instruction_handler_OUT(PikaObj* self,
|
||||
}
|
||||
/* set free object to nomal object */
|
||||
if (ARG_TYPE_OBJECT_NEW == outArg_type) {
|
||||
pika_assert(NULL != out_arg);
|
||||
arg_setType(out_arg, ARG_TYPE_OBJECT);
|
||||
}
|
||||
|
||||
|
@ -164,14 +164,12 @@ uint32_t arg_totleSize(Arg* self) {
|
||||
}
|
||||
|
||||
void arg_freeContent(Arg* self) {
|
||||
if (NULL != self) {
|
||||
uint32_t totleSize = arg_totleSize(self);
|
||||
pika_assert(NULL != self);
|
||||
if (_arg_cache_push(self, self->size)) {
|
||||
return;
|
||||
}
|
||||
pikaFree(self, totleSize);
|
||||
pikaFree(self, arg_totleSize(self));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) {
|
||||
@ -209,21 +207,14 @@ Arg* arg_setName(Arg* self, char* name) {
|
||||
return arg_setNameHash(self, hash_time33(name));
|
||||
}
|
||||
|
||||
Arg* arg_setType(Arg* self, ArgType type) {
|
||||
if (NULL == self) {
|
||||
return arg_create("", type, NULL, 0, NULL);
|
||||
}
|
||||
self->type = type;
|
||||
return self;
|
||||
}
|
||||
|
||||
Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) {
|
||||
self = arg_newContent(self, size + sizeof(size_t) + 1);
|
||||
self = arg_newContent(size + sizeof(size_t) + 1);
|
||||
if (NULL == self) {
|
||||
return NULL;
|
||||
}
|
||||
self = arg_setName(self, name);
|
||||
self = arg_setType(self, ARG_TYPE_BYTES);
|
||||
pika_assert(NULL != self);
|
||||
arg_setType(self, ARG_TYPE_BYTES);
|
||||
void* dir = arg_getContent(self);
|
||||
/* set content all to 0 */
|
||||
__platform_memset(dir, 0, size + sizeof(size_t) + 1);
|
||||
@ -238,8 +229,7 @@ Arg* arg_setBytes(Arg* self, char* name, uint8_t* src, size_t size) {
|
||||
return self;
|
||||
}
|
||||
|
||||
Arg* arg_newContent(Arg* self, uint32_t size) {
|
||||
arg_freeContent(self);
|
||||
Arg* arg_newContent(uint32_t size) {
|
||||
Arg* newContent = arg_create("", ARG_TYPE_NONE, NULL, size, NULL);
|
||||
return newContent;
|
||||
}
|
||||
@ -302,7 +292,8 @@ Arg* arg_setHeapStruct(Arg* self,
|
||||
Arg* struct_arg =
|
||||
arg_setContent(NULL, (uint8_t*)&struct_deinit_fun, sizeof(void*));
|
||||
struct_arg = arg_append(struct_arg, (uint8_t*)struct_ptr, struct_size);
|
||||
struct_arg = arg_setType(struct_arg, ARG_TYPE_STRUCT_HEAP);
|
||||
pika_assert(NULL != struct_arg);
|
||||
arg_setType(struct_arg, ARG_TYPE_STRUCT_HEAP);
|
||||
struct_arg = arg_setName(struct_arg, name);
|
||||
return struct_arg;
|
||||
}
|
||||
@ -387,7 +378,8 @@ Arg* arg_copy(Arg* arg_src) {
|
||||
arg_dict = arg_setContent(arg_dict, arg_getContent(arg_src),
|
||||
arg_getContentSize(arg_src));
|
||||
arg_dict = arg_setNameHash(arg_dict, arg_getNameHash(arg_src));
|
||||
arg_dict = arg_setType(arg_dict, arg_getType(arg_src));
|
||||
pika_assert(NULL != arg_dict);
|
||||
arg_setType(arg_dict, arg_getType(arg_src));
|
||||
arg_setIsKeyword(arg_dict, arg_getIsKeyword(arg_src));
|
||||
return arg_dict;
|
||||
}
|
||||
@ -411,7 +403,8 @@ Arg* arg_copy_noalloc(Arg* arg_src, Arg* arg_dict) {
|
||||
arg_dict = arg_setContent(arg_dict, arg_getContent(arg_src),
|
||||
arg_getContentSize(arg_src));
|
||||
arg_dict = arg_setNameHash(arg_dict, arg_getNameHash(arg_src));
|
||||
arg_dict = arg_setType(arg_dict, arg_getType(arg_src));
|
||||
pika_assert(NULL != arg_dict);
|
||||
arg_setType(arg_dict, arg_getType(arg_src));
|
||||
arg_setIsKeyword(arg_dict, arg_getIsKeyword(arg_src));
|
||||
return arg_dict;
|
||||
}
|
||||
@ -433,6 +426,7 @@ Arg* arg_append(Arg* self, void* new_content, size_t new_size) {
|
||||
if (NULL == new_arg) {
|
||||
new_arg = arg_setContent(NULL, NULL, old_size + new_size);
|
||||
}
|
||||
pika_assert(NULL != new_arg);
|
||||
arg_setType(new_arg, arg_getType(self));
|
||||
arg_setNameHash(new_arg, arg_getNameHash(self));
|
||||
if (self != new_arg) {
|
||||
@ -463,6 +457,7 @@ void arg_deinitHeap(Arg* self) {
|
||||
StructDeinitFun struct_deinit_fun =
|
||||
(StructDeinitFun)arg_getHeapStructDeinitFun(self);
|
||||
struct_deinit_fun(arg_getHeapStruct(self));
|
||||
return;
|
||||
}
|
||||
/* deinit sub object */
|
||||
if (ARG_TYPE_OBJECT == type) {
|
||||
@ -472,6 +467,7 @@ void arg_deinitHeap(Arg* self) {
|
||||
if (ref_cnt <= 0) {
|
||||
obj_deinit(subObj);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,8 +85,8 @@ void arg_freeContent(Arg* self);
|
||||
|
||||
Arg* arg_setName(Arg* self, char* name);
|
||||
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size);
|
||||
Arg* arg_newContent(Arg* self, uint32_t size);
|
||||
Arg* arg_setType(Arg* self, ArgType type);
|
||||
Arg* arg_newContent(uint32_t size);
|
||||
#define arg_setType(__self, __type) ((__self)->type = (__type))
|
||||
#define arg_getNameHash(__self) ((__self)->name_hash)
|
||||
#define arg_getType(__self) ((ArgType)(__self)->type)
|
||||
uint32_t arg_getContentSize(Arg* self);
|
||||
|
@ -60,12 +60,20 @@ void* args_getPtr(Args* self, char* name) {
|
||||
return pointer;
|
||||
}
|
||||
|
||||
PIKA_RES args_setPtrWithType(Args* self, char* name, ArgType type, void* val) {
|
||||
Arg* arg = args_getArg(self, name);
|
||||
if (NULL == arg) {
|
||||
args_pushArg_name(self, name, arg_newPtr(type, val));
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
void** val_ptr = (void**)arg_getContent(arg);
|
||||
*val_ptr = val;
|
||||
arg_setType(arg, type);
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
|
||||
PIKA_RES args_setPtr(Args* self, char* name, void* argPointer) {
|
||||
PIKA_RES errCode = PIKA_RES_OK;
|
||||
Arg* argNew = New_arg(NULL);
|
||||
argNew = arg_setPtr(argNew, name, ARG_TYPE_POINTER, argPointer);
|
||||
args_setArg(self, argNew);
|
||||
return errCode;
|
||||
return args_setPtrWithType(self, name, ARG_TYPE_POINTER, argPointer);
|
||||
}
|
||||
|
||||
PIKA_RES args_setRef(Args* self, char* name, void* argPointer) {
|
||||
@ -88,9 +96,6 @@ PIKA_RES args_setStr(Args* self, char* name, char* strIn) {
|
||||
}
|
||||
|
||||
PIKA_RES args_pushArg(Args* self, Arg* arg) {
|
||||
if (NULL == arg) {
|
||||
return PIKA_RES_ERR_ARG_NO_FOUND;
|
||||
}
|
||||
Arg* new_arg = NULL;
|
||||
if (!arg_isSerialized(arg)) {
|
||||
new_arg = arg_copy(arg);
|
||||
@ -102,6 +107,10 @@ PIKA_RES args_pushArg(Args* self, Arg* arg) {
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
|
||||
PIKA_RES args_pushArg_name(Args* self, char* name, Arg* arg) {
|
||||
return args_pushArg(self, arg_setName(arg, name));
|
||||
}
|
||||
|
||||
PIKA_RES args_setBytes(Args* self, char* name, uint8_t* src, size_t size) {
|
||||
Arg* argNew = arg_setBytes(NULL, name, src, size);
|
||||
args_setArg(self, argNew);
|
||||
@ -110,7 +119,7 @@ PIKA_RES args_setBytes(Args* self, char* name, uint8_t* src, size_t size) {
|
||||
|
||||
char* args_getBuff(Args* self, int32_t size) {
|
||||
Arg* argNew = New_arg(NULL);
|
||||
argNew = arg_newContent(argNew, size + 1);
|
||||
argNew = arg_newContent(size + 1);
|
||||
args_pushArg(self, argNew);
|
||||
return (char*)arg_getContent(argNew);
|
||||
}
|
||||
@ -157,10 +166,14 @@ size_t args_getBytesSize(Args* self, char* name) {
|
||||
return arg_getBytesSize(arg);
|
||||
}
|
||||
|
||||
PIKA_RES args_setInt(Args* self, char* name, int64_t int64In) {
|
||||
Arg* argNew = New_arg(NULL);
|
||||
argNew = arg_setInt(argNew, name, int64In);
|
||||
args_setArg(self, argNew);
|
||||
PIKA_RES args_setInt(Args* self, char* name, int64_t val) {
|
||||
Arg* arg = args_getArg(self, name);
|
||||
if (NULL == arg) {
|
||||
args_pushArg_name(self, name, arg_newInt(val));
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
int64_t* val_ptr = (int64_t*)arg_getContent(arg);
|
||||
*val_ptr = val;
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
|
||||
@ -280,6 +293,10 @@ PIKA_RES __updateArg(Args* self, Arg* argNew) {
|
||||
LinkNode* nodeToUpdate = NULL;
|
||||
LinkNode* nodeNow = self->firstNode;
|
||||
LinkNode* priorNode = NULL;
|
||||
PIKA_RES res;
|
||||
if (NULL == self->firstNode) {
|
||||
return PIKA_RES_ERR_ARG_NO_FOUND;
|
||||
}
|
||||
Hash nameHash = arg_getNameHash(argNew);
|
||||
while (1) {
|
||||
if (arg_getNameHash((Arg*)nodeNow) == nameHash) {
|
||||
@ -288,7 +305,7 @@ PIKA_RES __updateArg(Args* self, Arg* argNew) {
|
||||
}
|
||||
if (arg_getNext((Arg*)nodeNow) == NULL) {
|
||||
// error, node no found
|
||||
goto exit;
|
||||
return PIKA_RES_ERR_ARG_NO_FOUND;
|
||||
}
|
||||
priorNode = nodeNow;
|
||||
nodeNow = (LinkNode*)arg_getNext((Arg*)nodeNow);
|
||||
@ -297,33 +314,33 @@ PIKA_RES __updateArg(Args* self, Arg* argNew) {
|
||||
arg_deinitHeap((Arg*)nodeToUpdate);
|
||||
|
||||
nodeToUpdate = (LinkNode*)arg_setContent(
|
||||
(Arg*)nodeToUpdate, arg_getContent(argNew), arg_getContentSize(argNew));
|
||||
(Arg*)nodeToUpdate, arg_getContent(argNew), arg_getSize(argNew));
|
||||
|
||||
nodeToUpdate =
|
||||
(LinkNode*)arg_setType((Arg*)nodeToUpdate, arg_getType(argNew));
|
||||
pika_assert(NULL != nodeToUpdate);
|
||||
arg_setType((Arg*)nodeToUpdate, arg_getType(argNew));
|
||||
// update privior link, because arg_getContent would free origin pointer
|
||||
if (NULL == priorNode) {
|
||||
self->firstNode = nodeToUpdate;
|
||||
res = PIKA_RES_OK;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
arg_setNext((Arg*)priorNode, (Arg*)nodeToUpdate);
|
||||
res = PIKA_RES_OK;
|
||||
goto exit;
|
||||
exit:
|
||||
if (!arg_isSerialized(argNew)) {
|
||||
return PIKA_RES_OK;
|
||||
return res;
|
||||
}
|
||||
arg_freeContent(argNew);
|
||||
return PIKA_RES_OK;
|
||||
return res;
|
||||
}
|
||||
|
||||
PIKA_RES args_setArg(Args* self, Arg* arg) {
|
||||
Hash nameHash = arg_getNameHash(arg);
|
||||
if (!args_isArgExist_hash(self, nameHash)) {
|
||||
args_pushArg(self, arg);
|
||||
if (PIKA_RES_OK == __updateArg(self, arg)) {
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
__updateArg(self, arg);
|
||||
args_pushArg(self, arg);
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
|
||||
@ -331,13 +348,21 @@ PIKA_RES args_setArg(Args* self, Arg* arg) {
|
||||
#define __PIKA_CFG_HASH_LIST_CACHE_SIZE 4
|
||||
#endif
|
||||
|
||||
#define __USE_PIKA_HASH_LIST_CACHE 0
|
||||
|
||||
LinkNode* args_getNode_hash(Args* self, Hash nameHash) {
|
||||
LinkNode* node = self->firstNode;
|
||||
#if __USE_PIKA_HASH_LIST_CACHE
|
||||
int_fast8_t n = 0;
|
||||
#endif
|
||||
while (NULL != node) {
|
||||
Arg* arg = (Arg*)node;
|
||||
Hash thisNameHash = arg_getNameHash(arg);
|
||||
#if __USE_PIKA_HASH_LIST_CACHE
|
||||
n++;
|
||||
#endif
|
||||
if (thisNameHash == nameHash) {
|
||||
#if __USE_PIKA_HASH_LIST_CACHE
|
||||
if (n > __PIKA_CFG_HASH_LIST_CACHE_SIZE) {
|
||||
/* the first __PIKA_CFG_HASH_LIST_CACHE_SIZE items in the list
|
||||
* is considered as a cache.
|
||||
@ -352,6 +377,7 @@ LinkNode* args_getNode_hash(Args* self, Hash nameHash) {
|
||||
arg_setNext(arg, (Arg*)(self->firstNode));
|
||||
self->firstNode = (LinkNode*)arg;
|
||||
}
|
||||
#endif
|
||||
return (LinkNode*)arg;
|
||||
}
|
||||
node = (LinkNode*)arg_getNext((Arg*)node);
|
||||
@ -374,9 +400,6 @@ Arg* args_getArg_hash(Args* self, Hash nameHash) {
|
||||
Arg* args_getArg(Args* self, char* name) {
|
||||
pika_assert(NULL != self);
|
||||
LinkNode* node = args_getNode(self, name);
|
||||
if (NULL == node) {
|
||||
return NULL;
|
||||
}
|
||||
return (Arg*)node;
|
||||
}
|
||||
|
||||
@ -472,16 +495,6 @@ exit:
|
||||
return res;
|
||||
}
|
||||
|
||||
PIKA_RES args_setPtrWithType(Args* self,
|
||||
char* name,
|
||||
ArgType type,
|
||||
void* objPtr) {
|
||||
Arg* argNew = New_arg(NULL);
|
||||
argNew = arg_setPtr(argNew, name, type, objPtr);
|
||||
args_setArg(self, argNew);
|
||||
return PIKA_RES_OK;
|
||||
}
|
||||
|
||||
PIKA_RES args_foreach(Args* self,
|
||||
int32_t (*eachHandle)(Arg* argEach, Args* context),
|
||||
Args* context) {
|
||||
@ -543,7 +556,7 @@ PikaDict* New_dict(void) {
|
||||
PikaList* New_list(void) {
|
||||
PikaList* self = (PikaList*)New_args(NULL);
|
||||
/* set top index for append */
|
||||
args_setInt(&self->super, "top", 0);
|
||||
args_pushArg_name(&self->super, "top", arg_newInt(0));
|
||||
return self;
|
||||
}
|
||||
|
||||
|
@ -110,6 +110,7 @@ PIKA_RES args_foreach(Args* self,
|
||||
|
||||
char* args_getBuff(Args* self, int32_t size);
|
||||
PIKA_RES args_pushArg(Args* self, Arg* arg);
|
||||
PIKA_RES args_pushArg_name(Args* self, char* name, Arg* arg);
|
||||
Arg* args_getArgByidex(Args* self, int index);
|
||||
void* args_getHeapStruct(Args* self, char* name);
|
||||
PIKA_RES args_removeArg_notDeinitArg(Args* self, Arg* argNow);
|
||||
|
@ -1589,7 +1589,7 @@ TEST(vm, multi_return_fn) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(vm, rang_1) {
|
||||
TEST(vm, range_1) {
|
||||
/* init */
|
||||
pikaMemInfo.heapUsedMax = 0;
|
||||
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "test_common.h"
|
||||
|
||||
TEST(arg_test, cache_){
|
||||
TEST(arg_test, cache_) {
|
||||
Arg* arg1 = arg_newInt(1);
|
||||
arg_deinit(arg1);
|
||||
Arg* arg2 = arg_newInt(2);
|
||||
@ -57,8 +57,9 @@ TEST(arg_test, name) {
|
||||
}
|
||||
|
||||
TEST(arg_test, type) {
|
||||
Arg* arg = New_arg(NULL);
|
||||
arg = arg_setType(arg, ARG_TYPE_NONE);
|
||||
Arg* arg = arg_newInt(1);
|
||||
pika_assert(NULL != arg);
|
||||
arg_setType(arg, ARG_TYPE_NONE);
|
||||
ArgType type = arg_getType(arg);
|
||||
EXPECT_EQ(ARG_TYPE_NONE, type);
|
||||
arg_deinit(arg);
|
||||
|
@ -44,7 +44,8 @@ TEST(content, set) {
|
||||
contentIn[3] = 4;
|
||||
Arg* self = content_init("", ARG_TYPE_NONE, NULL, 0, NULL);
|
||||
self = content_setName(self, "name");
|
||||
self = arg_setType(self, ARG_TYPE_NONE);
|
||||
pika_assert(NULL != self);
|
||||
arg_setType(self, ARG_TYPE_NONE);
|
||||
self = content_setContent(self, contentIn, 4);
|
||||
|
||||
uint16_t typeOffset = content_typeOffset(self);
|
||||
|
Loading…
x
Reference in New Issue
Block a user