17 #ifndef AGX_MEMORYPOOL_H 18 #define AGX_MEMORYPOOL_H 20 #include <agx/config.h> 39 #define AGX_CHUNK_ALIGNMENT 128 40 #define AGX_CHUNK_GROW_FACTOR 1.5 48 BatchAllocator(
const char *name =
"BatchAllocator", uint32_t initialChunkSize = 64);
53 void *allocate(uint32_t numBytes);
54 void *allocate(uint32_t numBytes, uint32_t alignment);
55 void deallocate(
void *buffer);
61 void createChunk(uint32_t size);
62 size_t deleteAllChunks();
80 virtual void deallocateVirtual(
void *ptr) = 0;
94 MemoryPool(
const char *name =
"MemoryPool", uint32_t initialChunkSize = 64);
110 void deallocate(T *element);
120 void destroy(T *element);
137 virtual void deallocateVirtual(
void *ptr);
150 template <
typename T>
154 MemoryPoolOld(
size_t initialSize = 0,
size_t chunkSize = 64);
165 void setChunkSize(
size_t size );
168 size_t getChunkSize()
const;
184 T *getElement(
const T& initializer);
191 size_t capacity()
const;
194 size_t memUsed()
const;
205 void garbageCollect();
212 void setAutoGarbageCollect(
int n );
215 int getAutoGarbageCollect( )
const;
220 void setEnableCallConstructor(
bool flag );
225 void setMaxAllocationSize(
size_t numBytes );
233 ChunkVector m_chunks;
237 size_t m_lastAllocatedNumElements;
238 float m_averageNumElements;
240 bool m_callConstructor;
241 size_t m_maxAllocationSize;
244 size_t m_elementIndex;
247 SizeVector m_sizeVector;
252 template <
typename T>
254 m_numUsed(0), m_chunkSize(chunkSize), m_numFlushes(0), m_lastAllocatedNumElements(0), m_averageNumElements(0),
255 m_flushCount(
std::numeric_limits<int>::
max() ), m_callConstructor(true),
256 m_maxAllocationSize(
std::numeric_limits<size_t>::
max() ),
m_capacity( 0 ), m_chunkIndex(0), m_elementIndex(0)
258 size_t numChunks = initialSize ? (initialSize / chunkSize) + 1 : 0;
259 for (
size_t i = 0; i < numChunks; i++)
263 template <
typename T>
269 template <
typename T>
275 template <
typename T>
281 template <
typename T>
288 template <
typename T>
294 template <
typename T>
297 m_callConstructor = flag;
300 template <
typename T>
303 m_maxAllocationSize = numBytes;
307 template <
typename T>
313 template <
typename T>
319 template <
typename T>
325 template <
typename T>
332 else if ( m_elementIndex >= m_sizeVector[ m_chunkIndex] )
339 T* element = m_chunks[m_chunkIndex]+m_elementIndex;
342 if (m_callConstructor)
343 ::new((
void *)element) T();
349 template <
typename T>
357 if (m_numUsed > this->
capacity() || m_elementIndex >= m_sizeVector[ m_chunkIndex] )
361 T* element = m_chunks[m_chunkIndex]+m_elementIndex;
363 if (m_callConstructor)
364 ::new((
void *)element) T(initializer);
370 template <
typename T>
374 while (!m_chunks.
empty())
381 m_lastAllocatedNumElements=0;
385 template <
typename T>
389 T* ptr =
static_cast<T*
>(
allocateBytes(
sizeof(T) * m_chunkSize ) );
392 for (
size_t i=0; i < m_chunkSize; ++i)
393 ::
new( (
void*) &ptr[i]) T();
398 m_capacity += m_chunkSize;
399 m_chunkIndex = m_chunks.
size()-1;
403 template <
typename T>
406 T* ptr = m_chunks.
back();
408 for (
size_t i=0; i < m_sizeVector.
back(); ++i)
414 m_capacity -= m_sizeVector.
back();
419 m_chunkIndex = m_chunks.
size();
422 m_chunkIndex = m_chunks.
size()-1;
427 template <
typename T>
431 m_lastAllocatedNumElements = m_numUsed;
432 m_averageNumElements = m_averageNumElements*0.3f + 0.7f*m_lastAllocatedNumElements;
440 if (m_flushCount > 0 && (
int)m_numFlushes > m_flushCount)
444 template <
typename T>
448 if (!m_numFlushes || !m_chunks.
size())
453 size_t endIdx=m_sizeVector.
size()-1;
455 for(
size_t i=0; i < m_sizeVector.
size(); i++)
457 sum += m_sizeVector[i];
458 if (sum >= m_averageNumElements) {
464 size_t n=m_chunks.
size();
465 for(
size_t i=n-1; i > endIdx; i--)
499 if (m_firstChunk->head + numBytes > m_firstChunk->end)
500 this->createChunk(
std::max(m_chunkSize, numBytes) );
502 void* ret = m_firstChunk->head;
503 m_firstChunk->head += numBytes;
512 m_firstChunk->head += (alignment-1);
513 m_firstChunk->head = (
char *) ((
size_t)m_firstChunk->head & ~((size_t)alignment-1));
514 return this->allocate(numBytes);
538 Chunk *chunk = m_firstChunk;
552 template <
typename T>
557 size_t warningHack = 0;
559 agxAssert(
sizeof(
void*) <= (
sizeof(T)+warningHack) );
562 template <
typename T>
570 template <
typename T>
574 if ( m_freeHead != 0 )
577 m_freeHead = (T*) ( *((
void**)( m_freeHead )) );
581 return static_cast<T *
>( BatchAllocator::allocate(
sizeof(T)) );
584 template <
typename T>
587 void *element = this->allocate();
588 return ::new(element) T();
592 template <
typename T>
597 *((
void**)element) = m_freeHead;
598 m_freeHead = element;
602 template <
typename T>
606 this->deallocate(element);
609 template <
typename T>
612 this->deallocate(static_cast<T *>(ptr));
615 template <
typename T>
620 if ( this->m_freeHead )
629 chunkSizes.
push_back( chunkSizes.
back() + ((c->end - c->buffer) /
sizeof(T)) );
633 bitmap = (
char *)this->allocateBytes( chunkSizes.
back() );
634 memset( bitmap, 0, chunkSizes.
back() );
637 for (
void* l = this->m_freeHead; l != 0; l = *((
void**)l) )
642 while ( tmp && !( tmp->
buffer <= l && l <= tmp->head ) )
651 bitmap[ chunkSizes[idx] + ((
char*)l - tmp->
buffer) /
sizeof(T) ] = 1;
660 size_t end = (tmp->head - tmp->buffer) /
sizeof(T);
661 T* buff =
reinterpret_cast<T*
>(tmp->buffer);
663 for (
size_t e = 0; e <
end; ++e)
664 if ( !bitmap[ chunkSizes[idx] + e] )
669 this->deallocateBytes( bitmap );
681 T* element =
reinterpret_cast<T *
>(c->
buffer);
682 while ( (
char *)element < c->
head )
691 this->m_freeHead = 0;
696 template <
typename T>
703 this->m_freeHead = 0;
AGXTERRAIN_EXPORT void clear()
virtual ~AbstractMemoryPool()
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
void clear()
Clear all memory allocated by this MemoryPoolOld.
MemoryPool(const char *name="MemoryPool", uint32_t initialChunkSize=64)
Constructor.
void setChunkSize(size_t size)
Set the new size of the chunk.
Buffer is deallocated and replaced by an newly allocated empty buffer.
void garbageCollect()
Free memory not in use.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void deallocateBytes(void *buffer, size_t numBytes)
Deallocate a section previously allocated using allocateBytes method.
void setMaxAllocationSize(size_t numBytes)
Set the largest number of bytes a call to new will be.
void resize(size_t size)
Resize the vector, which then enables direct addressing using the bracket '[]' operator.
MemoryPoolOld(size_t initialSize=0, size_t chunkSize=64)
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
void * allocateBytes(size_t numBytes, size_t alignment=16)
Allocate a memory section of specified alignment.
void * allocate(uint32_t numBytes)
void flush()
Return all elements to container.
size_t capacity() const
\ return the total number of elements allocated
void setAutoGarbageCollect(int n)
Set the number of calls to flush which will trigger a call to garbageCollect.
void deallocate(void *buffer)
size_t getChunkSize() const
#define agxAssert1(expr, msg)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
int getAutoGarbageCollect() const
void setEnableCallConstructor(bool flag)
Set to true if the constructor of the elements should be called BEFORE they are returned in getElemen...
void push_back(const T2 &value)
bool isPowerOfTwo(T value)