added ActiveMemoryBlockManager.cpp/.h, splited ActiveIDManager.cpp
This commit is contained in:
parent
2e14c54ca4
commit
389abfefd5
@ -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;i<id_count+count;i++)
|
||||
{
|
||||
*id_list=i;
|
||||
++id_list;
|
||||
}
|
||||
|
||||
id_count+=count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
bool Create(int *id_list,const int count);
|
||||
|
||||
public:
|
||||
|
||||
ActiveIDManager()
|
||||
{
|
||||
id_count=0;
|
||||
}
|
||||
|
||||
ActiveIDManager(){id_count=0;}
|
||||
~ActiveIDManager()=default;
|
||||
|
||||
void Alloc(int c)
|
||||
{
|
||||
active_list.PreAlloc(c);
|
||||
idle_list.PreAlloc(c);
|
||||
}
|
||||
void Alloc(int c);
|
||||
|
||||
int GetActiveCount ()const{return active_list.GetCount();}
|
||||
int GetIdleCount ()const{return idle_list.GetCount();}
|
||||
@ -58,106 +37,14 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 创建若干ID,并置于活跌ID列表中。(注:不从闲置列表中获取)
|
||||
* @return 成功添加的个数
|
||||
*/
|
||||
int CreateActive(int *id,const int count=1)
|
||||
{
|
||||
if(!id||count<=0)return(0);
|
||||
int CreateActive(int *id,const int count=1); ///<创建若干ID,并置于活跌ID列表中。(注:不从闲置列表中获取)
|
||||
int CreateIdle (int *idp=nullptr,const int count=1); ///<创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的ID)
|
||||
int CreateIdle (const int count=1); ///<创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的ID)
|
||||
|
||||
if(!Create(id,count))return(0);
|
||||
bool Get (int *id,const int count=1); ///<激活指定量的ID数据(优先从Idle中取,没有不会创建新的。激活后会被放入Active列表)
|
||||
bool GetOrCreate(int *id,const int count=1); ///<激活指定量的ID数据(优从从Idle中取,如果不够则创建新的。激活后会被放入Active列表)
|
||||
|
||||
return active_list.Add(id,count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的ID)
|
||||
* @return 成功添加的个数
|
||||
*/
|
||||
int CreateIdle(const int count=1)
|
||||
{
|
||||
if(count<=0)return(0);
|
||||
|
||||
const int new_count=idle_list.GetCount()+count;
|
||||
|
||||
idle_list.PreAlloc(new_count);
|
||||
|
||||
int *end=idle_list.end();
|
||||
|
||||
if(!Create(end,count))return(0);
|
||||
|
||||
idle_list.SetCount(new_count);
|
||||
|
||||
return(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活指定量的ID数据(优先从Idle中取,没有不会创建新的。激活后会被放入Active列表)
|
||||
*/
|
||||
bool Get(int *id,const int count=1)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
if(!idle_list.Pop(id,count))
|
||||
return(false);
|
||||
|
||||
active_list.Add(id,count);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活指定量的ID数据(优从从Idle中取,如果不够则创建新的。激活后会被放入Active列表)
|
||||
*/
|
||||
bool GetOrCreate(int *id,const int count=1)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
if(idle_list.GetCount()<count)
|
||||
CreateIdle(count-idle_list.GetCount());
|
||||
|
||||
return Get(id,count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放指定量的ID数据(会从Active列表中取出,放入Idle列表中)
|
||||
*/
|
||||
int Release(int *id,int count=1)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
int result=0;
|
||||
|
||||
while(count--)
|
||||
{
|
||||
if(active_list.Delete(*id))
|
||||
{
|
||||
idle_list.Push(*id);
|
||||
++result;
|
||||
}
|
||||
|
||||
++id;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放所有活跃ID
|
||||
*/
|
||||
int ReleaseAllActive()
|
||||
{
|
||||
const int count=active_list.GetCount();
|
||||
|
||||
if(count>0)
|
||||
{
|
||||
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
|
||||
|
52
inc/hgl/type/ActiveMemoryBlockManager.h
Normal file
52
inc/hgl/type/ActiveMemoryBlockManager.h
Normal file
@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/ActiveIDManager.h>
|
||||
#include<hgl/type/MemoryBlock.h>
|
||||
|
||||
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<int> &GetActiveArray()const{return aim.GetActiveArray();}
|
||||
const DataArray<int> &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
|
@ -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}
|
||||
|
136
src/Type/ActiveIDManager.cpp
Normal file
136
src/Type/ActiveIDManager.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include<hgl/type/ActiveIDManager.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
bool ActiveIDManager::Create(int *id_list,const int count)
|
||||
{
|
||||
if(!id_list||count<=0)return(false);
|
||||
|
||||
for(int i=id_count;i<id_count+count;i++)
|
||||
{
|
||||
*id_list=i;
|
||||
++id_list;
|
||||
}
|
||||
|
||||
id_count+=count;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
void ActiveIDManager::Alloc(int c)
|
||||
{
|
||||
active_list.PreAlloc(c);
|
||||
idle_list.PreAlloc(c);
|
||||
}
|
||||
|
||||
int ActiveIDManager::CreateActive(int *id,const int count)
|
||||
{
|
||||
if(!id||count<=0)return(0);
|
||||
|
||||
if(!Create(id,count))return(0);
|
||||
|
||||
return active_list.Add(id,count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的ID)
|
||||
* @return 成功添加的个数
|
||||
*/
|
||||
int ActiveIDManager::CreateIdle(int *idp,const int count)
|
||||
{
|
||||
if(count<=0)return(0);
|
||||
|
||||
const int new_count=idle_list.GetCount()+count;
|
||||
|
||||
idle_list.PreAlloc(new_count);
|
||||
|
||||
int *end=idle_list.end();
|
||||
|
||||
if(!Create(end,count))return(0);
|
||||
|
||||
if(idp)
|
||||
hgl_cpy(idp,end,count);
|
||||
|
||||
idle_list.SetCount(new_count);
|
||||
|
||||
return(count);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的ID)
|
||||
* @return 成功添加的个数
|
||||
*/
|
||||
int ActiveIDManager::CreateIdle(const int count)
|
||||
{
|
||||
return CreateIdle(nullptr,count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活指定量的ID数据(优先从Idle中取,没有不会创建新的。激活后会被放入Active列表)
|
||||
*/
|
||||
bool ActiveIDManager::Get(int *id,const int count)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
if(!idle_list.Pop(id,count))
|
||||
return(false);
|
||||
|
||||
active_list.Add(id,count);
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活指定量的ID数据(优从从Idle中取,如果不够则创建新的。激活后会被放入Active列表)
|
||||
*/
|
||||
bool ActiveIDManager::GetOrCreate(int *id,const int count)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
if(idle_list.GetCount()<count)
|
||||
CreateIdle(count-idle_list.GetCount());
|
||||
|
||||
return Get(id,count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放指定量的ID数据(会从Active列表中取出,放入Idle列表中)
|
||||
*/
|
||||
int ActiveIDManager::Release(int *id,int count)
|
||||
{
|
||||
if(!id||count<=0)return(false);
|
||||
|
||||
int result=0;
|
||||
|
||||
while(count--)
|
||||
{
|
||||
if(active_list.Delete(*id))
|
||||
{
|
||||
idle_list.Push(*id);
|
||||
++result;
|
||||
}
|
||||
|
||||
++id;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放所有活跃ID
|
||||
*/
|
||||
int ActiveIDManager::ReleaseAllActive()
|
||||
{
|
||||
const int count=active_list.GetCount();
|
||||
|
||||
if(count>0)
|
||||
{
|
||||
idle_list.Push(active_list.GetData(),count);
|
||||
|
||||
active_list.Clear();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}//namespace hgl
|
179
src/Type/ActiveMemoryBlockManager.cpp
Normal file
179
src/Type/ActiveMemoryBlockManager.cpp
Normal file
@ -0,0 +1,179 @@
|
||||
#include<hgl/type/ActiveMemoryBlockManager.h>
|
||||
|
||||
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<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=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<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=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<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=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<count;i++)
|
||||
{
|
||||
if(*idp<0||*idp>=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
|
Loading…
x
Reference in New Issue
Block a user