📄 arraypool.hpp
字号:
#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)) return; /** * Getting a non-seized element */ ErrorReporter::handleAssert("ArrayPool<T>::getPtr", __FILE__, __LINE__);#endif } else { ptr.i = RNIL; }}template <class T>inlinevoidArrayPool<T>::getPtr(ConstPtr<T> & ptr, Uint32 i, bool CrashOnBoundaryError) const { ptr.i = i; if(i < size){ ptr.p = &theArray[i];#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)) return; /** * Getting a non-seized element */ ErrorReporter::handleAssert("ArrayPool<T>::getPtr", __FILE__, __LINE__);#endif } else { ptr.i = RNIL; }} template <class T>inlineT * ArrayPool<T>::getPtr(Uint32 i, bool CrashOnBoundaryError){ if(i < size){#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)) return &theArray[i]; /** * Getting a non-seized element */ ErrorReporter::handleAssert("ArrayPool<T>::getPtr", __FILE__, __LINE__); return 0;#else return &theArray[i];#endif } else { return 0; }}template <class T>inlineconst T * ArrayPool<T>::getConstPtr(Uint32 i, bool CrashOnBoundaryError) const { if(i < size){#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)) return &theArray[i]; /** * Getting a non-seized element */ ErrorReporter::handleAssert("ArrayPool<T>::getConstPtr", __FILE__,__LINE__); return 0;#else return &theArray[i];#endif } else { return 0; }} /** * Allocate an object from pool - update Ptr * * Return i */template <class T>inlineboolArrayPool<T>::seize(Ptr<T> & ptr){ Uint32 ff = firstFree; if(ff != RNIL){ firstFree = theArray[ff].nextPool; ptr.i = ff; ptr.p = &theArray[ff];#ifdef ARRAY_GUARD if(!BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, ff)){ BitmaskImpl::set(bitmaskSz, theAllocatedBitmask, ff); noOfFree--; return true; } else { /** * Seizing an already seized element */ ErrorReporter::handleAssert("ArrayPool<T>::seize", __FILE__, __LINE__); return false; }#else noOfFree--; return true;#endif } ptr.i = RNIL; ptr.p = NULL; return false;}template <class T>inlineboolArrayPool<T>::seizeId(Ptr<T> & ptr, Uint32 i){ Uint32 ff = firstFree; Uint32 prev = RNIL; while(ff != i && ff != RNIL){ prev = ff; ff = theArray[ff].nextPool; } if(ff != RNIL){ if(prev == RNIL) firstFree = theArray[ff].nextPool; else theArray[prev].nextPool = theArray[ff].nextPool; ptr.i = ff; ptr.p = &theArray[ff];#ifdef ARRAY_GUARD if(!BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, ff)){ BitmaskImpl::set(bitmaskSz, theAllocatedBitmask, ff); noOfFree--; return true; } else { /** * Seizing an already seized element */ ErrorReporter::handleAssert("ArrayPool<T>::seizeId", __FILE__, __LINE__); return false; }#else noOfFree--; return true;#endif } ptr.i = RNIL; ptr.p = NULL; return false;}template <class T>inlineboolArrayPool<T>::findId(Uint32 i) const { if (i >= size) return false; Uint32 ff = firstFree; while(ff != i && ff != RNIL){ ff = theArray[ff].nextPool; } return (ff == RNIL);}template<class T>Uint32ArrayPool<T>::seizeN(Uint32 n){ Uint32 curr = firstFree; Uint32 prev = RNIL; Uint32 sz = 0; while(sz < n && curr != RNIL){ if(theArray[curr].nextPool == (curr + 1)){ sz++; } else { sz = 0; prev = curr; } curr = theArray[curr].nextPool; } if(sz != n){ return RNIL; } const Uint32 base = curr - n; if(base == firstFree){ firstFree = curr; } else { theArray[prev].nextPool = curr; } noOfFree -= n;#ifdef ARRAY_GUARD for(Uint32 j = base; j<curr; j++){ if(!BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, j)){ BitmaskImpl::set(bitmaskSz, theAllocatedBitmask, j); } else { /** * Seizing an already seized element */ ErrorReporter::handleAssert("ArrayPool<T>::seize", __FILE__, __LINE__); return RNIL; } }#endif return base;}template<class T>inlinevoid ArrayPool<T>::releaseN(Uint32 base, Uint32 n){ Uint32 curr = firstFree; Uint32 prev = RNIL; while(curr < base){ prev = curr; curr = theArray[curr].nextPool; } if(curr == firstFree){ firstFree = base; } else { theArray[prev].nextPool = base; } const Uint32 end = base + n; for(Uint32 i = base; i<end; i++){#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)){ BitmaskImpl::clear(bitmaskSz, theAllocatedBitmask, i); } else { /** * Relesing a already released element */ ErrorReporter::handleAssert("ArrayPool<T>::release", __FILE__, __LINE__); return; }#endif theArray[i].nextPool = i + 1; } theArray[end-1].nextPool = curr; noOfFree += n;}template<class T>inlinevoid ArrayPool<T>::releaseList(Uint32 n, Uint32 first, Uint32 last){ if(first < size && last < size){ Uint32 ff = firstFree; firstFree = first; theArray[last].nextPool = ff; noOfFree += n;#ifdef ARRAY_GUARD Uint32 tmp = first; for(Uint32 i = 0; i<n; i++){ if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, tmp)){ BitmaskImpl::clear(bitmaskSz, theAllocatedBitmask, tmp); } else { /** * Relesing a already released element */ ErrorReporter::handleAssert("ArrayPool<T>::releaseList", __FILE__, __LINE__); return; } tmp = theArray[tmp].nextPool; }#endif return; } ErrorReporter::handleAssert("ArrayPool<T>::releaseList", __FILE__, __LINE__);}/** * Return an object to pool */template <class T>inlinevoidArrayPool<T>::release(Uint32 _i){ const Uint32 i = _i; if(i < size){ Uint32 ff = firstFree; theArray[i].nextPool = ff; firstFree = i;#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)){ BitmaskImpl::clear(bitmaskSz, theAllocatedBitmask, i); noOfFree++; return; } /** * Relesing a already released element */ ErrorReporter::handleAssert("ArrayPool<T>::release", __FILE__, __LINE__);#endif noOfFree++; return; } ErrorReporter::handleAssert("ArrayPool<T>::release", __FILE__, __LINE__);}/** * Return an object to pool */template <class T>inlinevoidArrayPool<T>::release(Ptr<T> & ptr){ Uint32 i = ptr.i; if(i < size){ Uint32 ff = firstFree; theArray[i].nextPool = ff; firstFree = i;#ifdef ARRAY_GUARD if(BitmaskImpl::get(bitmaskSz, theAllocatedBitmask, i)){ BitmaskImpl::clear(bitmaskSz, theAllocatedBitmask, i); //assert(noOfFree() == noOfFree2()); noOfFree++; return; } /** * Relesing a already released element */ ErrorReporter::handleAssert("ArrayPool<T>::release", __FILE__, __LINE__);#endif noOfFree++; return; } ErrorReporter::handleAssert("ArrayPool<T>::release", __FILE__, __LINE__);}template <class T>class UnsafeArrayPool : public ArrayPool<T> {public: /** * Update p value for ptr according to i value * ignore if it's allocated or not */ void getPtrForce(Ptr<T> &); void getPtrForce(ConstPtr<T> &) const; T * getPtrForce(Uint32 i); const T * getConstPtrForce(Uint32 i) const; void getPtrForce(Ptr<T> &, Uint32 i); void getPtrForce(ConstPtr<T> &, Uint32 i) const;};template <class T>inlinevoid UnsafeArrayPool<T>::getPtrForce(Ptr<T> & ptr){ Uint32 i = ptr.i; if(i < this->size){ ptr.p = &this->theArray[i]; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); }}template <class T>inlinevoid UnsafeArrayPool<T>::getPtrForce(ConstPtr<T> & ptr) const{ Uint32 i = ptr.i; if(i < this->size){ ptr.p = &this->theArray[i]; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); }}template <class T>inlineT * UnsafeArrayPool<T>::getPtrForce(Uint32 i){ if(i < this->size){ return &this->theArray[i]; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); return 0; }}template <class T>inlineconst T * UnsafeArrayPool<T>::getConstPtrForce(Uint32 i) const { if(i < this->size){ return &this->theArray[i]; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); return 0; }}template <class T>inlinevoid UnsafeArrayPool<T>::getPtrForce(Ptr<T> & ptr, Uint32 i){ ptr.i = i; if(i < this->size){ ptr.p = &this->theArray[i]; return ; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); }}template <class T>inlinevoid UnsafeArrayPool<T>::getPtrForce(ConstPtr<T> & ptr, Uint32 i) const{ ptr.i = i; if(i < this->size){ ptr.p = &this->theArray[i]; return ; } else { ErrorReporter::handleAssert("UnsafeArrayPool<T>::getPtr", __FILE__, __LINE__); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -