Skip to content

Commit

Permalink
修改ljson为动态库,并修复内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
DSKY\toney.yin committed Feb 2, 2024
1 parent 0197326 commit 48d9c31
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 88 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion core/hive/src/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions core/plugins/lplugins.mak
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions core/plugins/lplugins.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
<ClInclude Include="src\lcrypt\sha1.h"/>
<ClInclude Include="src\lcrypt\sha2.h"/>
<ClInclude Include="src\lcrypt\xxtea.h"/>
<ClInclude Include="src\ljson\ljson.h"/>
<ClInclude Include="src\ljson\yyjson.h"/>
<ClInclude Include="src\lrandom\random.hpp"/>
<ClInclude Include="src\ltimer\croncpp.h"/>
<ClInclude Include="src\ltimer\ltimer.h"/>
Expand All @@ -55,8 +53,6 @@
<ClCompile Include="src\lcrypt\sha1.c"/>
<ClCompile Include="src\lcrypt\sha2.c"/>
<ClCompile Include="src\lcrypt\xxtea.c"/>
<ClCompile Include="src\ljson\ljson.cpp"/>
<ClCompile Include="src\ljson\yyjson.c"/>
<ClCompile Include="src\lrandom\lrandom.cpp"/>
<ClCompile Include="src\lstdfs\lstdfs.cpp"/>
<ClCompile Include="src\ltimer\ltimer.cpp"/>
Expand Down
15 changes: 0 additions & 15 deletions core/plugins/lplugins.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@
<ClInclude Include="src\lcrypt\xxtea.h">
<Filter>lcrypt</Filter>
</ClInclude>
<ClInclude Include="src\ljson\ljson.h">
<Filter>ljson</Filter>
</ClInclude>
<ClInclude Include="src\ljson\yyjson.h">
<Filter>ljson</Filter>
</ClInclude>
<ClInclude Include="src\lrandom\random.hpp">
<Filter>lrandom</Filter>
</ClInclude>
Expand Down Expand Up @@ -141,12 +135,6 @@
<ClCompile Include="src\lcrypt\xxtea.c">
<Filter>lcrypt</Filter>
</ClCompile>
<ClCompile Include="src\ljson\ljson.cpp">
<Filter>ljson</Filter>
</ClCompile>
<ClCompile Include="src\ljson\yyjson.c">
<Filter>ljson</Filter>
</ClCompile>
<ClCompile Include="src\lrandom\lrandom.cpp">
<Filter>lrandom</Filter>
</ClCompile>
Expand Down Expand Up @@ -185,9 +173,6 @@
<Filter Include="lcrypt">
<UniqueIdentifier>{6779B38A-0EC6-088E-3974-E2A164AE1520}</UniqueIdentifier>
</Filter>
<Filter Include="ljson">
<UniqueIdentifier>{D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}</UniqueIdentifier>
</Filter>
<Filter Include="lrandom">
<UniqueIdentifier>{3FF84F4B-3548-31CF-586F-ADCF4C5716CF}</UniqueIdentifier>
</Filter>
Expand Down
38 changes: 38 additions & 0 deletions extend/ljson/ljson.lmak
Original file line number Diff line number Diff line change
@@ -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"
132 changes: 132 additions & 0 deletions extend/ljson/ljson.mak
Original file line number Diff line number Diff line change
@@ -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:
79 changes: 79 additions & 0 deletions extend/ljson/ljson.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Develop|x64">
<Configuration>Develop</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\ljson.h"/>
<ClInclude Include="src\yyjson.h"/>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ljson.cpp"/>
<ClCompile Include="src\yyjson.c"/>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{D2FD53CE-5F95-C3EC-13A1-11D91B9A412C}</ProjectGuid>
<RootNamespace>ljson</RootNamespace>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>ljson</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>11.0.50727.1</_ProjectFileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'">
<TargetName>ljson</TargetName>
<OutDir>$(SolutionDir)temp\bin\$(Platform)\</OutDir>
<IntDir>$(SolutionDir)temp\$(ProjectName)\$(Platform)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Develop|x64'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\lua\lua;..\luakit\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;LUA_BUILD_AS_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>Default</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<PrecompiledHeader></PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<CompileAs>Default</CompileAs>
<LanguageStandard>stdcpp17</LanguageStandard>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>$(SolutionDir)library\$(Platform);;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
<ImportLibrary>$(SolutionDir)library\$(Platform)\$(TargetName).lib</ImportLibrary>
<ProgramDatabaseFile>$(SolutionDir)temp\$(ProjectName)\$(Platform)\$(TargetName).pdb</ProgramDatabaseFile>
<AdditionalDependencies>lua.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ForceFileOutput>
</ForceFileOutput>
</Link>
<PreBuildEvent>
</PreBuildEvent>
<PostBuildEvent>
<Command>copy /y $(TargetPath) $(SolutionDir)bin</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
27 changes: 27 additions & 0 deletions extend/ljson/ljson.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="src\ljson.h">
<Filter>inc</Filter>
</ClInclude>
<ClInclude Include="src\yyjson.h">
<Filter>inc</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\ljson.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\yyjson.c">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="inc">
<UniqueIdentifier>{CF9F3FDE-7326-F1D8-E642-05F0E07A3695}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{25D902C2-4283-AB8C-FBAC-54DFA101AD31}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define LUA_LIB

#include "ljson.h"

namespace ljson {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 48d9c31

Please sign in to comment.