diff --git a/inc/hgl/type/Pool.cpp b/inc/hgl/type/Pool.cpp index 7e1c9b9..8523687 100644 --- a/inc/hgl/type/Pool.cpp +++ b/inc/hgl/type/Pool.cpp @@ -6,36 +6,58 @@ namespace hgl { template - void Pool::PreMalloc(int num) + void Pool::PreMalloc(int num,bool set_to_max_count) { if(num<=0)return; for(int i=0;ihistory_max) - history_max=count; + alloc_count+=num; + if(alloc_count>history_max) + history_max=alloc_count; + + if(set_to_max_count) + max_count=alloc_count; + } + + template + int Pool::SetMaxCount(int mc) + { + if(mc<0) + return(mc); + + if(mc==0) + { + max_count=0; + return 0; + } } template - T Pool::Acquire() + bool Pool::Acquire(T &value) { - T result; - - if(!Inactive.Pop(result)) + if(!Inactive.Pop(value)) { + if(max_count>0&&alloc_count>=max_count) + return(false); + result=Create(); - count++; + alloc_count++; - if(count>history_max) - history_max=count; + if(alloc_count>history_max) + history_max=alloc_count; } - Active.Add(result); + Active.Add(value); + return(true); + } - return(result); + template + bool Pool::Get(T &value) + { + return Inactive.Pop(value); } template @@ -45,10 +67,10 @@ namespace hgl if(!Inactive.Pop(result)) { - count++; + alloc_count++; - if(count>history_max) - history_max=count; + if(alloc_count>history_max) + history_max=alloc_count; } Active.Add(result); @@ -74,11 +96,11 @@ namespace hgl } template - int Pool::Release(T *vl,int count) + int Pool::Release(T *vl,int alloc_count) { int total=0; - for(int i=0;i int Pool::ReleaseAll() { - int count=Active.GetCount(); - T *p=Active.GetData(); + int alloc_count=Active.GetCount(); - for(int i=0;i + void Pool::Clear(T *dp,int dc) + { + for(int i=0;i void Pool::ClearInactive() { - T result; + T *p=Inactive.GetData(); + int ic=Inactive.GetCount(); - count-=Inactive.GetCount(); + alloc_count-=ic; - while(Inactive.Pop(result)) - Clear(result); + Clear(p,ic); + + Inactive.ClearData(); } template void Pool::ClearAll() { - { //不能直接调ClearInactive - T result; + Clear(Inactive.GetData(),Inactive.GetCount()); + Inactive.ClearData(); - count-=Inactive.GetCount(); + Clear(Active.GetData(),Active.GetCount()); + Active.ClearData(); - while(Inactive.Pop(result)) - Clear(result); - } - - { - int n=Active.GetCount(); - T *p=Active.GetData(); - - while(n--) - { - Clear(*p); - ++p; - } - - Active.Clear(); - } - - count=0; + alloc_count=0; } }//namespace hgl #endif//HGL_POOL_CPP diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index ada94bb..4892686 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -18,28 +18,35 @@ namespace hgl List Active; Queue Inactive; - int count; - int history_max; + int alloc_count; ///<已分配的数据数量 + int max_count; ///<最大可分配数量 + int history_max; ///<历史最大数量 protected: virtual T Create()=0; ///<创建数据 virtual void Clear(T)=0; ///<清除数据 + void Clear(T *,int); + public: //属性 - virtual int GetActiveCount() const{return Active.GetCount();} ///<取得活动数据数量 - virtual int GetInactiveCount() const{return Inactive.GetCount();} ///<取得非活动数据数量 - virtual int GetHistoryMaxCount()const{return history_max;} ///<取得历史性最大数据数量 + int GetMaxCount() const{return max_count;} ///<最大最大限制 + int GetActiveCount() const{return Active.GetCount();} ///<取得活动数据数量 + int GetInactiveCount() const{return Inactive.GetCount();} ///<取得非活动数据数量 + int GetHistoryMaxCount()const{return history_max;} ///<取得历史性最大数据数量 public: - Pool(){count=0;history_max=0;} + Pool(){alloc_count=0;history_max=0;max_count=0;} virtual ~Pool()=default; - virtual void PreMalloc(int); ///<预分配空间 + virtual void PreMalloc(int,bool set_to_max=false); ///<预分配空间 - virtual T Acquire(); ///<申请一个数据 + virtual int SetMaxCount(int); ///<设定最大数量限制 + + virtual bool Acquire(T &); ///<申请一个数据(如果没有空余,创建新的) + virtual bool Get(T &); ///<获取一个数据(如果没有空余,返回失败) virtual void Append(T); ///<添加一个数据 virtual bool Release(T); ///<释放一个数据 virtual int Release(T *,int); ///<释放一批数据 diff --git a/inc/hgl/type/Queue.cpp b/inc/hgl/type/Queue.cpp index 661d7de..3c08ac7 100644 --- a/inc/hgl/type/Queue.cpp +++ b/inc/hgl/type/Queue.cpp @@ -10,7 +10,7 @@ namespace hgl template Queue::Queue(int m) { - count=0; + cur_count=0; if(m) { @@ -20,30 +20,59 @@ namespace hgl } else max_count=0; - mem_count=max_count; + alloc_count=max_count; } template Queue::~Queue() { - if(count||max_count)hgl_free(items); + if(cur_count||max_count)hgl_free(items); + } + + template + bool Queue::AllocMemory(int count) + { + if(max_count) + if(count>max_count)return(false); + + if(cur_count) + { + if(count>alloc_count) + { + alloc_count=power_to_2(count); + + items=(T *)hgl_realloc(items,alloc_count*sizeof(T)); + } + } + else + { + alloc_count=power_to_2(count); + + items=hgl_aligned_malloc(alloc_count); + } + + return(true); } /** * 修改队列的最大值 */ template - void Queue::SetMax(int m) + bool Queue::SetMaxCount(int m) { - if(max_count||(!max_count&&count)) - items=(T *)hgl_realloc(items,m*sizeof(T)); - else - items=hgl_aligned_malloc(m); + if(max_count) + { + if(max_count==m)return(true); + } + + if(cur_count) + { + if(cur_count>m) + return(false); + } max_count=m; - mem_count=m; - - if(count>=max_count)count=max_count-1; + return(true); } /** @@ -53,13 +82,13 @@ namespace hgl void Queue::Clear() { if(max_count==0) - if(count) + if(cur_count) { hgl_free(items); - mem_count=0; + alloc_count=0; } - count=0; + cur_count=0; } /** @@ -68,7 +97,7 @@ namespace hgl template void Queue::ClearData() { - count=0; + cur_count=0; } /** @@ -79,7 +108,7 @@ namespace hgl template bool Queue::Peek(T &t) { - if(count) + if(cur_count) { // t=items[0]; memcpy(&t,items,sizeof(T)); @@ -96,15 +125,15 @@ namespace hgl template bool Queue::Delete(int index) { - if(index<0||index>=count) + if(index<0||index>=cur_count) return(false); - count--; + cur_count--; - if(count) + if(cur_count) { - if(index bool Queue::Pop(T &t) { - if(count) + if(cur_count) { // t=items[0]; memcpy(&t,items,sizeof(T)); - count--; + cur_count--; if(max_count==0) { - if(count) + if(cur_count) { //memcpy(items,items+1,count*sizeof(T)); - memmove(items,items+1,count*sizeof(T)); + memmove(items,items+1,cur_count*sizeof(T)); // items=(T *)hgl_realloc(items,count*sizeof(T)); } } else { - memcpy(items,items+1,count*sizeof(T)); + memcpy(items,items+1,cur_count*sizeof(T)); } return(true); @@ -154,32 +183,29 @@ namespace hgl template bool Queue::Push(const T &data) { - if(max_count) - { - if(count>=max_count)return(false); - } - else - { - if(count) - { - if(count+1>mem_count) - { - mem_count=power_to_2(count+1); - - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); - } - } - else - { - items=hgl_aligned_malloc(1); - - mem_count=1; - } - } + if(!AllocMemory(cur_count+1)) + return(false); // items[count++]=data; - memcpy(items+count,&data,sizeof(T)); - count++; + memcpy(items+cur_count,&data,sizeof(T)); + cur_count++; + + return(true); + } + + /** + * 向队列中压入一堆数据 + * @param dp 要放入的数据指针 + * @param dc 要放入的数据数量 + */ + template + bool Queue::Push(T *dp,const int dc) + { + if(!AllocMemory(cur_count+dc)) + return(false); + + memcpy(items+cur_count,dp,sizeof(T)*dc); + cur_count+=dc; return(true); } @@ -187,11 +213,11 @@ namespace hgl template int Queue::Find(const T &data) { - if(count<=0) + if(cur_count<=0) return(-1); T *p=items; - for(int i=0;i void Queue::operator =(const Queue &ori) { - if(ori.count==0)return; + if(ori.cur_count==0)return; Clear(); - max_count=ori.count; - count=ori.count; + max_count=ori.cur_count; + cur_count=ori.cur_count; if(max_count==0) - mem_count=count; + alloc_count=cur_count; else - mem_count=max_count; + alloc_count=max_count; - items=hgl_aligned_malloc(mem_count); + items=hgl_aligned_malloc(alloc_count); - memcpy(items,ori.items,mem_count*sizeof(T)); + memcpy(items,ori.items,alloc_count*sizeof(T)); } } @@ -228,7 +254,7 @@ namespace hgl template void QueueObject::Clear() { - int n=Queue::count; + int n=Queue::cur_count; while(n--) delete Queue::items[n]; diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index c83514b..cc899c2 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -13,39 +13,42 @@ namespace hgl { protected: - int max_count; - int mem_count; - int count; + int max_count; ///<最大可分配空间数量 + int alloc_count; ///<已分配的空间数量 + int cur_count; T *items; + protected: + + bool AllocMemory(int); + public: //属性 - T *GetData()const{return items;} ///<取得原始数据 - int GetCount()const{return count;} ///<取得数据数量 + T * GetData ()const{return items;} ///<取得原始数据 + int GetCount ()const{return cur_count;} ///<取得数据数量 - int GetMax()const{return max_count;} ///<取得最大数量 - void SetMax(int); ///<设置最大数量 - - T *GetData(){return items;} ///<取得原始数据 + int GetMaxCount ()const{return max_count;} ///<取得最大数量 + bool SetMaxCount (int); ///<设置最大数量 public: //方法 Queue(int=0); virtual ~Queue(); - bool Peek(T &); ///<尝试访问一个数据 - bool Pop(T &); ///<弹出一个数据 - bool Push(const T &); ///<压入一个数据 + bool Peek (T &); ///<尝试访问一个数据 + bool Pop (T &); ///<弹出一个数据 + bool Push (const T &); ///<压入一个数据 + bool Push (T *,int); ///<压入一批数据 - int Find(const T &); ///<查找队列中这个数据的编号 - bool Delete(int); ///<删除队列中指定编号的数据 + int Find (const T &); ///<查找队列中这个数据的编号 + bool Delete (int); ///<删除队列中指定编号的数据 - void Clear(); ///<清除所有数据 - void ClearData(); ///<清除所有数据,但不释放内存 + void Clear (); ///<清除所有数据 + void ClearData (); ///<清除所有数据,但不释放内存 - bool GetItem(int n,T &ti) ///<取得指定项数据 + bool GetItem (int n,T &ti) ///<取得指定项数据 { - if(n<0||n>=count)return(false); + if(n<0||n>=cur_count)return(false); ti=items[n]; return(true); @@ -65,7 +68,7 @@ namespace hgl T *operator[](int n)const { - if(n<0||n>=Queue::count)return(nullptr); + if(n<0||n>=Queue::cur_count)return(nullptr); return Queue::items[n]; } diff --git a/inc/hgl/type/Stack.cpp b/inc/hgl/type/Stack.cpp index 69db7e0..89d1c7c 100644 --- a/inc/hgl/type/Stack.cpp +++ b/inc/hgl/type/Stack.cpp @@ -11,7 +11,7 @@ namespace hgl template Stack::Stack(int m) { - count=0; + cur_count=0; if(m) { @@ -22,38 +22,38 @@ namespace hgl else max_count=0; - mem_count=max_count; + alloc_count=max_count; } template Stack::~Stack() { - if(count||max_count)hgl_free(items); + if(cur_count||max_count)hgl_free(items); } /** * 修改堆栈的最大值 */ template - void Stack::SetMax(int m) + void Stack::SetMaxCount(int m) { if(m<=0)return; - if(mem_count==0) + if(alloc_count==0) { - mem_count=power_to_2(m); - items=hgl_aligned_malloc(mem_count); + alloc_count=power_to_2(m); + items=hgl_aligned_malloc(alloc_count); } else - if(mem_count=max_count)count=max_count-1; + if(cur_count>=max_count)cur_count=max_count-1; } template @@ -64,7 +64,7 @@ namespace hgl if(c>max_count) return(false); - count=c; + cur_count=c; return(true); } @@ -75,21 +75,21 @@ namespace hgl void Stack::Clear() { if(max_count==0) - if(count) + if(cur_count) { hgl_free(items); - mem_count=0; + alloc_count=0; } - count=0; + cur_count=0; } template bool Stack::GetItem(int n,T &data) { - if(n<0||n>=count) + if(n<0||n>=cur_count) { - LOG_ERROR(OS_TEXT("从堆栈中按索引<") + OSString(n) + OS_TEXT(">取数据,超出正常范围<")+OSString(count) + OS_TEXT(">")); + LOG_ERROR(OS_TEXT("从堆栈中按索引<") + OSString(n) + OS_TEXT(">取数据,超出正常范围<")+OSString(cur_count) + OS_TEXT(">")); return(false); } @@ -106,10 +106,10 @@ namespace hgl template bool Stack::Peek(T &t) { - if(count) + if(cur_count) { - memcpy(&t,items+(count-1),sizeof(T)); -// t=items[count-1]; + memcpy(&t,items+(cur_count-1),sizeof(T)); +// t=items[cur_count-1]; return(true); } else @@ -124,22 +124,22 @@ namespace hgl template bool Stack::Pop(T &t) { - if(count) + if(cur_count) { -// t=items[--count]; - count--; - memcpy(&t,items+count,sizeof(T)); +// t=items[--cur_count]; + cur_count--; + memcpy(&t,items+cur_count,sizeof(T)); if(max_count==0) { - if(count==0) + if(cur_count==0) { hgl_free(items); - mem_count=0; + alloc_count=0; } //else - //items=(T *)hgl_realloc(items,count*sizeof(T)); + //items=(T *)hgl_realloc(items,cur_count*sizeof(T)); } return(true); @@ -159,30 +159,30 @@ namespace hgl { if(max_count) { - if(count>=max_count)return(false); + if(cur_count>=max_count)return(false); } else { - if(count) + if(cur_count) { - if(count+1>mem_count) + if(cur_count+1>alloc_count) { - mem_count=power_to_2(count+1); + alloc_count=power_to_2(cur_count+1); - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); + items=(T *)hgl_realloc(items,alloc_count*sizeof(T)); } } else { items=hgl_aligned_malloc(1); - mem_count=1; + alloc_count=1; } } -// items[count++]=data; - memcpy(items+count,&data,sizeof(T)); - count++; +// items[cur_count++]=data; + memcpy(items+cur_count,&data,sizeof(T)); + cur_count++; return(true); } @@ -195,33 +195,33 @@ namespace hgl * @return false 放入数据失败 */ template - bool Stack::MultiPush(T *data,int data_count) + bool Stack::Push(T *data,int data_count) { if(max_count) { - if(count>=max_count)return(false); + if(cur_count>=max_count)return(false); } else { - if(count) + if(cur_count) { - if(count+data_count>mem_count) + if(cur_count+data_count>alloc_count) { - mem_count=power_to_2(count+data_count); + alloc_count=power_to_2(cur_count+data_count); - items=(T *)hgl_realloc(items,mem_count*sizeof(T)); + items=(T *)hgl_realloc(items,alloc_count*sizeof(T)); } } else { items=hgl_aligned_malloc(data_count); - mem_count=data_count; + alloc_count=data_count; } } - memcpy(items+count,data,data_count*sizeof(T)); - count+=data_count; + memcpy(items+cur_count,data,data_count*sizeof(T)); + cur_count+=data_count; return(true); } @@ -229,21 +229,21 @@ namespace hgl template void Stack::operator =(const Stack &ori) { - if(ori.count==0)return; + if(ori.cur_count==0)return; Clear(); - max_count=ori.count; - count=ori.count; + max_count=ori.cur_count; + cur_count=ori.cur_count; if(max_count==0) - mem_count=count; + alloc_count=cur_count; else - mem_count=max_count; + alloc_count=max_count; - items=hgl_aligned_malloc(mem_count); + items=hgl_aligned_malloc(alloc_count); - memcpy(items,ori.items,mem_count*sizeof(T)); + memcpy(items,ori.items,alloc_count*sizeof(T)); } }//namespace hgl @@ -252,7 +252,7 @@ namespace hgl template void StackObject::Clear() { - for(int i=0;icount;i++) + for(int i=0;icur_count;i++) delete this->items[i]; Stack::Clear(); diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index 8d3926c..6633d11 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -14,19 +14,19 @@ namespace hgl protected: int max_count; - int mem_count; - int count; + int alloc_count; + int cur_count; T *items; public: //属性 - int GetCount() const{return count;} ///<取得堆栈中数据的个数 - bool SetCount(int c); ///<直接设置堆栈中数据的个数 + int GetCount () const{return cur_count;} ///<取得堆栈中数据的个数 + bool SetCount (int c); ///<直接设置堆栈中数据的个数 - int GetMax () const{return max_count;} ///<取得堆栈中的最大数据个数 - void SetMax (int); ///<设置堆栈中的最大数据个数 + int GetMaxCount () const{return max_count;} ///<取得堆栈中的最大数据个数 + void SetMaxCount (int); ///<设置堆栈中的最大数据个数 - T * GetData () {return items;} ///<取得原始数据 + T * GetData () {return items;} ///<取得原始数据 public: //方法 @@ -36,7 +36,7 @@ namespace hgl bool Peek(T &); ///<尝试访问一个数据 virtual bool Pop(T &); ///<弹出一个数据 bool Push(T &); ///<压入一个数据 - bool MultiPush(T *,int); ///<放入多个数据 + bool Push(T *,int); ///<压入多个数据 virtual void Clear(); ///<清除所有数据