From 48d9c31d598b52a1e81e525d45e7708c5259554a Mon Sep 17 00:00:00 2001 From: "DSKY\\toney.yin" Date: Fri, 2 Feb 2024 17:41:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ljson=E4=B8=BA=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=BA=93=EF=BC=8C=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 1 + core/hive/src/export.cpp | 1 - core/plugins/lplugins.mak | 6 - core/plugins/lplugins.vcxproj | 4 - core/plugins/lplugins.vcxproj.filters | 15 -- extend/ljson/ljson.lmak | 38 +++++ extend/ljson/ljson.mak | 132 ++++++++++++++++++ extend/ljson/ljson.vcxproj | 79 +++++++++++ extend/ljson/ljson.vcxproj.filters | 27 ++++ .../src/ljson => extend/ljson/src}/ljson.cpp | 3 + .../src/ljson => extend/ljson/src}/ljson.h | 32 +++-- .../src/ljson => extend/ljson/src}/yyjson.c | 98 ++++++------- .../src/ljson => extend/ljson/src}/yyjson.h | 10 +- hive.sln | 8 ++ 14 files changed, 366 insertions(+), 88 deletions(-) create mode 100644 extend/ljson/ljson.lmak create mode 100644 extend/ljson/ljson.mak create mode 100644 extend/ljson/ljson.vcxproj create mode 100644 extend/ljson/ljson.vcxproj.filters rename {core/plugins/src/ljson => extend/ljson/src}/ljson.cpp (94%) rename {core/plugins/src/ljson => extend/ljson/src}/ljson.h (90%) rename {core/plugins/src/ljson => extend/ljson/src}/yyjson.c (99%) rename {core/plugins/src/ljson => extend/ljson/src}/yyjson.h (99%) diff --git a/Makefile b/Makefile index a384f131..7f91155c 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ lua: luaext: cd extend/lcurl; make -j4 SOLUTION_DIR=$(CUR_DIR) -f lcurl.mak; cd extend/ldetour; make -j4 SOLUTION_DIR=$(CUR_DIR) -f ldetour.mak; + cd extend/ljson; make -j4 SOLUTION_DIR=$(CUR_DIR) -f ljson.mak; cd extend/luaxlsx; make -j4 SOLUTION_DIR=$(CUR_DIR) -f luaxlsx.mak; share: diff --git a/core/hive/src/export.cpp b/core/hive/src/export.cpp index 85694124..de9c160c 100644 --- a/core/hive/src/export.cpp +++ b/core/hive/src/export.cpp @@ -21,7 +21,6 @@ extern "C" REGISTER_CUSTOM_LIBRARY("lcodec", luaopen_lcodec); REGISTER_CUSTOM_LIBRARY("lstdfs", luaopen_lstdfs); REGISTER_CUSTOM_LIBRARY("lcrypt", luaopen_lcrypt); - REGISTER_CUSTOM_LIBRARY("ljson", luaopen_ljson); REGISTER_CUSTOM_LIBRARY("luabus", luaopen_luabus); REGISTER_CUSTOM_LIBRARY("laes", luaopen_laes); diff --git a/core/plugins/lplugins.mak b/core/plugins/lplugins.mak index ba3e7b4b..73ccdb01 100644 --- a/core/plugins/lplugins.mak +++ b/core/plugins/lplugins.mak @@ -122,11 +122,6 @@ OBJS += $(patsubst $(SRC_DIR)/lcrypt/%.m, $(INT_DIR)/lcrypt/%.o, $(filter-out $( OBJS += $(patsubst $(SRC_DIR)/lcrypt/%.cc, $(INT_DIR)/lcrypt/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/lcrypt/*.cc))) OBJS += $(patsubst $(SRC_DIR)/lcrypt/%.cpp, $(INT_DIR)/lcrypt/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/lcrypt/*.cpp))) #子目录 -OBJS += $(patsubst $(SRC_DIR)/ljson/%.c, $(INT_DIR)/ljson/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/ljson/*.c))) -OBJS += $(patsubst $(SRC_DIR)/ljson/%.m, $(INT_DIR)/ljson/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/ljson/*.m))) -OBJS += $(patsubst $(SRC_DIR)/ljson/%.cc, $(INT_DIR)/ljson/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/ljson/*.cc))) -OBJS += $(patsubst $(SRC_DIR)/ljson/%.cpp, $(INT_DIR)/ljson/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/ljson/*.cpp))) -#子目录 OBJS += $(patsubst $(SRC_DIR)/lrandom/%.c, $(INT_DIR)/lrandom/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/lrandom/*.c))) OBJS += $(patsubst $(SRC_DIR)/lrandom/%.m, $(INT_DIR)/lrandom/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/lrandom/*.m))) OBJS += $(patsubst $(SRC_DIR)/lrandom/%.cc, $(INT_DIR)/lrandom/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/lrandom/*.cc))) @@ -192,7 +187,6 @@ pre_build: mkdir -p $(INT_DIR)/lbson mkdir -p $(INT_DIR)/lcodec mkdir -p $(INT_DIR)/lcrypt - mkdir -p $(INT_DIR)/ljson mkdir -p $(INT_DIR)/lrandom mkdir -p $(INT_DIR)/lstdfs mkdir -p $(INT_DIR)/ltimer diff --git a/core/plugins/lplugins.vcxproj b/core/plugins/lplugins.vcxproj index 250b3a91..3f4f827d 100644 --- a/core/plugins/lplugins.vcxproj +++ b/core/plugins/lplugins.vcxproj @@ -30,8 +30,6 @@ - - @@ -55,8 +53,6 @@ - - diff --git a/core/plugins/lplugins.vcxproj.filters b/core/plugins/lplugins.vcxproj.filters index adf1daad..c243d9f6 100644 --- a/core/plugins/lplugins.vcxproj.filters +++ b/core/plugins/lplugins.vcxproj.filters @@ -70,12 +70,6 @@ lcrypt - - ljson - - - ljson - lrandom @@ -141,12 +135,6 @@ lcrypt - - ljson - - - ljson - lrandom @@ -185,9 +173,6 @@ {6779B38A-0EC6-088E-3974-E2A164AE1520} - - {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C} - {3FF84F4B-3548-31CF-586F-ADCF4C5716CF} diff --git a/extend/ljson/ljson.lmak b/extend/ljson/ljson.lmak new file mode 100644 index 00000000..6b155225 --- /dev/null +++ b/extend/ljson/ljson.lmak @@ -0,0 +1,38 @@ +--工程名字 +PROJECT_NAME = "ljson" + +--目标名字 +TARGET_NAME = "ljson" + +----工程类型: static/dynamic/exe +PROJECT_TYPE = "dynamic" + +EX_FLAGS = { + "Wno-implicit-fallthrough" +} + +--需要的include目录 +INCLUDES = { + "../lua/lua", + "../luakit/include" +} + +MIMALLOC = false + +--需要连接的库文件 +LIBS = { + "lua" +} + +--WINDOWS需要定义的选项 +WINDOWS_DEFINES = { + "LUA_BUILD_AS_DLL" +} + +--依赖项目 +DEPS = { + "lualib" +} + +--分组定义 +GROUP = "luaext" diff --git a/extend/ljson/ljson.mak b/extend/ljson/ljson.mak new file mode 100644 index 00000000..b88d0dff --- /dev/null +++ b/extend/ljson/ljson.mak @@ -0,0 +1,132 @@ +#工程名字 +PROJECT_NAME = ljson + +#目标名字 +TARGET_NAME = ljson + +#系统环境 +UNAME_S = $(shell uname -s) + +#伪目标 +.PHONY: clean all target pre_build post_build +all : pre_build target post_build + +#CFLAG +MYCFLAGS = + +#需要定义的FLAG +MYCFLAGS += -Wsign-compare +MYCFLAGS += -Wno-sign-compare +MYCFLAGS += -Wno-unused-variable +MYCFLAGS += -Wno-unused-parameter +MYCFLAGS += -Wno-unknown-pragmas +MYCFLAGS += -Wno-unused-but-set-variable +MYCFLAGS += -Wno-unused-but-set-parameter +MYCFLAGS += -Wno-unused-function +MYCFLAGS += -Wno-unused-result +MYCFLAGS += -Wno-implicit-fallthrough + +#c标准库版本 +#gnu99/gnu11/gnu17 +STDC = -std=gnu99 + +#c++标准库版本 +#c++11/c++14/c++17/c++20 +STDCPP = -std=c++17 + +#需要的include目录 +MYCFLAGS += -I../lua/lua +MYCFLAGS += -I../luakit/include + +#需要定义的选项 + +#LDFLAGS +LDFLAGS = + + +#源文件路径 +SRC_DIR = src + +#需要排除的源文件,目录基于$(SRC_DIR) +EXCLUDE = + +#需要连接的库文件 +LIBS = +#自定义库 +LIBS += -llua +#系统库 +LIBS += -lm -ldl -lstdc++ -lpthread + +#定义基础的编译选项 +ifndef CC +CC = gcc +endif +ifndef CX +CX = c++ +endif +CFLAGS = -g -O2 -Wall -Wno-deprecated -Wextra $(STDC) $(MYCFLAGS) +CXXFLAGS = -g -O2 -Wall -Wno-deprecated -Wextra $(STDCPP) $(MYCFLAGS) + +#项目目录 +ifndef SOLUTION_DIR +SOLUTION_DIR=./ +endif + +#临时文件目录 +INT_DIR = $(SOLUTION_DIR)temp/$(PROJECT_NAME) + +#目标文件前缀,定义则.so和.a加lib前缀,否则不加 +PROJECT_PREFIX = + +#目标定义 +MYCFLAGS += -fPIC +TARGET_DIR = $(SOLUTION_DIR)bin +TARGET_DYNAMIC = $(TARGET_DIR)/$(PROJECT_PREFIX)$(TARGET_NAME).so +#soname +ifeq ($(UNAME_S), Linux) +LDFLAGS += -Wl,-soname,$(PROJECT_PREFIX)$(TARGET_NAME).so +endif +#install_name +ifeq ($(UNAME_S), Darwin) +LDFLAGS += -Wl,-install_name,$(PROJECT_PREFIX)$(TARGET_NAME).so +endif + +#link添加.so目录 +LDFLAGS += -L$(SOLUTION_DIR)bin +LDFLAGS += -L$(SOLUTION_DIR)library + +#自动生成目标 +OBJS = +#根目录 +OBJS += $(patsubst $(SRC_DIR)/%.c, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.c))) +OBJS += $(patsubst $(SRC_DIR)/%.m, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.m))) +OBJS += $(patsubst $(SRC_DIR)/%.cc, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cc))) +OBJS += $(patsubst $(SRC_DIR)/%.cpp, $(INT_DIR)/%.o, $(filter-out $(EXCLUDE), $(wildcard $(SRC_DIR)/*.cpp))) + +# 编译所有源文件 +$(INT_DIR)/%.o : $(SRC_DIR)/%.c + $(CC) $(CFLAGS) -c $< -o $@ +$(INT_DIR)/%.o : $(SRC_DIR)/%.m + $(CC) $(CFLAGS) -c $< -o $@ +$(INT_DIR)/%.o : $(SRC_DIR)/%.cc + $(CX) $(CXXFLAGS) -c $< -o $@ +$(INT_DIR)/%.o : $(SRC_DIR)/%.cpp + $(CX) $(CXXFLAGS) -c $< -o $@ + +$(TARGET_DYNAMIC) : $(OBJS) + $(CC) -o $@ -shared $(OBJS) $(LDFLAGS) $(LIBS) + +#target伪目标 +target : $(TARGET_DYNAMIC) + +#clean伪目标 +clean : + rm -rf $(INT_DIR) + +#预编译 +pre_build: + mkdir -p $(INT_DIR) + mkdir -p $(TARGET_DIR) + +#后编译 +post_build: diff --git a/extend/ljson/ljson.vcxproj b/extend/ljson/ljson.vcxproj new file mode 100644 index 00000000..2b5cf313 --- /dev/null +++ b/extend/ljson/ljson.vcxproj @@ -0,0 +1,79 @@ + + + + + Develop + x64 + + + + + + + + + + + + {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C} + ljson + Win32Proj + 10.0 + ljson + + + + DynamicLibrary + v142 + MultiByte + + + + + + + + + + <_ProjectFileVersion>11.0.50727.1 + + + ljson + $(SolutionDir)temp\bin\$(Platform)\ + $(SolutionDir)temp\$(ProjectName)\$(Platform)\ + + + + Disabled + ..\lua\lua;..\luakit\include;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions) + Default + MultiThreadedDLL + + Level3 + ProgramDatabase + Default + stdcpp17 + true + + + $(OutDir)$(TargetName)$(TargetExt) + $(SolutionDir)library\$(Platform);;%(AdditionalLibraryDirectories) + true + Console + $(SolutionDir)library\$(Platform)\$(TargetName).lib + $(SolutionDir)temp\$(ProjectName)\$(Platform)\$(TargetName).pdb + lua.lib;%(AdditionalDependencies) + + + + + + + copy /y $(TargetPath) $(SolutionDir)bin + + + + + + \ No newline at end of file diff --git a/extend/ljson/ljson.vcxproj.filters b/extend/ljson/ljson.vcxproj.filters new file mode 100644 index 00000000..740428e0 --- /dev/null +++ b/extend/ljson/ljson.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + inc + + + inc + + + + + src + + + src + + + + + {CF9F3FDE-7326-F1D8-E642-05F0E07A3695} + + + {25D902C2-4283-AB8C-FBAC-54DFA101AD31} + + + \ No newline at end of file diff --git a/core/plugins/src/ljson/ljson.cpp b/extend/ljson/src/ljson.cpp similarity index 94% rename from core/plugins/src/ljson/ljson.cpp rename to extend/ljson/src/ljson.cpp index cb12e810..09949741 100644 --- a/core/plugins/src/ljson/ljson.cpp +++ b/extend/ljson/src/ljson.cpp @@ -1,3 +1,5 @@ +#define LUA_LIB + #include "ljson.h" namespace ljson { @@ -10,6 +12,7 @@ namespace ljson { } luakit::lua_table open_ljson(lua_State* L) { + thread_json.init_alc(); luakit::kit_state kit_state(L); auto ljson = kit_state.new_table(); ljson.set_function("jsoncodec", json_codec); diff --git a/core/plugins/src/ljson/ljson.h b/extend/ljson/src/ljson.h similarity index 90% rename from core/plugins/src/ljson/ljson.h rename to extend/ljson/src/ljson.h index 771b4f57..95a127d6 100644 --- a/core/plugins/src/ljson/ljson.h +++ b/extend/ljson/src/ljson.h @@ -13,6 +13,11 @@ namespace ljson { class yyjson { public: friend jsoncodec; + + yyjson_alc* get_alc() { + return &m_alc; + } + int encode(lua_State* L) { bool empty_as_array = luaL_opt(L, lua_toboolean, 2, false); return encode_impl(L, YYJSON_WRITE_ALLOW_INVALID_UNICODE, empty_as_array); @@ -27,13 +32,13 @@ namespace ljson { int encode_impl(lua_State* L, yyjson_write_flag flag, bool emy_as_arr) { size_t data_len; yyjson_write_err err; - yyjson_mut_doc* doc = yyjson_mut_doc_new(nullptr); + yyjson_mut_doc* doc = yyjson_mut_doc_new(&m_alc); yyjson_mut_val* val = encode_one(L, doc, emy_as_arr, 1, 0); - char* json = yyjson_mut_val_write_opts(val, flag, nullptr, &data_len, &err); + char* json = yyjson_mut_val_write_opts(val, flag, &m_alc, &data_len, &err); if (!json) luaL_error(L, err.msg); lua_pushlstring(L, json, data_len); + m_alc.free(m_alc.ctx, json); yyjson_mut_doc_free(doc); - free(json); return 1; } @@ -42,13 +47,17 @@ namespace ljson { yyjson_read_err err; const char* buf = luaL_checklstring(L, 1, &len); bool numkeyable = luaL_opt(L, lua_toboolean, 2, false); - yyjson_doc* doc = yyjson_read_opts((char*)buf, len, YYJSON_READ_ALLOW_INVALID_UNICODE, nullptr, &err); + yyjson_doc* doc = yyjson_read_opts((char*)buf, len, YYJSON_READ_ALLOW_INVALID_UNICODE, &m_alc, &err); if (!doc) luaL_error(L, err.msg); decode_one(L, yyjson_doc_get_root(doc), numkeyable); yyjson_doc_free(doc); return 1; } + void init_alc() { + yyjson_alc_pool_init(&m_alc, m_buf, USHRT_MAX); + } + protected: bool is_array(lua_State* L, int index, bool emy_as_arr) { size_t raw_len = lua_rawlen(L, index); @@ -213,6 +222,10 @@ namespace ljson { break; } } + + protected: + yyjson_alc m_alc; + char m_buf[USHRT_MAX]; }; class jsoncodec : public codec_base { @@ -225,16 +238,19 @@ namespace ljson { virtual uint8_t* encode(lua_State* L, int index, size_t* len) { yyjson_write_err err; - yyjson_mut_doc* doc = yyjson_mut_doc_new(nullptr); + yyjson_alc* alc = m_json->get_alc(); + yyjson_mut_doc* doc = yyjson_mut_doc_new(alc); yyjson_mut_val* val = m_json->encode_one(L, doc, false, index, 0); - uint8_t* json = (uint8_t*)yyjson_mut_val_write_opts(val, YYJSON_WRITE_ALLOW_INVALID_UNICODE, nullptr, len, &err); - return json; + auto json = yyjson_mut_val_write_opts(val, YYJSON_WRITE_ALLOW_INVALID_UNICODE, alc, len, &err); + alc->free(alc->ctx, json); + yyjson_mut_doc_free(doc); + return (uint8_t*)json; } virtual size_t decode(lua_State* L) { if (!m_slice) return 0; yyjson_read_err err; - yyjson_doc* doc = yyjson_read_opts((char*)m_slice->head(), m_slice->size(), YYJSON_READ_ALLOW_INVALID_UNICODE, nullptr, &err); + yyjson_doc* doc = yyjson_read_opts((char*)m_slice->head(), m_slice->size(), YYJSON_READ_ALLOW_INVALID_UNICODE, m_json->get_alc(), &err); if (!doc) throw invalid_argument(err.msg); int otop = lua_gettop(L); m_json->decode_one(L, yyjson_doc_get_root(doc), true); diff --git a/core/plugins/src/ljson/yyjson.c b/extend/ljson/src/yyjson.c similarity index 99% rename from core/plugins/src/ljson/yyjson.c rename to extend/ljson/src/yyjson.c index 28b2bf3c..c669bab9 100644 --- a/core/plugins/src/ljson/yyjson.c +++ b/extend/ljson/src/yyjson.c @@ -1138,9 +1138,9 @@ bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) { ctx->size = size; ctx->free_list = chunk; - alc->pmalloc = pool_malloc; - alc->prealloc = pool_realloc; - alc->pfree = pool_free; + alc->malloc = pool_malloc; + alc->realloc = pool_realloc; + alc->free = pool_free; alc->ctx = (void *)ctx; return true; } @@ -1156,7 +1156,7 @@ static_inline void unsafe_yyjson_str_pool_release(yyjson_str_pool *pool, yyjson_str_chunk *chunk = pool->chunks, *next; while (chunk) { next = chunk->next; - alc->pfree(alc->ctx, chunk); + alc->free(alc->ctx, chunk); chunk = next; } } @@ -1166,7 +1166,7 @@ static_inline void unsafe_yyjson_val_pool_release(yyjson_val_pool *pool, yyjson_val_chunk *chunk = pool->chunks, *next; while (chunk) { next = chunk->next; - alc->pfree(alc->ctx, chunk); + alc->free(alc->ctx, chunk); chunk = next; } } @@ -1181,7 +1181,7 @@ bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, if (unlikely(len > max_len)) return false; size = len + sizeof(yyjson_str_chunk); size = yyjson_max(pool->chunk_size, size); - chunk = (yyjson_str_chunk *)alc->pmalloc(alc->ctx, size); + chunk = (yyjson_str_chunk *)alc->malloc(alc->ctx, size); if (unlikely(!chunk)) return false; /* insert the new chunk as the head of the linked list */ @@ -1208,7 +1208,7 @@ bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, if (unlikely(count > max_count)) return false; size = (count + 1) * sizeof(yyjson_mut_val); size = yyjson_max(pool->chunk_size, size); - chunk = (yyjson_val_chunk *)alc->pmalloc(alc->ctx, size); + chunk = (yyjson_val_chunk *)alc->malloc(alc->ctx, size); if (unlikely(!chunk)) return false; /* insert the new chunk as the head of the linked list */ @@ -1244,14 +1244,14 @@ void yyjson_mut_doc_free(yyjson_mut_doc *doc) { yyjson_alc alc = doc->alc; unsafe_yyjson_str_pool_release(&doc->str_pool, &alc); unsafe_yyjson_val_pool_release(&doc->val_pool, &alc); - alc.pfree(alc.ctx, doc); + alc.free(alc.ctx, doc); } } yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc) { yyjson_mut_doc *doc; if (!alc) alc = &YYJSON_DEFAULT_ALC; - doc = (yyjson_mut_doc *)alc->pmalloc(alc->ctx, sizeof(yyjson_mut_doc)); + doc = (yyjson_mut_doc *)alc->malloc(alc->ctx, sizeof(yyjson_mut_doc)); if (!doc) return NULL; memset(doc, 0, sizeof(yyjson_mut_doc)); @@ -1512,7 +1512,7 @@ yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval, /* create doc and val pool */ hdr_size = size_align_up(sizeof(yyjson_doc), sizeof(yyjson_val)); buf_size = hdr_size + val_num * sizeof(yyjson_val); - doc = (yyjson_doc *)alc->pmalloc(alc->ctx, buf_size); + doc = (yyjson_doc *)alc->malloc(alc->ctx, buf_size); if (!doc) return NULL; memset(doc, 0, sizeof(yyjson_doc)); val_hdr = (yyjson_val *)(void *)((char *)(void *)doc + hdr_size); @@ -1521,10 +1521,10 @@ yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval, /* create str pool */ if (str_sum > 0) { - str_hdr = (char *)alc->pmalloc(alc->ctx, str_sum); + str_hdr = (char *)alc->malloc(alc->ctx, str_sum); doc->str_pool = str_hdr; if (!str_hdr) { - alc->pfree(alc->ctx, (void *)doc); + alc->free(alc->ctx, (void *)doc); return NULL; } } @@ -5539,7 +5539,7 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.pfree(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) @@ -5559,7 +5559,7 @@ static_noinline yyjson_doc *read_root_single(u8 *hdr, hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; alc_num = hdr_len + 1; /* single value */ - val_hdr = (yyjson_val *)alc.pmalloc(alc.ctx, alc_num * sizeof(yyjson_val)); + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_num * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val = val_hdr + hdr_len; raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW); @@ -5653,7 +5653,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.pfree(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) @@ -5663,7 +5663,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, usize alc_old = alc_len; \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ - val_tmp = (yyjson_val *)alc.prealloc(alc.ctx, (void *)val_hdr, \ + val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ @@ -5700,7 +5700,7 @@ static_inline yyjson_doc *read_root_minify(u8 *hdr, alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4; alc_len = yyjson_min(alc_len, alc_max); - val_hdr = (yyjson_val *)alc.pmalloc(alc.ctx, alc_len * sizeof(yyjson_val)); + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ val = val_hdr + hdr_len; @@ -6041,7 +6041,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ - if (val_hdr) alc.pfree(alc.ctx, (void *)val_hdr); \ + if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) @@ -6051,7 +6051,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, usize alc_old = alc_len; \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ - val_tmp = (yyjson_val *)alc.prealloc(alc.ctx, (void *)val_hdr, \ + val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ @@ -6088,7 +6088,7 @@ static_inline yyjson_doc *read_root_pretty(u8 *hdr, alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_PRETTY_RATIO) + 4; alc_len = yyjson_min(alc_len, alc_max); - val_hdr = (yyjson_val *)alc.pmalloc(alc.ctx, alc_len * sizeof(yyjson_val)); + val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ val = val_hdr + hdr_len; @@ -6468,7 +6468,7 @@ yyjson_doc *yyjson_read_opts(char *dat, err->pos = (usize)(_pos); \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ - if (!has_read_flag(INSITU) && hdr) alc.pfree(alc.ctx, (void *)hdr); \ + if (!has_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ return NULL; \ } while (false) @@ -6500,7 +6500,7 @@ yyjson_doc *yyjson_read_opts(char *dat, if (unlikely(len >= USIZE_MAX - YYJSON_PADDING_SIZE)) { return_err(0, MEMORY_ALLOCATION, "memory allocation failed"); } - hdr = (u8 *)alc.pmalloc(alc.ctx, len + YYJSON_PADDING_SIZE); + hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); if (unlikely(!hdr)) { return_err(0, MEMORY_ALLOCATION, "memory allocation failed"); } @@ -6558,7 +6558,7 @@ yyjson_doc *yyjson_read_opts(char *dat, err->msg = "UTF-16 encoding is not supported"; } } - if (!has_read_flag(INSITU)) alc.pfree(alc.ctx, (void *)hdr); + if (!has_read_flag(INSITU)) alc.free(alc.ctx, (void *)hdr); } return doc; @@ -6601,7 +6601,7 @@ yyjson_doc *yyjson_read_fp(FILE *file, err->pos = 0; \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ - if (buf) alc.pfree(alc.ctx, buf); \ + if (buf) alc.free(alc.ctx, buf); \ return NULL; \ } while (false) @@ -6632,7 +6632,7 @@ yyjson_doc *yyjson_read_fp(FILE *file, if (file_size > 0) { /* read the entire file in one call */ buf_size = (usize)file_size + YYJSON_PADDING_SIZE; - buf = alc.pmalloc(alc.ctx, buf_size); + buf = alc.malloc(alc.ctx, buf_size); if (buf == NULL) { return_err(MEMORY_ALLOCATION, "fail to alloc memory"); } @@ -6654,10 +6654,10 @@ yyjson_doc *yyjson_read_fp(FILE *file, } buf_size += chunk_now; if (!buf) { - buf = alc.pmalloc(alc.ctx, buf_size); + buf = alc.malloc(alc.ctx, buf_size); if (!buf) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); } else { - tmp = alc.prealloc(alc.ctx, buf, buf_size - chunk_now, buf_size); + tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); if (!tmp) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); buf = tmp; } @@ -6679,7 +6679,7 @@ yyjson_doc *yyjson_read_fp(FILE *file, doc->str_pool = (char *)buf; return doc; } else { - alc.pfree(alc.ctx, buf); + alc.free(alc.ctx, buf); return NULL; } @@ -6726,7 +6726,7 @@ const char *yyjson_read_number(const char *dat, hdr = buf; cur = hdr; } else { - hdr = (u8 *)alc->pmalloc(alc->ctx, dat_len + 1); + hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1); cur = hdr; if (unlikely(!hdr)) { return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); @@ -6742,10 +6742,10 @@ const char *yyjson_read_number(const char *dat, #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV if (!read_number(&cur, pre, flg, val, &msg)) { - if (dat_len >= sizeof(buf)) alc->pfree(alc->ctx, hdr); + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); return_err(cur, INVALID_NUMBER, msg); } - if (dat_len >= sizeof(buf)) alc->pfree(alc->ctx, hdr); + if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); if (yyjson_is_raw(val)) val->uni.str = dat; return dat + (cur - hdr); #else @@ -8118,7 +8118,7 @@ static_inline u8 *yyjson_write_single(yyjson_val *val, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ - if (hdr) alc.pfree(alc.ctx, (void *)hdr); \ + if (hdr) alc.free(alc.ctx, (void *)hdr); \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ @@ -8126,7 +8126,7 @@ static_inline u8 *yyjson_write_single(yyjson_val *val, } while (false) #define incr_len(_len) do { \ - hdr = (u8 *)alc.pmalloc(alc.ctx, _len); \ + hdr = (u8 *)alc.malloc(alc.ctx, _len); \ if (!hdr) goto fail_alloc; \ cur = hdr; \ } while (false) @@ -8229,7 +8229,7 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root, *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ - if (hdr) alc.pfree(alc.ctx, hdr); \ + if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) @@ -8241,7 +8241,7 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root, if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ - tmp = (u8 *)alc.prealloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ @@ -8274,7 +8274,7 @@ static_inline u8 *yyjson_write_minify(const yyjson_val *root, alc_len = root->uni.ofs / sizeof(yyjson_val); alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); - hdr = (u8 *)alc.pmalloc(alc.ctx, alc_len); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; @@ -8407,7 +8407,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ - if (hdr) alc.pfree(alc.ctx, hdr); \ + if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) @@ -8419,7 +8419,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ - tmp = (u8 *)alc.prealloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ @@ -8453,7 +8453,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, alc_len = root->uni.ofs / sizeof(yyjson_val); alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); - hdr = (u8 *)alc.pmalloc(alc.ctx, alc_len); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; @@ -8658,7 +8658,7 @@ bool yyjson_val_write_file(const char *path, dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->pfree(alc_ptr->ctx, dat); + alc_ptr->free(alc_ptr->ctx, dat); return suc; } @@ -8684,7 +8684,7 @@ bool yyjson_val_write_fp(FILE *fp, dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->pfree(alc_ptr->ctx, dat); + alc_ptr->free(alc_ptr->ctx, dat); return suc; } @@ -8770,7 +8770,7 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ - if (hdr) alc.pfree(alc.ctx, hdr); \ + if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) @@ -8782,7 +8782,7 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ - tmp = (u8 *)alc.prealloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ @@ -8814,7 +8814,7 @@ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); - hdr = (u8 *)alc.pmalloc(alc.ctx, alc_len); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; @@ -8954,7 +8954,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ - if (hdr) alc.pfree(alc.ctx, hdr); \ + if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) @@ -8966,7 +8966,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ - tmp = (u8 *)alc.prealloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ + tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ @@ -8999,7 +8999,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); - hdr = (u8 *)alc.pmalloc(alc.ctx, alc_len); + hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; @@ -9230,7 +9230,7 @@ bool yyjson_mut_val_write_file(const char *path, dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_file(path, dat, dat_len, err); - alc_ptr->pfree(alc_ptr->ctx, dat); + alc_ptr->free(alc_ptr->ctx, dat); return suc; } @@ -9256,7 +9256,7 @@ bool yyjson_mut_val_write_fp(FILE *fp, dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_fp(fp, dat, dat_len, err); - alc_ptr->pfree(alc_ptr->ctx, dat); + alc_ptr->free(alc_ptr->ctx, dat); return suc; } diff --git a/core/plugins/src/ljson/yyjson.h b/extend/ljson/src/yyjson.h similarity index 99% rename from core/plugins/src/ljson/yyjson.h rename to extend/ljson/src/yyjson.h index b538950b..a9740045 100644 --- a/core/plugins/src/ljson/yyjson.h +++ b/extend/ljson/src/yyjson.h @@ -617,11 +617,11 @@ typedef uint8_t yyjson_subtype; */ typedef struct yyjson_alc { /** Same as libc's malloc(size), should not be NULL. */ - void *(*pmalloc)(void *ctx, size_t size); + void *(*malloc)(void *ctx, size_t size); /** Same as libc's realloc(ptr, size), should not be NULL. */ - void *(*prealloc)(void *ctx, void *ptr, size_t old_size, size_t size); + void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t size); /** Same as libc's free(ptr), should not be NULL. */ - void (*pfree)(void *ctx, void *ptr); + void (*free)(void *ctx, void *ptr); /** A context for malloc/realloc/free, can be NULL. */ void *ctx; } yyjson_alc; @@ -4781,8 +4781,8 @@ yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { yyjson_api_inline void yyjson_doc_free(yyjson_doc *doc) { if (doc) { yyjson_alc alc = doc->alc; - if (doc->str_pool) alc.pfree(alc.ctx, doc->str_pool); - alc.pfree(alc.ctx, doc); + if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); + alc.free(alc.ctx, doc); } } diff --git a/hive.sln b/hive.sln index 5483bd1a..fb6d0345 100644 --- a/hive.sln +++ b/hive.sln @@ -34,6 +34,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ldetour", "extend\ldetour\l {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ljson", "extend\ljson\ljson.vcxproj", "{D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}" + ProjectSection(ProjectDependencies) = postProject + {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} + EndProjectSection +EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "luaxlsx", "extend\luaxlsx\luaxlsx.vcxproj", "{B2999D78-279A-1A53-CC82-74D494144722}" ProjectSection(ProjectDependencies) = postProject {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} @@ -75,6 +80,8 @@ Global {6B01DD6C-28A1-231F-FE9B-1D3F40E3CA3B}.Develop|x64.Build.0 = Develop|x64 {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55}.Develop|x64.ActiveCfg = Develop|x64 {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55}.Develop|x64.Build.0 = Develop|x64 + {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}.Develop|x64.ActiveCfg = Develop|x64 + {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}.Develop|x64.Build.0 = Develop|x64 {B2999D78-279A-1A53-CC82-74D494144722}.Develop|x64.ActiveCfg = Develop|x64 {B2999D78-279A-1A53-CC82-74D494144722}.Develop|x64.Build.0 = Develop|x64 {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF}.Develop|x64.ActiveCfg = Develop|x64 @@ -95,6 +102,7 @@ Global {8A4AC216-FB23-0DA3-834D-E641B3E5D0F7} = {AB6A8F06-D7BB-8801-F776-B07145542020} {6B01DD6C-28A1-231F-FE9B-1D3F40E3CA3B} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {086B9D7D-2973-47BD-8CF0-82BB5A9BFA55} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} + {D2FD53CE-5F95-C3EC-13A1-11D91B9A412C} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {B2999D78-279A-1A53-CC82-74D494144722} = {DEDA8889-2128-69C7-B2CC-3996BC8EB907} {B0AEF51E-FBD8-3472-DB40-2C819DE2E5DF} = {EEE587B4-691E-274F-5E0D-3D4754F2B4B6} {F8DBBBDB-3B80-B4F1-70A8-272978F579EB} = {EEE587B4-691E-274F-5E0D-3D4754F2B4B6}