diff --git a/inc/hgl/type/ActiveIDManager.h b/inc/hgl/type/ActiveIDManager.h index 4181e5b..cf1d046 100644 --- a/inc/hgl/type/ActiveIDManager.h +++ b/inc/hgl/type/ActiveIDManager.h @@ -18,35 +18,14 @@ namespace hgl private: - bool Create(int *id_list,const int count) - { - if(!id_list||count<=0)return(false); - - for(int i=id_count;i0) - { - idle_list.Push(active_list.GetData(),count); - - active_list.Clear(); - } - - return count; - } + int Release (int *id,int count=1); ///<释放指定量的ID数据(会从Active列表中取出,放入Idle列表中) + int ReleaseAllActive(); ///<释放所有活跃ID(会从Active列表中取出,放入Idle列表中) };//class ActiveIDManager }//namespace hgl diff --git a/inc/hgl/type/ActiveMemoryBlockManager.h b/inc/hgl/type/ActiveMemoryBlockManager.h new file mode 100644 index 0000000..0f2ab28 --- /dev/null +++ b/inc/hgl/type/ActiveMemoryBlockManager.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +namespace hgl +{ + class ActiveMemoryBlockManager + { + uint unit_size; ///<单个数据大小 + MemoryBlock *data_mb; ///<数据内存块 + + ActiveIDManager aim; ///<活跃ID管理器 + + private: + + void ReallocDataBlock(); + + public: + + ActiveMemoryBlockManager(const uint us,AbstractMemoryAllocator *ma=new MemoryAllocator); + virtual ~ActiveMemoryBlockManager(); + + bool SetUnitSize(const uint us); + void Alloc(int c); + + int GetActiveCount ()const{return aim.GetActiveCount();} + int GetIdleCount ()const{return aim.GetIdleCount();} + int GetTotalCount ()const{return aim.GetTotalCount();} + int GetHistoryMaxId ()const{return aim.GetHistoryMaxId();} + + const DataArray &GetActiveArray()const{return aim.GetActiveArray();} + const DataArray &GetIdleArray ()const{return aim.GetIdleArray();} + + public: + + bool WriteData (void *d,const int id); + bool WriteDataArray (void **da,const int *idp,const int count); + bool WriteDataArray (void *da,const int *idp,const int count)const; + + bool GetDataArrayPointer(void **da,const int *idp,const int count)const; ///<根据ID获取数据 + bool GetData(void *da,const int *idp,const int count)const; ///<根据ID获取数据,并整齐排列到一起 + + int CreateActive(int *da,const int count=1); + int CreateIdle(int *da,const int count=1); + + bool Get(int *da,const int count=1); + + int Release(int *id,const int count=1); + int ReleaseAllActive(); + };//class ActiveMemoryBlockManager +}//namespace hgl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 97fb225..4d777f7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,13 @@ SET(TYPE_SOURCE_FILES Type/Collection.cpp) SOURCE_GROUP("DataType\\Collection" FILES ${TYPE_INCLUDE_PATH}/Collection.h ${COLLECTION_SOURCE}) +SET(ACTIVE_MANAGER_FILES ${TYPE_INCLUDE_PATH}/ActiveIDManager.h + ${TYPE_INCLUDE_PATH}/ActiveMemoryBlockManager.h + Type/ActiveIDManager.cpp + Type/ActiveMemoryBlockManager.cpp) + +SOURCE_GROUP("DataType\\ActiveManager" FILES ${ACTIVE_MANAGER_FILES}) + SET(SYSTEM_INFO_SOURCE ${CORE_PLATFORM_INCLUDE_PATH}/SystemInfo.h SystemInfo.cpp) @@ -215,6 +222,8 @@ add_cm_library(CMCore "CM" ${CORE_PLATFORM_HEADER_FILES} ${TYPE_TEMPLATE_HEADER} ${TYPE_SOURCE_FILES} + ${ACTIVE_MANAGER_FILES} + ${IO_SOURCE_FILES} ${MATH_HEADER_FILES} diff --git a/src/Type/ActiveIDManager.cpp b/src/Type/ActiveIDManager.cpp new file mode 100644 index 0000000..f6c86d3 --- /dev/null +++ b/src/Type/ActiveIDManager.cpp @@ -0,0 +1,136 @@ +#include + +namespace hgl +{ + bool ActiveIDManager::Create(int *id_list,const int count) + { + if(!id_list||count<=0)return(false); + + for(int i=id_count;i0) + { + idle_list.Push(active_list.GetData(),count); + + active_list.Clear(); + } + + return count; + } +}//namespace hgl diff --git a/src/Type/ActiveMemoryBlockManager.cpp b/src/Type/ActiveMemoryBlockManager.cpp new file mode 100644 index 0000000..9da98de --- /dev/null +++ b/src/Type/ActiveMemoryBlockManager.cpp @@ -0,0 +1,179 @@ +#include + +namespace hgl +{ + void ActiveMemoryBlockManager::ReallocDataBlock() + { + data_mb->Alloc(aim.GetHistoryMaxId()*unit_size); + } + + ActiveMemoryBlockManager::ActiveMemoryBlockManager(const uint us,AbstractMemoryAllocator *ma) + { + unit_size=us; + + data_mb=new MemoryBlock(ma); + } + + ActiveMemoryBlockManager::~ActiveMemoryBlockManager() + { + delete data_mb; + } + + bool ActiveMemoryBlockManager::SetUnitSize(const uint us) + { + if(unit_size>0) //unit_size已经存在 + return(unit_size==us); + + unit_size=us; + return(true); + } + + void ActiveMemoryBlockManager::Alloc(int c) + { + aim.Alloc(c); + data_mb->Alloc(c*unit_size); + } + + bool ActiveMemoryBlockManager::WriteData(void *d,const int id) + { + if(!d||id<0||id>=aim.GetHistoryMaxId()) + return(false); + + return data_mb->Write(unit_size*id,d,unit_size); + } + + bool ActiveMemoryBlockManager::WriteDataArray(void **da,const int *idp,const int count) + { + if(!da||!idp||count<=0)return(false); + + uint8 *sp=(uint8 *)(data_mb->Get()); + + for(int i=0;i=aim.GetHistoryMaxId()) + return(false); + + memcpy(sp+(*idp)*unit_size,da,unit_size); + + ++da; + ++idp; + } + + return(true); + } + + bool ActiveMemoryBlockManager::WriteDataArray(void *da,const int *idp,const int count)const + { + if(!da||!idp||count<=0)return(false); + + uint8 *sp=(uint8 *)da; + uint8 *tp=(uint8 *)(data_mb->Get()); + + for(int i=0;i=aim.GetHistoryMaxId()) + return(false); + + memcpy(tp+(*idp)*unit_size,sp,unit_size); + + sp+=unit_size; + ++idp; + } + + return(true); + } + + /** + * 根据ID获取数据 + */ + bool ActiveMemoryBlockManager::GetDataArrayPointer(void **da,const int *idp,const int count)const + { + if(!da||!idp||count<=0)return(false); + + uint8 *sp=(uint8 *)(data_mb->Get()); + + for(int i=0;i=aim.GetHistoryMaxId()) + return(false); + + *da=sp+(*idp)*unit_size; + + ++da; + ++idp; + } + + return(true); + } + + /** + * 根据ID获取数据,并整齐排列到一起 + */ + bool ActiveMemoryBlockManager::GetData(void *da,const int *idp,const int count)const + { + if(!da||!idp||count<=0)return(false); + + uint8 *sp=(uint8 *)(data_mb->Get()); + uint8 *tp=(uint8 *)da; + + for(int i=0;i=aim.GetHistoryMaxId()) + return(false); + + memcpy(tp,sp+(*idp)*unit_size,unit_size); + + tp+=unit_size; + ++idp; + } + + return(true); + } + + int ActiveMemoryBlockManager::CreateActive(int *da,const int count) + { + if(!da||count<=0)return(-1); + + int result=aim.CreateActive(da,count); + + if(result!=count) + return(false); + + ReallocDataBlock(); + return(result); + } + + int ActiveMemoryBlockManager::CreateIdle(int *da,const int count) + { + if(count<=0)return 0; + + int result=aim.CreateIdle(da,count); + + if(result!=count) + return(false); + + ReallocDataBlock(); + return(result); + } + + bool ActiveMemoryBlockManager::Get(int *da,const int count) + { + if(!da||count<=0)return(false); + + if(aim.Get(da,count)!=count) + return(false); + + ReallocDataBlock(); + return(true); + } + + int ActiveMemoryBlockManager::Release(int *id,const int count) + { + return aim.Release(id,count); + } + + int ActiveMemoryBlockManager::ReleaseAllActive() + { + return aim.ReleaseAllActive(); + } +}//namespace hgl