added ActiveMemoryBlockManager.cpp/.h, splited ActiveIDManager.cpp

This commit is contained in:
2023-09-18 22:27:23 +08:00
parent 2e14c54ca4
commit 389abfefd5
5 changed files with 386 additions and 123 deletions

View File

@@ -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

View 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