This commit is contained in:
codeboss 2025-09-12 06:36:56 +08:00
parent 3fd47f6bfe
commit 6d6df7fa83
7 changed files with 247 additions and 18 deletions

View File

@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35825.156 d17.13
VisualStudioVersion = 17.13.35825.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ECSMemoryPool", "ECSMemoryPool\ECSMemoryPool.vcxproj", "{666EC5D6-70A6-40DA-B92A-364116921A07}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "QtConsoleApplication1", "QtConsoleApplication1\QtConsoleApplication1.vcxproj", "{427B4974-6806-430A-9E25-16B85EADE3B3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -15,6 +17,10 @@ Global
{666EC5D6-70A6-40DA-B92A-364116921A07}.Debug|x64.Build.0 = Debug|x64
{666EC5D6-70A6-40DA-B92A-364116921A07}.Release|x64.ActiveCfg = Release|x64
{666EC5D6-70A6-40DA-B92A-364116921A07}.Release|x64.Build.0 = Release|x64
{427B4974-6806-430A-9E25-16B85EADE3B3}.Debug|x64.ActiveCfg = Debug|x64
{427B4974-6806-430A-9E25-16B85EADE3B3}.Debug|x64.Build.0 = Debug|x64
{427B4974-6806-430A-9E25-16B85EADE3B3}.Release|x64.ActiveCfg = Release|x64
{427B4974-6806-430A-9E25-16B85EADE3B3}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -21,6 +21,7 @@ concept CompenentType = requires(T t, T other, const QJsonObject & in, QJsonObje
template<typename T> class ComponentRefer : protected MemoryElement {
public:
ComponentRefer(ElementControlBlock* data) : MemoryElement(data) { referAdd(); }
ComponentRefer(const ComponentRefer<T> &other) :MemoryElement(other.data_ptr) { referAdd(); }
virtual ~ComponentRefer() { referSub(); }
T* dataLock() {
@ -29,6 +30,7 @@ public:
void unlock() {
MemoryElement::release();
}
};
@ -53,7 +55,7 @@ public:
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="one"></param>
template<CompenentType T> void registerComponent(T one) {
template<CompenentType T> void registerComponent(const T &one) {
std::lock_guard<std::mutex> lockx(_pool_protected_);
auto ptr = new char[T::typeSize()];
@ -62,7 +64,7 @@ public:
// 注册组件数据类型
auto type_code = T::typeCode();
auto type_size = T::typeSize();
_basic_component_memory_example[type_code] = std::make_pair<uint16_t, void*>(type_size, ptr);
_basic_component_memory_example[type_code] = std::make_pair(type_size, (void*)ptr);
}
/// <summary>
@ -91,10 +93,11 @@ public:
slice_ptr->page_refer->lock();
// 裁决元素可用性
if (!slice_ptr->refer_count) {
// 元素可用
// 元素可用,引用占用
slice_ptr->refer_count++;
refer_ptr = slice_ptr;
slice_ptr->page_refer->release();
refer_ptr->page_refer->getActiveCount(1);
// 跳转到元素重用
goto exists_reuse;
}
@ -104,13 +107,22 @@ public:
}
}
}
// 生成新的内存页
auto new_page = new MemoryPage(MemoryElement::rawSize(T::typeSize()), type_code);
_storage_pages->insert(type_code, new_page);
refer_ptr = new_page->getSlicePtr(0);
{
// 生成新的内存页
auto new_page = new MemoryPage(MemoryElement::rawSize(T::typeSize()), type_code);
_storage_pages.insert(type_code, new_page);
refer_ptr = new_page->getSlicePtr(0);
refer_ptr->page_refer->getActiveCount(1);
refer_ptr->refer_count++;
}
exists_reuse:
return ComponentRefer<T>(refer_ptr);
ComponentRefer<T> temp_inst(refer_ptr);
auto data_pointer = temp_inst.dataLock();
refer_ptr->refer_count--;
auto dpair = _basic_component_memory_example[T::typeCode()];
memcpy(data_pointer, dpair.second, dpair.first);
temp_inst.unlock();
return temp_inst;
}
};

View File

