add MemoryAllocator/MemoryBlock codes.
This commit is contained in:
@@ -48,9 +48,6 @@ using os_char =wchar_t;
|
|||||||
#define HGL_MAX_PATH MAX_PATH
|
#define HGL_MAX_PATH MAX_PATH
|
||||||
|
|
||||||
#define HGL_MEM_ALIGN 16 //内存对齐字节数
|
#define HGL_MEM_ALIGN 16 //内存对齐字节数
|
||||||
|
|
||||||
#define HGL_GL_WINDOW_INCLUDE_FILE <hgl/platform/WinOpenGL.h> //指定OpenGL窗口引用头文件
|
|
||||||
#define HGL_GL_WINDOW_CLASS WinGLWindow //指定OpenGL窗口类名称
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// == 目前MINGW和MSVC在以下接口上应该能保持一致了
|
// == 目前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_realloc(ptr,size) _aligned_realloc(ptr,size,HGL_MEM_ALIGN)
|
||||||
#define hgl_free _aligned_free
|
#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(n<=0)return(nullptr);
|
||||||
|
|
||||||
|
if(align_size==0)
|
||||||
|
align_size=HGL_MEM_ALIGN;
|
||||||
|
|
||||||
return _aligned_malloc(n,align_size);
|
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));
|
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)
|
if(n<=0)
|
||||||
{
|
{
|
||||||
@@ -84,6 +84,9 @@ void *hgl_align_realloc(void *ptr,size_t n,size_t align_size)
|
|||||||
return(nullptr);
|
return(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(align_size==0)
|
||||||
|
align_size=HGL_MEM_ALIGN;
|
||||||
|
|
||||||
if(ptr)
|
if(ptr)
|
||||||
return _aligned_realloc(ptr,n,align_size);
|
return _aligned_realloc(ptr,n,align_size);
|
||||||
else
|
else
|
||||||
|
@@ -1,220 +1,9 @@
|
|||||||
#ifndef HGL_COLLECTION_INCLUDE
|
#ifndef HGL_COLLECTION_INCLUDE
|
||||||
#define HGL_COLLECTION_INCLUDE
|
#define HGL_COLLECTION_INCLUDE
|
||||||
|
|
||||||
#include<hgl/type/DataType.h>
|
#include<hgl/type/MemoryBlock.h>
|
||||||
namespace hgl
|
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(size<alloc_size)
|
|
||||||
return(true);
|
|
||||||
|
|
||||||
alloc_size=ComputeAllocSize(size);
|
|
||||||
|
|
||||||
memory_block=hgl_align_realloc(memory_block,alloc_size,alloc_unit_size); //hgl_align_realloc支持传入空的memory_block,所以无需判断malloc/realloc
|
|
||||||
|
|
||||||
return memory_block;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Free() override
|
|
||||||
{
|
|
||||||
if(memory_block)
|
|
||||||
{
|
|
||||||
hgl_free(memory_block);
|
|
||||||
memory_block=nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
data_size=0;
|
|
||||||
alloc_size=0;
|
|
||||||
}
|
|
||||||
};//class MemoryAllocator:public AbstractMemoryAllocator
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 内存数据块
|
|
||||||
*/
|
|
||||||
class MemoryBlock
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
|
|
||||||
AbstractMemoryAllocator *memory_allocator;
|
|
||||||
|
|
||||||
void *temp_block;
|
|
||||||
uint64 temp_size;
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
MemoryBlock(AbstractMemoryAllocator *ama)
|
|
||||||
{
|
|
||||||
memory_allocator=ama;
|
|
||||||
|
|
||||||
temp_block=nullptr;
|
|
||||||
temp_size=0;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~MemoryBlock()
|
|
||||||
{
|
|
||||||
hgl_free(temp_block);
|
|
||||||
SAFE_CLEAR(memory_allocator); //会同步释放内存
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual 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()
|
|
||||||
{
|
|
||||||
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:
|
public:
|
||||||
|
|
||||||
Collection(AbstractMemoryBlock *adb)
|
Collection(MemoryBlock *mb)
|
||||||
{
|
{
|
||||||
memory_block=adb;
|
memory_block=mb;
|
||||||
data_count=0;
|
data_count=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,7 +54,7 @@ namespace hgl
|
|||||||
if(!memory_block->Alloc((data_count+source_size)*sizeof(T)))
|
if(!memory_block->Alloc((data_count+source_size)*sizeof(T)))
|
||||||
return(false);
|
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()
|
virtual void Clear()
|
||||||
|
66
inc/hgl/type/MemoryAllocator.h
Normal file
66
inc/hgl/type/MemoryAllocator.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#ifndef HGL_MEMORY_ALLOCATOR_INCLUDE
|
||||||
|
#define HGL_MEMORY_ALLOCATOR_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/type/DataType.h>
|
||||||
|
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
|
37
inc/hgl/type/MemoryBlock.h
Normal file
37
inc/hgl/type/MemoryBlock.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef HGL_MEMORY_BLOCK_INCLUDE
|
||||||
|
#define HGL_MEMORY_BLOCK_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/type/MemoryAllocator.h>
|
||||||
|
#include<string.h>
|
||||||
|
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
|
@@ -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)
|
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)
|
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})
|
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------------------------------------------------------------
|
##I/O------------------------------------------------------------
|
||||||
SET(IO_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/io)
|
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})
|
SOURCE_GROUP("Time\\Source Files" FILES ${BASE_TIME_SOURCE_FILES})
|
||||||
|
|
||||||
SET(BASE_THREAD_SOURCE Other/ThreadFunc.cpp)
|
SET(BASE_THREAD_SOURCE Other/ThreadFunc.cpp)
|
||||||
|
SOURCE_GROUP("Thread" FILES ${BASE_THREAD_SOURCE})
|
||||||
SOURCE_GROUP("Thread" FILES ${BASE_OTHER_SOURCE})
|
|
||||||
|
|
||||||
file(GLOB BASE_PLUG_IN_HEADER ${CMCORE_ROOT_INCLUDE_PATH}/hgl/plugin/*.h)
|
file(GLOB BASE_PLUG_IN_HEADER ${CMCORE_ROOT_INCLUDE_PATH}/hgl/plugin/*.h)
|
||||||
file(GLOB BASE_PLUG_IN_SOURCE PlugIn/*.cpp)
|
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\\Header Files" FILES ${BASE_LOG_HEADER})
|
||||||
SOURCE_GROUP("Log\\Source Files" FILES ${BASE_LOG_SOURCE})
|
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_HEADER_FILES}
|
||||||
${COLOR_SOURCE_FILES}
|
${COLOR_SOURCE_FILES}
|
||||||
@@ -166,6 +197,7 @@ add_cm_library(CMCore "CM" #${SYSTEM_INFO_SOURCE}
|
|||||||
${BASE_TIME_HEADER_FILES}
|
${BASE_TIME_HEADER_FILES}
|
||||||
${BASE_TIME_SOURCE_FILES}
|
${BASE_TIME_SOURCE_FILES}
|
||||||
|
|
||||||
|
${BASE_OTHER_SOURCE}
|
||||||
${BASE_THREAD_SOURCE}
|
${BASE_THREAD_SOURCE}
|
||||||
|
|
||||||
${BASE_PLUG_IN_HEADER}
|
${BASE_PLUG_IN_HEADER}
|
||||||
|
80
src/Other/MemoryAllocator.cpp
Normal file
80
src/Other/MemoryAllocator.cpp
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include<hgl/type/MemoryAllocator.h>
|
||||||
|
|
||||||
|
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<alloc_size)
|
||||||
|
return(true);
|
||||||
|
|
||||||
|
alloc_size=ComputeAllocSize(size);
|
||||||
|
|
||||||
|
memory_block=hgl_align_realloc(memory_block,alloc_size,alloc_unit_size); //hgl_align_realloc支持传入空的memory_block,所以无需判断malloc/realloc
|
||||||
|
|
||||||
|
if(!memory_block)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
data_size=size;
|
||||||
|
|
||||||
|
return(memory_block);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemoryAllocator::Free()
|
||||||
|
{
|
||||||
|
if(memory_block)
|
||||||
|
{
|
||||||
|
hgl_free(memory_block);
|
||||||
|
memory_block=nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
data_size=0;
|
||||||
|
alloc_size=0;
|
||||||
|
}
|
||||||
|
}//namespace hgl
|
78
src/Other/MemoryBlock.cpp
Normal file
78
src/Other/MemoryBlock.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include<hgl/type/MemoryBlock.h>
|
||||||
|
#include<string.h>
|
||||||
|
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); //<2F><>ͬ<EFBFBD><CDAC><EFBFBD>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
Reference in New Issue
Block a user