From 50994106163330c2d3a6f158beced02be6cc0355 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 7 Mar 2021 20:01:13 +0800 Subject: [PATCH] add MemoryAllocator/MemoryBlock codes. --- inc/hgl/platform/os/MSWindows.h | 13 +- inc/hgl/type/Collection.h | 219 +------------------------------- inc/hgl/type/MemoryAllocator.h | 66 ++++++++++ inc/hgl/type/MemoryBlock.h | 37 ++++++ src/CMakeLists.txt | 42 +++++- src/Other/MemoryAllocator.cpp | 80 ++++++++++++ src/Other/MemoryBlock.cpp | 78 ++++++++++++ 7 files changed, 310 insertions(+), 225 deletions(-) create mode 100644 inc/hgl/type/MemoryAllocator.h create mode 100644 inc/hgl/type/MemoryBlock.h create mode 100644 src/Other/MemoryAllocator.cpp create mode 100644 src/Other/MemoryBlock.cpp diff --git a/inc/hgl/platform/os/MSWindows.h b/inc/hgl/platform/os/MSWindows.h index 50a21d0..d2530ed 100644 --- a/inc/hgl/platform/os/MSWindows.h +++ b/inc/hgl/platform/os/MSWindows.h @@ -48,9 +48,6 @@ using os_char =wchar_t; #define HGL_MAX_PATH MAX_PATH #define HGL_MEM_ALIGN 16 //内存对齐字节数 - -#define HGL_GL_WINDOW_INCLUDE_FILE //指定OpenGL窗口引用头文件 -#define HGL_GL_WINDOW_CLASS WinGLWindow //指定OpenGL窗口类名称 //-------------------------------------------------------------------------------------------------- // == 目前MINGW和MSVC在以下接口上应该能保持一致了 @@ -59,10 +56,13 @@ using os_char =wchar_t; #define hgl_realloc(ptr,size) _aligned_realloc(ptr,size,HGL_MEM_ALIGN) #define hgl_free _aligned_free -void *hgl_align_malloc(size_t n,size_t align_size) +inline void *hgl_align_malloc(size_t n,size_t align_size) { if(n<=0)return(nullptr); + if(align_size==0) + align_size=HGL_MEM_ALIGN; + return _aligned_malloc(n,align_size); } @@ -74,7 +74,7 @@ inline T *hgl_align_malloc(size_t n) return (T *)_aligned_malloc(n*sizeof(T),alignof(T)); } -void *hgl_align_realloc(void *ptr,size_t n,size_t align_size) +inline void *hgl_align_realloc(void *ptr,size_t n,size_t align_size) { if(n<=0) { @@ -84,6 +84,9 @@ void *hgl_align_realloc(void *ptr,size_t n,size_t align_size) return(nullptr); } + if(align_size==0) + align_size=HGL_MEM_ALIGN; + if(ptr) return _aligned_realloc(ptr,n,align_size); else diff --git a/inc/hgl/type/Collection.h b/inc/hgl/type/Collection.h index 9852862..d55476b 100644 --- a/inc/hgl/type/Collection.h +++ b/inc/hgl/type/Collection.h @@ -1,220 +1,9 @@ #ifndef HGL_COLLECTION_INCLUDE #define HGL_COLLECTION_INCLUDE -#include +#include namespace hgl { - /** - * 抽像内存分配器 - */ - class AbstractMemoryAllocator - { - protected: - - void *memory_block; ///<内存地址 - - uint64 data_size; ///<数据长度 - - uint64 alloc_unit_size; ///<分配单位长度(分配长度必须为此值的整倍数) - uint64 alloc_size; ///<实际分配数据长度 - - public: - - virtual const uint64 GetSize ()const{return data_size;} ///<获取所需求的内存大小 - virtual const uint64 GetAllocUnitSize()const{return alloc_unit_size;} ///<获取分配单元大小 - virtual const uint64 GetAllocSize ()const{return alloc_size;} ///<获取实际分配的内存大小 - - virtual void * Get (){return memory_block;} ///<取得内存块地址 - virtual void * Get (const uint64 offset) - { - return (uint8 *)memory_block+offset; - } - - public: - - /** - * 设置分配单元长度 - */ - virtual void SetAllocUnitSize(const uint64 size) - { - alloc_unit_size=size; - } - - /** - * 计算需要分配的内存容量 - */ - virtual uint64 ComputeAllocSize(const uint64 size) - { - uint64 result; - - if(alloc_unit_size==0) - { - result=power_to_2(size); - } - else - if(alloc_unit_size==1) - { - result=size; - } - else - { - result=(size+alloc_unit_size-1)/alloc_unit_size; - result*=alloc_unit_size; - } - - return result; - } - - public: - - AbstractMemoryAllocator() - { - memory_block=nullptr; - - data_size=0; - alloc_unit_size=0; - alloc_size=0; - } - - virtual ~AbstractMemoryAllocator()=default; - - virtual bool Alloc (const uint64 size)=0; ///<分配指定空间的数据 - virtual void Free ()=0; ///<释放数据空间 - virtual void Clear (){data_size=0;} - };//class AbstractMemoryAllocator - - class MemoryAllocator:public AbstractMemoryAllocator - { - public: - - using AbstractMemoryAllocator::AbstractMemoryAllocator; - - virtual ~MemoryAllocator() - { - Free(); - } - - bool Alloc(const uint64 size) override - { - if(size<=0)return(false); - if(size<=data_size)return(true); - - if(sizeGetSize();} - virtual void * Get () {return memory_allocator->Get();} - virtual void * Get (const uint64 offset) {return memory_allocator->Get(offset);} - - virtual void Clear() - { - memory_allocator->Clear(); - } - - virtual bool Alloc(const uint64 size,const uint64 uint_size=0) - { - memory_allocator->SetAllocUnitSize(uint_size); - memory_allocator->Alloc(size); - } - - virtual bool Write(const uint64 target,const void *source,const uint64 size) - { - if(target+size>memory_allocator->GetSize())return(false); - if(!source||!size)return(false); - - memcpy(Get(target),source,size); - return(true); - } - - virtual bool Write(const uint64 target,MemoryBlock *source,const uint64 offset,const uint64 size) - { - if(offset+size>source->GetSize())return(false); - - return Write(target,source->Get(offset),size); - } - - virtual bool Exchange (const uint64 target,const uint64 source,const uint64 size) - { - if(size<=0)return(false); - if(target==source)return(true); - - if(size>temp_size) - { - temp_size=memory_allocator->ComputeAllocSize(size); - temp_block=hgl_align_realloc(temp_block,temp_size,memory_allocator->GetAllocUnitSize()); - } - - memcpy( temp_block, - Get(target), - size); - - memcpy( Get(target), - Get(source), - size); - - memcpy( Get(source), - temp_block, - size); - - return(true); - } - - void Move (const uint64 target,const uint64 source,const uint64 size) - { - memmove(Get(target), - Get(source), - size); - } - };//class MemoryBlock:public AbstractDataBlock - /** * 数据合集 */ @@ -228,9 +17,9 @@ namespace hgl public: - Collection(AbstractMemoryBlock *adb) + Collection(MemoryBlock *mb) { - memory_block=adb; + memory_block=mb; data_count=0; } @@ -265,7 +54,7 @@ namespace hgl if(!memory_block->Alloc((data_count+source_size)*sizeof(T))) return(false); - return memory_block->Write(data_count*sizeof(T),GetMemoryBlock()); + return memory_block->Write(data_count*sizeof(T),GetMemoryBlock(),0,source_size); } virtual void Clear() diff --git a/inc/hgl/type/MemoryAllocator.h b/inc/hgl/type/MemoryAllocator.h new file mode 100644 index 0000000..0dd90d4 --- /dev/null +++ b/inc/hgl/type/MemoryAllocator.h @@ -0,0 +1,66 @@ +#ifndef HGL_MEMORY_ALLOCATOR_INCLUDE +#define HGL_MEMORY_ALLOCATOR_INCLUDE + +#include +namespace hgl +{ + /** + * 抽像内存分配器 + */ + class AbstractMemoryAllocator + { + protected: + + void *memory_block; ///<内存地址 + + uint64 data_size; ///<数据长度 + + uint64 alloc_unit_size; ///<分配单位长度(分配长度必须为此值的整倍数) + uint64 alloc_size; ///<实际分配数据长度 + + public: + + virtual const uint64 GetSize ()const{return data_size;} ///<获取所需求的内存大小 + virtual const uint64 GetAllocUnitSize()const{return alloc_unit_size;} ///<获取分配单元大小 + virtual const uint64 GetAllocSize ()const{return alloc_size;} ///<获取实际分配的内存大小 + + virtual void * Get (){return memory_block;} ///<取得内存块地址 + virtual void * Get (const uint64 offset) + { + return (uint8 *)memory_block+offset; + } + + public: + + /** + * 设置分配单元长度 + */ + virtual void SetAllocUnitSize(const uint64 size); + + /** + * 计算需要分配的内存容量 + */ + virtual uint64 ComputeAllocSize(const uint64 size); + + public: + + AbstractMemoryAllocator(); + virtual ~AbstractMemoryAllocator()=default; + + virtual bool Alloc (const uint64 size)=0; ///<分配指定空间的数据 + virtual void Free ()=0; ///<释放数据空间 + virtual void Clear (){data_size=0;} + };//class AbstractMemoryAllocator + + class MemoryAllocator:public AbstractMemoryAllocator + { + public: + + using AbstractMemoryAllocator::AbstractMemoryAllocator; + virtual ~MemoryAllocator(); + + virtual bool Alloc(const uint64 size); + virtual void Free(); + };//class MemoryAllocator:public AbstractMemoryAllocator +}//namespace hgl +#endif//HGL_MEMORY_ALLOCATOR_INCLUDE diff --git a/inc/hgl/type/MemoryBlock.h b/inc/hgl/type/MemoryBlock.h new file mode 100644 index 0000000..998ab89 --- /dev/null +++ b/inc/hgl/type/MemoryBlock.h @@ -0,0 +1,37 @@ +#ifndef HGL_MEMORY_BLOCK_INCLUDE +#define HGL_MEMORY_BLOCK_INCLUDE + +#include +#include +namespace hgl +{ + /** + * 内存数据块 + */ + class MemoryBlock + { + protected: + + AbstractMemoryAllocator *memory_allocator; + + void *temp_block; + uint64 temp_size; + + public: + + MemoryBlock(AbstractMemoryAllocator *ama); + virtual ~MemoryBlock(); + + virtual const uint64 GetSize ()const {return memory_allocator->GetSize();} + virtual void * Get () {return memory_allocator->Get();} + virtual void * Get (const uint64 offset) {return memory_allocator->Get(offset);} + + virtual void Clear (); + virtual bool Alloc (const uint64 size,const uint64 uint_size=0); + virtual bool Write (const uint64 target,const void *source,const uint64 size); + virtual bool Write (const uint64 target,MemoryBlock *source,const uint64 offset,const uint64 size); + virtual bool Exchange (const uint64 target,const uint64 source,const uint64 size); + virtual void Move (const uint64 target,const uint64 source,const uint64 size); + };//class MemoryBlock:public AbstractDataBlock +}//namespace hgl +#endif//HGL_MEMORY_BLOCK_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 679b419..00a77ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,33 @@ -SET(TYPE_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/type) +SET(CORE_PLATFORM_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/platform) + +SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_INCLUDE_PATH}/Platform.h + ${CORE_PLATFORM_INCLUDE_PATH}/os/CharType.h) + +IF(WIN32) + + SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_HEADER_FILES} ${CORE_PLATFORM_INCLUDE_PATH}/os/MSWindows.h) + +ELSEIF(UNIX) + + IF(APPLE) + SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_HEADER_FILES} ${CORE_PLATFORM_INCLUDE_PATH}/os/Apple.h) + ELSEIF(ANDROID) + SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_HEADER_FILES} ${CORE_PLATFORM_INCLUDE_PATH}/os/Android.h) + ELSE() + ENDIF() + + SET(CORE_PLATFORM_HEADER_FILES ${CORE_PLATFORM_HEADER_FILES} ${CORE_PLATFORM_INCLUDE_PATH}/os/PosixThread.h) +ENDIF() + +SOURCE_GROUP("Platform" FILES ${CORE_PLATFORM_HEADER_FILES}) + + +SET(TYPE_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/type) file(GLOB TYPE_TEMPLATE_HEADER ${TYPE_INCLUDE_PATH}/*.h) -SET(SYSTEM_INFO_SOURCE SystemInfo.cpp) +SET(SYSTEM_INFO_SOURCE ${CORE_PLATFORM_INCLUDE_PATH}/SystemInfo.h + SystemInfo.cpp) SET(MATH_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/math) @@ -56,6 +81,12 @@ SOURCE_GROUP("Text\\Source Files" FILES ${TEXT_SOURCE_FILES}) SOURCE_GROUP("Datatype\\Template" FILES ${TYPE_TEMPLATE_HEADER}) +SET(BASE_OTHER_SOURCE ${TYPE_INCLUDE_PATH}/MemoryBlock.h + ${TYPE_INCLUDE_PATH}/MemoryAllocator.h + Other/MemoryBlock.cpp + Other/MemoryAllocator.cpp) +SOURCE_GROUP("DataType\\Template\\Memory" FILES ${BASE_OTHER_SOURCE}) + ##I/O------------------------------------------------------------ SET(IO_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/io) @@ -129,8 +160,7 @@ SOURCE_GROUP("Time\\Header Files" FILES ${BASE_TIME_HEADER_FILES}) SOURCE_GROUP("Time\\Source Files" FILES ${BASE_TIME_SOURCE_FILES}) SET(BASE_THREAD_SOURCE Other/ThreadFunc.cpp) - -SOURCE_GROUP("Thread" FILES ${BASE_OTHER_SOURCE}) +SOURCE_GROUP("Thread" FILES ${BASE_THREAD_SOURCE}) file(GLOB BASE_PLUG_IN_HEADER ${CMCORE_ROOT_INCLUDE_PATH}/hgl/plugin/*.h) file(GLOB BASE_PLUG_IN_SOURCE PlugIn/*.cpp) @@ -144,7 +174,8 @@ file(GLOB BASE_LOG_SOURCE Log/*.*) SOURCE_GROUP("Log\\Header Files" FILES ${BASE_LOG_HEADER}) SOURCE_GROUP("Log\\Source Files" FILES ${BASE_LOG_SOURCE}) -add_cm_library(CMCore "CM" #${SYSTEM_INFO_SOURCE} +add_cm_library(CMCore "CM" ${CORE_PLATFORM_HEADER_FILES} + #${SYSTEM_INFO_SOURCE} ${COLOR_HEADER_FILES} ${COLOR_SOURCE_FILES} @@ -166,6 +197,7 @@ add_cm_library(CMCore "CM" #${SYSTEM_INFO_SOURCE} ${BASE_TIME_HEADER_FILES} ${BASE_TIME_SOURCE_FILES} + ${BASE_OTHER_SOURCE} ${BASE_THREAD_SOURCE} ${BASE_PLUG_IN_HEADER} diff --git a/src/Other/MemoryAllocator.cpp b/src/Other/MemoryAllocator.cpp new file mode 100644 index 0000000..7dffb9f --- /dev/null +++ b/src/Other/MemoryAllocator.cpp @@ -0,0 +1,80 @@ +#include + +namespace hgl +{ + AbstractMemoryAllocator::AbstractMemoryAllocator() + { + memory_block=nullptr; + + data_size=0; + alloc_unit_size=0; + alloc_size=0; + } + + void AbstractMemoryAllocator::SetAllocUnitSize(const uint64 size) + { + alloc_unit_size=size; + } + + /** + * 计算需要分配的内存容量 + */ + uint64 AbstractMemoryAllocator::ComputeAllocSize(const uint64 size) + { + uint64 result; + + if(alloc_unit_size==0) + { + result=power_to_2(size); + } + else + if(alloc_unit_size==1) + { + result=size; + } + else + { + result=(size+alloc_unit_size-1)/alloc_unit_size; + result*=alloc_unit_size; + } + + return result; + } + + MemoryAllocator::~MemoryAllocator() + { + Free(); + } + + bool MemoryAllocator::Alloc(const uint64 size) + { + if(size<=0)return(false); + if(size<=data_size)return(true); + + if(size +#include +namespace hgl +{ + MemoryBlock::MemoryBlock(AbstractMemoryAllocator *ama) + { + memory_allocator=ama; + + temp_block=nullptr; + temp_size=0; + } + + MemoryBlock::~MemoryBlock() + { + hgl_free(temp_block); + SAFE_CLEAR(memory_allocator); //ͬͷڴ + } + + void MemoryBlock::Clear() + { + memory_allocator->Clear(); + } + + bool MemoryBlock::Alloc(const uint64 size,const uint64 uint_size) + { + memory_allocator->SetAllocUnitSize(uint_size); + return memory_allocator->Alloc(size); + } + + bool MemoryBlock::Write(const uint64 target,const void *source,const uint64 size) + { + if(target+size>memory_allocator->GetSize())return(false); + if(!source||!size)return(false); + + memcpy(Get(target),source,size); + return(true); + } + + bool MemoryBlock::Write(const uint64 target,MemoryBlock *source,const uint64 offset,const uint64 size) + { + if(offset+size>source->GetSize())return(false); + + return Write(target,source->Get(offset),size); + } + + bool MemoryBlock::Exchange (const uint64 target,const uint64 source,const uint64 size) + { + if(size<=0)return(false); + if(target==source)return(true); + + if(size>temp_size) + { + temp_size=memory_allocator->ComputeAllocSize(size); + temp_block=hgl_align_realloc(temp_block,temp_size,memory_allocator->GetAllocUnitSize()); + } + + memcpy( temp_block, + Get(target), + size); + + memcpy( Get(target), + Get(source), + size); + + memcpy( Get(source), + temp_block, + size); + + return(true); + } + + void MemoryBlock::Move (const uint64 target,const uint64 source,const uint64 size) + { + memmove(Get(target), + Get(source), + size); + } +}//namespace hgl