-
Notifications
You must be signed in to change notification settings - Fork 8
/
geodesic_memory.h
195 lines (155 loc) · 3.88 KB
/
geodesic_memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Copyright (C) 2008 Danil Kirsanov, MIT License
#ifndef _GEODESIC_MEMORY_20071231
#define _GEODESIC_MEMORY_20071231
//two fast and simple memory allocators
#include <vector>
#include <assert.h>
#include <math.h>
#include <memory>
namespace geodesic{
template<class T> //quickly allocates multiple elements of a given type; no deallocation
class SimlpeMemoryAllocator
{
public:
typedef T* pointer;
SimlpeMemoryAllocator(unsigned block_size = 0,
unsigned max_number_of_blocks = 0)
{
reset(block_size,
max_number_of_blocks);
};
~SimlpeMemoryAllocator(){};
void reset(unsigned block_size,
unsigned max_number_of_blocks)
{
m_block_size = block_size;
m_max_number_of_blocks = max_number_of_blocks;
m_current_position = 0;
m_storage.reserve(max_number_of_blocks);
m_storage.resize(1);
m_storage[0].resize(block_size);
};
pointer allocate(unsigned const n) //allocate n units
{
assert(n < m_block_size);
if(m_current_position + n >= m_block_size)
{
m_storage.push_back( std::vector<T>() );
m_storage.back().resize(m_block_size);
m_current_position = 0;
}
pointer result = & m_storage.back()[m_current_position];
m_current_position += n;
return result;
};
private:
std::vector<std::vector<T> > m_storage;
unsigned m_block_size; //size of a single block
unsigned m_max_number_of_blocks; //maximum allowed number of blocks
unsigned m_current_position; //first unused element inside the current block
};
template<class T> //quickly allocates and deallocates single elements of a given type
class MemoryAllocator
{
public:
typedef T* pointer;
MemoryAllocator(unsigned block_size = 1024,
unsigned max_number_of_blocks = 1024)
{
reset(block_size,
max_number_of_blocks);
};
~MemoryAllocator(){};
void clear()
{
reset(m_block_size,
m_max_number_of_blocks);
}
void reset(unsigned block_size,
unsigned max_number_of_blocks)
{
m_block_size = block_size;
m_max_number_of_blocks = max_number_of_blocks;
assert(m_block_size > 0);
assert(m_max_number_of_blocks > 0);
m_current_position = 0;
m_storage.reserve(max_number_of_blocks);
m_storage.resize(1);
m_storage[0].resize(block_size);
m_deleted.clear();
m_deleted.reserve(2*block_size);
};
pointer allocate() //allocates single unit of memory
{
pointer result;
if(m_deleted.empty())
{
if(m_current_position + 1 >= m_block_size)
{
m_storage.push_back( std::vector<T>() );
m_storage.back().resize(m_block_size);
m_current_position = 0;
}
result = & m_storage.back()[m_current_position];
++m_current_position;
}
else
{
result = m_deleted.back();
m_deleted.pop_back();
}
return result;
};
void deallocate(pointer p) //allocate n units
{
if(m_deleted.size() < m_deleted.capacity())
{
m_deleted.push_back(p);
}
};
private:
std::vector<std::vector<T> > m_storage;
unsigned m_block_size; //size of a single block
unsigned m_max_number_of_blocks; //maximum allowed number of blocks
unsigned m_current_position; //first unused element inside the current block
std::vector<pointer> m_deleted; //pointers to deleted elemets
};
class OutputBuffer
{
public:
OutputBuffer():
m_num_bytes(0)
{}
void clear()
{
m_num_bytes = 0;
m_buffer = std::unique_ptr<double>();
}
template<class T>
T* allocate(unsigned n)
{
double wanted = n*sizeof(T);
if(wanted > m_num_bytes)
{
unsigned new_size = (unsigned) ceil(wanted / (double)sizeof(double));
m_buffer = std::unique_ptr<double>(new double[new_size]);
m_num_bytes = new_size*sizeof(double);
}
return (T*)m_buffer.get();
}
template <class T>
T* get()
{
return (T*)m_buffer.get();
}
template<class T>
unsigned capacity()
{
return (unsigned)floor((double)m_num_bytes/(double)sizeof(T));
};
private:
std::unique_ptr<double> m_buffer;
unsigned m_num_bytes;
};
} //geodesic
#endif //_GEODESIC_MEMORY_20071231