@ -1,5 +1,6 @@
#pragma once
#include <mutex>
#include "ecsmemorypool_global.h"
class MemoryPage;
@ -38,7 +39,7 @@ struct ElementControlBlock {
/// <summary>
/// 内存页面16K大小
/// </summary>
class MemoryPage {
class ECSMEMORYPOOL_EXPORT MemoryPage {
private:
PageControlBlock pcb;
unsigned char data_buffer[16 * 1024 - (sizeof(PageControlBlock) / 8 + 1) * 8] = {};
@ -115,14 +116,19 @@ public:
/// <summary>
/// 内存元素访问接口
/// </summary>
class MemoryElement {
private:
class ECSMEMORYPOOL_EXPORT MemoryElement {
protected:
ElementControlBlock* const data_ptr;
public:
static const uint32_t data_buffer_offset;
static uint32_t validOffset();
static uint32_t rawSize(uint32_t data_type_size);
/// <summary>
/// 数据活跃状态
/// </summary>
/// <returns></returns>
static bool isActived(ElementControlBlock* refer);
/// <summary>
/// 构建内存元素访问接口
@ -142,11 +148,6 @@ public:
/// <param name="time_usec"></param>
void accessUpdate(uint64_t time_usec);
/// <summary>
/// 数据活跃状态
/// </summary>
/// <returns></returns>
static bool isActived(ElementControlBlock *refer);
/// <summary>
/// 设置数据活跃状态
/// </summary>

View File

@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{427B4974-6806-430A-9E25-16B85EADE3B3}</ProjectGuid>
<Keyword>QtVS_v304</Keyword>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">10.0</WindowsTargetPlatformVersion>
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v143</PlatformToolset>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt_defaults.props')">
<Import Project="$(QtMsBuild)\qt_defaults.props" />
</ImportGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="QtSettings">
<QtInstall>5.12.11_msvc2017_64</QtInstall>
<QtModules>core</QtModules>
<QtBuildConfig>debug</QtBuildConfig>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="QtSettings">
<QtInstall>5.12.11_msvc2017_64</QtInstall>
<QtModules>core</QtModules>
<QtBuildConfig>release</QtBuildConfig>
</PropertyGroup>
<Target Name="QtMsBuildNotFound" BeforeTargets="CustomBuild;ClCompile" Condition="!Exists('$(QtMsBuild)\qt.targets') or !Exists('$(QtMsBuild)\qt.props')">
<Message Importance="High" Text="QtMsBuild: could not locate qt.targets, qt.props; project may not build correctly." />
</Target>
<ImportGroup Label="ExtensionSettings" />
<ImportGroup Label="Shared" />
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(QtMsBuild)\Qt.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<IncludePath>$(SolutionDir)ECSMemoryPool;$(IncludePath)</IncludePath>
<LibraryPath>$(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>ECSMemoryPool.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'" Label="Configuration">
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>false</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
<Import Project="$(QtMsBuild)\qt.targets" />
</ImportGroup>
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Form Files">
<UniqueIdentifier>{99349809-55BA-4b9d-BF79-8FDBB0286EB3}</UniqueIdentifier>
<Extensions>ui</Extensions>
</Filter>
<Filter Include="Translation Files">
<UniqueIdentifier>{639EADAA-A684-42e4-A9AD-28FC9BCB8F7C}</UniqueIdentifier>
<Extensions>ts</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<QtTouchProperty>
</QtTouchProperty>
</PropertyGroup>
<PropertyGroup Label="QtSettings" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<QtTouchProperty>
</QtTouchProperty>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,54 @@
#include <QtCore/QCoreApplication>
#include <ecs_memorypool.h>
struct ComponentT {
double speed_x = 50, speed_y = 55, speed_z = 7;
static uint64_t typeCode() {
return __LINE__;
}
static uint16_t typeSize() {
return sizeof(ComponentT);
}
void loadFrom(const QJsonObject& o) {}
void saveTo(QJsonObject& o) {}
};
struct ComponentM {
double speed_x = 50, speed_y = 55, speed_z = 7;
static uint64_t typeCode() {
return __LINE__;
}
static uint16_t typeSize() {
return sizeof(ComponentM);
}
void loadFrom(const QJsonObject& o) {}
void saveTo(QJsonObject& o) {}
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
ComponentT o;
ECSMemoryPool pool;
pool.registerComponent<ComponentT>(o);
pool.registerComponent<ComponentM>(ComponentM());
auto refer1 = pool.generate<ComponentT>();
auto refer2 = pool.generate<ComponentT>();
auto refer3 = pool.generate<ComponentT>();
auto mm = refer2;
auto item = *mm.dataLock();
mm.unlock();
auto adf = pool.generate<ComponentM>();
return app.exec();
}