diff --git a/ECSMemoryPool.sln b/ECSMemoryPool.sln index b3fa658..bb6e6bf 100644 --- a/ECSMemoryPool.sln +++ b/ECSMemoryPool.sln @@ -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 diff --git a/ECSMemoryPool/ecs_memorypool.h b/ECSMemoryPool/ecs_memorypool.h index 0c0762a..59e0fe2 100644 --- a/ECSMemoryPool/ecs_memorypool.h +++ b/ECSMemoryPool/ecs_memorypool.h @@ -21,6 +21,7 @@ concept CompenentType = requires(T t, T other, const QJsonObject & in, QJsonObje template class ComponentRefer : protected MemoryElement { public: ComponentRefer(ElementControlBlock* data) : MemoryElement(data) { referAdd(); } + ComponentRefer(const ComponentRefer &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: /// /// /// - template void registerComponent(T one) { + template void registerComponent(const T &one) { std::lock_guard 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(type_size, ptr); + _basic_component_memory_example[type_code] = std::make_pair(type_size, (void*)ptr); } /// @@ -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(refer_ptr); + ComponentRefer 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; } }; diff --git a/ECSMemoryPool/memory_pages.h b/ECSMemoryPool/memory_pages.h index 17313fa..83d5960 100644 --- a/ECSMemoryPool/memory_pages.h +++ b/ECSMemoryPool/memory_pages.h @@ -1,5 +1,6 @@ #pragma once #include +#include "ecsmemorypool_global.h" class MemoryPage; @@ -38,7 +39,7 @@ struct ElementControlBlock { /// /// ÄÚ´æÒ³Ãæ16K´óС /// -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: /// /// ÄÚ´æÔªËØ·ÃÎÊ½Ó¿Ú /// -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); + /// + /// Êý¾Ý»îԾ״̬ + /// + /// + static bool isActived(ElementControlBlock* refer); /// /// ¹¹½¨ÄÚ´æÔªËØ·ÃÎÊ½Ó¿Ú @@ -142,11 +148,6 @@ public: /// void accessUpdate(uint64_t time_usec); - /// - /// Êý¾Ý»îԾ״̬ - /// - /// - static bool isActived(ElementControlBlock *refer); /// /// ÉèÖÃÊý¾Ý»îԾ״̬ /// diff --git a/QtConsoleApplication1/QtConsoleApplication1.vcxproj b/QtConsoleApplication1/QtConsoleApplication1.vcxproj new file mode 100644 index 0000000..a2e13ef --- /dev/null +++ b/QtConsoleApplication1/QtConsoleApplication1.vcxproj @@ -0,0 +1,113 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {427B4974-6806-430A-9E25-16B85EADE3B3} + QtVS_v304 + 10.0 + 10.0 + $(MSBuildProjectDirectory)\QtMsBuild + + + + Application + v143 + true + Unicode + + + Application + v143 + false + true + Unicode + + + + + + + 5.12.11_msvc2017_64 + core + debug + + + 5.12.11_msvc2017_64 + core + release + + + + + + + + + + + + + + + + + $(SolutionDir)ECSMemoryPool;$(IncludePath) + $(SolutionDir)$(Platform)\$(Configuration);$(LibraryPath) + + + + + + stdcpp20 + + + ECSMemoryPool.lib;%(AdditionalDependencies) + + + + + true + Level3 + true + true + + + Console + true + + + + + true + Level3 + true + true + true + true + + + Console + false + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/QtConsoleApplication1/QtConsoleApplication1.vcxproj.filters b/QtConsoleApplication1/QtConsoleApplication1.vcxproj.filters new file mode 100644 index 0000000..0d52bb1 --- /dev/null +++ b/QtConsoleApplication1/QtConsoleApplication1.vcxproj.filters @@ -0,0 +1,31 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {99349809-55BA-4b9d-BF79-8FDBB0286EB3} + ui + + + {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} + ts + + + + + Source Files + + + + diff --git a/QtConsoleApplication1/QtConsoleApplication1.vcxproj.user b/QtConsoleApplication1/QtConsoleApplication1.vcxproj.user new file mode 100644 index 0000000..41a709a --- /dev/null +++ b/QtConsoleApplication1/QtConsoleApplication1.vcxproj.user @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/QtConsoleApplication1/main.cpp b/QtConsoleApplication1/main.cpp new file mode 100644 index 0000000..86776d9 --- /dev/null +++ b/QtConsoleApplication1/main.cpp @@ -0,0 +1,54 @@ +#include +#include + +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(o); + pool.registerComponent(ComponentM()); + + auto refer1 = pool.generate(); + auto refer2 = pool.generate(); + auto refer3 = pool.generate(); + auto mm = refer2; + + auto item = *mm.dataLock(); + mm.unlock(); + + auto adf = pool.generate(); + + return app.exec(); +}