47 #ifndef _MIRA_POOLALLOCATOR_H_ 48 #define _MIRA_POOLALLOCATOR_H_ 54 #include <boost/noncopyable.hpp> 135 mFreeList(NULL), mNextCapacity(poolCapacity), mAllocatedBytes(0), mGrowPools(growPools) {
136 mItemSize =
itemSize >
sizeof(FreeListItem*) ?
itemSize :
sizeof(FreeListItem*);
143 foreach(uint8* p, mMemoryBlocks)
148 mFreeList(NULL), mItemSize(0), mNextCapacity(0), mAllocatedBytes(0), mGrowPools(
false) {
160 std::swap(mMemoryBlocks, other.mMemoryBlocks);
161 std::swap(mFreeList, other.mFreeList);
162 std::swap(mItemSize, other.mItemSize);
163 std::swap(mNextCapacity, other.mNextCapacity);
164 std::swap(mAllocatedBytes, other.mAllocatedBytes);
165 std::swap(mGrowPools, other.mGrowPools);
171 return mAllocatedBytes;
182 FreeListItem* curr = mFreeList;
185 freeBytes += mItemSize;
233 uint8* memory = NULL;
239 assert(mFreeList!=NULL);
242 memory =
reinterpret_cast<uint8*
>(mFreeList);
244 mFreeList = mFreeList->next;
252 T* obj =
new(p) T(val);
267 FreeListItem* item =
reinterpret_cast<FreeListItem*
>(p);
270 item->next = mFreeList;
298 return new(obj) T(val);
318 const size_type blockSize = mNextCapacity * mItemSize;
319 uint8* currentBlock = (uint8*) malloc(blockSize);
320 if(currentBlock==NULL)
322 << blockSize <<
" bytes of memory");
325 FreeListItem* oldHead = mFreeList;
328 uint8* p = currentBlock;
329 FreeListItem* l =
reinterpret_cast<FreeListItem*
>(p);
334 for(
size_type i=1; i<mNextCapacity; ++i, p+=mItemSize)
336 l->next =
reinterpret_cast<FreeListItem*
>(p);
341 mMemoryBlocks.push_back(currentBlock);
342 mAllocatedBytes += blockSize;
345 mNextCapacity = mNextCapacity*2;
350 std::list<uint8*> mMemoryBlocks;
352 struct FreeListItem {
355 FreeListItem* mFreeList;
pointer allocate(size_type n, const void *hint=0)
Allocate a block of storage and returns the pointer, note that the object is not constructed.
Definition: PoolAllocator.h:229
Macro for iterating over all elements in a container.
T & reference
Reference to element.
Definition: PoolAllocator.h:111
Typedefs for OS independent basic data types.
T * construct(const_reference val)
Allocates a new feature and returns its pointer This method is provided for convenience.
Definition: PoolAllocator.h:296
size_type itemSize() const MIRA_NOEXCEPT_OR_NOTHROW
Returns the size of each item in bytes.
Definition: PoolAllocator.h:218
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Provides a pool allocator that is compatible to STL allocators.
Definition: PoolAllocator.h:92
size_type unusedMemory() const
Definition: PoolAllocator.h:179
void destroy(T *p)
Destroy an object. Notice that this does not deallocate space for the element.
Definition: PoolAllocator.h:256
void construct(pointer p, const_reference val)
Constructs an object of type T on the location p using its copy constructor to initialize its value t...
Definition: PoolAllocator.h:251
size_type allocatedMemory() const
Definition: PoolAllocator.h:169
std::size_t size_type
Quantities of elements.
Definition: PoolAllocator.h:102
T * pointer
Pointer to element.
Definition: PoolAllocator.h:108
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
size_type max_size() const MIRA_NOEXCEPT_OR_NOTHROW
Returns the maximum number of elements of type T (the template parameter) that could be allocated by ...
Definition: PoolAllocator.h:211
void destruct(pointer p)
Destroys the object and deallocates the memory.
Definition: PoolAllocator.h:307
void deallocate(pointer p, size_type n)
Releases a block of storage previously allocated with member allocate and not yet released...
Definition: PoolAllocator.h:262
std::ptrdiff_t difference_type
Difference between two pointers.
Definition: PoolAllocator.h:105
Commonly used exception classes.
T * construct()
Allocates and constructs a new object and returns its pointer.
Definition: PoolAllocator.h:283
#define MIRA_NOEXCEPT_OR_NOTHROW
Definition: NoExcept.h:99
const T & const_reference
Constant reference to element.
Definition: PoolAllocator.h:117
~PoolAllocator()
Destructor.
Definition: PoolAllocator.h:141
PoolAllocator(size_type poolCapacity=32, bool growPools=true, size_type itemSize=sizeof(T))
Creates a new PoolAllocator.
Definition: PoolAllocator.h:134
PoolAllocator & operator=(PoolAllocator &&other) noexcept
Definition: PoolAllocator.h:152
pointer address(reference x) const
Returns the address of x.
Definition: PoolAllocator.h:196
T value_type
Element type.
Definition: PoolAllocator.h:99
size_type usedMemory() const
Definition: PoolAllocator.h:174
const_pointer address(const_reference x) const
Returns the address of x.
Definition: PoolAllocator.h:201
void swap(PoolAllocator &other)
Definition: PoolAllocator.h:158
PoolAllocator(PoolAllocator &&other) noexcept
Definition: PoolAllocator.h:147
const T * const_pointer
Reference to element.
Definition: PoolAllocator.h:114