2 #ifndef _GEODESIC_MEMORY_20071231
3 #define _GEODESIC_MEMORY_20071231
20 unsigned max_number_of_blocks = 0)
23 max_number_of_blocks);
29 unsigned max_number_of_blocks)
31 m_block_size = block_size;
32 m_max_number_of_blocks = max_number_of_blocks;
35 m_current_position = 0;
37 m_storage.reserve(max_number_of_blocks);
39 m_storage[0].resize(block_size);
44 assert(n < m_block_size);
46 if(m_current_position + n >= m_block_size)
48 m_storage.push_back( std::vector<T>() );
49 m_storage.back().resize(m_block_size);
50 m_current_position = 0;
52 pointer result = & m_storage.back()[m_current_position];
53 m_current_position += n;
58 std::vector<std::vector<T> > m_storage;
59 unsigned m_block_size;
60 unsigned m_max_number_of_blocks;
61 unsigned m_current_position;
72 unsigned max_number_of_blocks = 1024)
75 max_number_of_blocks);
83 m_max_number_of_blocks);
87 unsigned max_number_of_blocks)
89 m_block_size = block_size;
90 m_max_number_of_blocks = max_number_of_blocks;
92 assert(m_block_size > 0);
93 assert(m_max_number_of_blocks > 0);
95 m_current_position = 0;
97 m_storage.reserve(max_number_of_blocks);
99 m_storage[0].resize(block_size);
102 m_deleted.reserve(2*block_size);
108 if(m_deleted.empty())
110 if(m_current_position + 1 >= m_block_size)
112 m_storage.push_back( std::vector<T>() );
113 m_storage.back().resize(m_block_size);
114 m_current_position = 0;
116 result = & m_storage.back()[m_current_position];
117 ++m_current_position;
121 result = m_deleted.back();
122 m_deleted.pop_back();
130 if(m_deleted.size() < m_deleted.capacity())
132 m_deleted.push_back(p);
137 std::vector<std::vector<T> > m_storage;
138 unsigned m_block_size;
139 unsigned m_max_number_of_blocks;
140 unsigned m_current_position;
142 std::vector<pointer> m_deleted;
156 m_buffer = std::unique_ptr<double>();
162 double wanted = n*
sizeof(T);
163 if(wanted > m_num_bytes)
165 unsigned new_size = (unsigned) ceil(wanted / (
double)
sizeof(double));
166 m_buffer = std::unique_ptr<double>(
new double[new_size]);
167 m_num_bytes = new_size*
sizeof(double);
170 return (T*)m_buffer.get();
176 return (T*)m_buffer.get();
182 return (
unsigned)floor((
double)m_num_bytes/(
double)
sizeof(T));
187 std::unique_ptr<double> m_buffer;
188 unsigned m_num_bytes;
void deallocate(pointer p)
void reset(unsigned block_size, unsigned max_number_of_blocks)
MemoryAllocator(unsigned block_size=1024, unsigned max_number_of_blocks=1024)
pointer allocate(unsigned const n)
void reset(unsigned block_size, unsigned max_number_of_blocks)
SimlpeMemoryAllocator(unsigned block_size=0, unsigned max_number_of_blocks=0)