25 #ifndef HBRALLOCATOR_H
26 #define HBRALLOCATOR_H
28 #include "../version.h"
30 namespace OpenSubdiv {
31 namespace OPENSUBDIV_VERSION {
44 HbrAllocator(
size_t *memorystat,
int blocksize,
void (*increment)(
size_t bytes),
void (*decrement)(
size_t bytes),
size_t elemsize =
sizeof(T));
64 const int m_blocksize;
85 : m_memorystat(memorystat), m_blocksize(blocksize), m_elemsize((int)elemsize), m_blocks(0), m_nblocks(0), m_blockCapacity(0), m_freecount(0), m_increment(increment), m_decrement(decrement) {
95 for (
int i = 0; i < m_nblocks; ++i) {
97 T* blockptr = m_blocks[i];
98 T* startblock = blockptr;
99 for (
int j = 0; j < m_blocksize; ++j) {
101 blockptr = (T*) ((
char*) blockptr + m_elemsize);
104 if (m_decrement) m_decrement(m_blocksize * m_elemsize);
105 *m_memorystat -= m_blocksize * m_elemsize;
115 template <
typename T>
121 T* block = (T*) malloc(m_blocksize * m_elemsize);
124 for (
int i = 0; i < m_blocksize; ++i) {
126 blockptr = (T*) ((
char*) blockptr + m_elemsize);
128 if (m_increment) m_increment(m_blocksize * m_elemsize);
129 *m_memorystat += m_blocksize * m_elemsize;
133 for (
int i = 0; i < m_blocksize - 1; ++i) {
134 T* next = (T*) ((
char*) blockptr + m_elemsize);
135 blockptr->GetNext() = next;
138 blockptr->GetNext() = 0;
142 if (m_nblocks + 1 >= m_blockCapacity) {
143 m_blockCapacity = m_blockCapacity * 2;
144 if (m_blockCapacity < 1) m_blockCapacity = 1;
145 m_blocks = (T**) realloc(m_blocks, m_blockCapacity *
sizeof(T*));
147 m_blocks[m_nblocks] = block;
149 m_freecount += m_blocksize;
152 m_freelist = obj->GetNext();
158 template <
typename T>
161 assert(!obj->GetNext());
162 obj->GetNext() = m_freelist;
168 using namespace OPENSUBDIV_VERSION;
void Clear()
Clear the allocator, deleting all allocated objects.
~HbrAllocator()
Destructor.
T * Allocate()
Create an allocated object.
void SetMemStatsIncrement(void(*increment)(size_t bytes))
void Deallocate(T *)
Return an allocated object to the block allocator.
void(* HbrMemStatFunction)(size_t bytes)
HbrAllocator(size_t *memorystat, int blocksize, void(*increment)(size_t bytes), void(*decrement)(size_t bytes), size_t elemsize=sizeof(T))
Constructor.
void SetMemStatsDecrement(void(*decrement)(size_t bytes))