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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#pragma once
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Utilities/StaticAssert.h"
#include <memory> // std::uninitialized_fill
// dynamic_array - simplified version of std::vector<T>
//
// features:
// . always uses memcpy for copying elements. Your data structures must be simple and can't have internal pointers / rely on copy constructor.
// . EASTL like push_back(void) implementation
// Existing std STL implementations implement insertion operations by copying from an element.
// For example, resize(size() + 1) creates a throw-away temporary object.
// There is no way in existing std STL implementations to add an element to a container without implicitly or
// explicitly providing one to copy from (aside from some existing POD optimizations).
// For expensive-to-construct objects this creates a potentially serious performance problem.
// . grows X2 on reallocation
// . small code footprint
// . clear actually deallocates memory
// . resize does NOT initialize members!
//
// Changelog:
// Added pop_back()
// Added assign()
// Added clear() - frees the data, use resize(0) to clear w/o freeing
// zero allocation for empty array
//
//
template<typename T>
struct AlignOfType
{
enum { align = ALIGN_OF(T) };
};
template <typename T, size_t align = AlignOfType<T>::align, MemLabelIdentifier defaultLabel = kMemDynamicArrayId>
struct dynamic_array
{
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef T value_type;
typedef size_t size_type;
typedef size_t difference_type;
typedef T& reference;
typedef const T& const_reference;
public:
dynamic_array() : m_data(NULL), m_label(defaultLabel, NULL), m_size(0), m_capacity(0)
{
m_label = MemLabelId(defaultLabel, GET_CURRENT_ALLOC_ROOT_HEADER());
}
dynamic_array(MemLabelRef label) : m_data(NULL), m_label(label), m_size(0), m_capacity(0)
{
}
explicit dynamic_array (size_t size, MemLabelRef label)
: m_label(label), m_size(size), m_capacity (size)
{
m_data = allocate (size);
}
dynamic_array (size_t size, T const& init_value, MemLabelRef label)
: m_label(label), m_size (size), m_capacity (size)
{
m_data = allocate (size);
std::uninitialized_fill (m_data, m_data + size, init_value);
}
~dynamic_array()
{
if (owns_data())
m_data = deallocate(m_data);
}
dynamic_array(const dynamic_array& other) : m_capacity(0), m_size(0), m_label(other.m_label)
{
//m_label.SetRootHeader(GET_CURRENT_ALLOC_ROOT_HEADER());
m_data = NULL;
assign(other.begin(), other.end());
}
dynamic_array& operator=(const dynamic_array& other)
{
// should not allocate memory unless we have to
assign(other.begin(), other.end());
return *this;
}
void clear()
{
if (owns_data())
m_data = deallocate(m_data);
m_size = 0;
m_capacity = 0;
}
void assign(const_iterator begin, const_iterator end)
{
Assert(begin<=end);
resize_uninitialized(end-begin);
memcpy(m_data, begin, m_size * sizeof(T));
}
void erase(iterator input_begin, iterator input_end)
{
Assert(input_begin <= input_end);
Assert(input_begin >= begin());
Assert(input_end <= end());
size_t leftOverSize = end() - input_end;
memmove(input_begin, input_end, leftOverSize * sizeof(T));
m_size -= input_end - input_begin;
}
iterator erase(iterator position)
{
Assert(position >= begin());
Assert(position < end());
size_t leftOverSize = end() - position - 1;
memmove(position, position+1, leftOverSize * sizeof(T));
m_size -= 1;
return position;
}
iterator insert(iterator insert_before, const_iterator input_begin, const_iterator input_end)
{
Assert(input_begin <= input_end);
Assert(insert_before >= begin());
Assert(insert_before <= end());
// resize (make sure that insertBefore does not get invalid in the meantime because of a reallocation)
size_t insert_before_index = insert_before - begin();
size_t elements_to_be_moved = size() - insert_before_index;
resize_uninitialized((input_end - input_begin) + size());
insert_before = begin() + insert_before_index;
size_t insertsize = input_end - input_begin;
// move to the end of where the inserted data will be
memmove(insert_before + insertsize, insert_before, elements_to_be_moved * sizeof(T));
// inject input data in the hole we just created
memcpy(insert_before, input_begin, insertsize * sizeof(T));
return insert_before;
}
iterator insert(iterator insertBefore, const T& t) { return insert(insertBefore, &t, &t + 1); }
void swap(dynamic_array& other) throw()
{
if (m_data) UNITY_TRANSFER_OWNERSHIP_TO_HEADER(m_data, m_label, other.m_label.GetRootHeader());
if (other.m_data) UNITY_TRANSFER_OWNERSHIP_TO_HEADER(other.m_data, other.m_label, m_label.GetRootHeader());
std::swap(m_data, other.m_data);
std::swap(m_size, other.m_size);
std::swap(m_capacity, other.m_capacity);
std::swap(m_label, other.m_label);
}
T& push_back()
{
if (++m_size > capacity())
reserve(std::max<size_t>(capacity()*2, 1));
return back();
}
void push_back(const T& t)
{
push_back() = t;
}
void pop_back()
{
Assert(m_size >= 1);
m_size--;
}
void resize_uninitialized(size_t size, bool double_on_resize = false)
{
m_size = size;
if (m_size <= capacity())
return;
if(double_on_resize && size < capacity()*2)
size = capacity()*2;
reserve(size);
}
void resize_initialized(size_t size, const T& t = T(), bool double_on_resize = false)
{
if (size > capacity())
{
size_t requested_size = size;
if(double_on_resize && size < capacity()*2)
requested_size = capacity()*2;
reserve(requested_size);
}
if (size > m_size)
std::uninitialized_fill (m_data + m_size, m_data + size, t);
m_size = size;
}
void reserve(size_t inCapacity)
{
if (capacity() >= inCapacity)
return;
if (owns_data())
{
m_capacity = inCapacity;
m_data = reallocate(m_data, inCapacity);
}
else
{
T* newData = allocate(inCapacity);
memcpy(newData, m_data, m_size * sizeof(T));
// Invalidate old non-owned data, since using the data from two places is most likely a really really bad idea.
#if DEBUGMODE
memset(m_data, 0xCD, capacity() * sizeof(T));
#endif
m_capacity = inCapacity; // and clear reference bit
m_data = newData;
}
}
void assign_external (T* begin, T* end)
{
if (owns_data())
m_data = deallocate(m_data);
m_size = m_capacity = reinterpret_cast<value_type*> (end) - reinterpret_cast<value_type*> (begin);
Assert(m_size < k_reference_bit);
m_capacity |= k_reference_bit;
m_data = begin;
}
void set_owns_data (bool ownsData)
{
if (ownsData)
m_capacity &= ~k_reference_bit;
else
m_capacity |= k_reference_bit;
}
void shrink_to_fit()
{
if (owns_data())
{
m_capacity = m_size;
m_data = reallocate(m_data, m_size);
}
}
const T& back() const { Assert (m_size != 0); return m_data[m_size - 1]; }
const T& front() const { Assert (m_size != 0); return m_data[0]; }
T& back() { Assert (m_size != 0); return m_data[m_size - 1]; }
T& front() { Assert (m_size != 0); return m_data[0]; }
T* data () { return m_data; }
T const* data () const { return m_data; }
bool empty () const { return m_size == 0; }
size_t size() const { return m_size; }
size_t capacity() const { return m_capacity & ~k_reference_bit; }
T const& operator[] (size_t index) const { DebugAssert(index < m_size); return m_data[index]; }
T& operator[] (size_t index) { DebugAssert(index < m_size); return m_data[index]; }
T const* begin() const { return m_data; }
T* begin() { return m_data; }
T const* end() const { return m_data + m_size; }
T* end() { return m_data + m_size; }
bool owns_data() { return (m_capacity & k_reference_bit) == 0; }
bool equals(const dynamic_array& other)
{
if(m_size != other.m_size)
return false;
for( int i = 0; i < m_size; i++)
{
if (m_data[i] != other.m_data[i])
return false;
}
return true;
}
void set_memory_label (MemLabelRef label)
{
Assert(m_data == NULL);
m_label = label;
}
private:
static const size_t k_reference_bit = (size_t)1 << (sizeof (size_t) * 8 - 1);
T* allocate (size_t size)
{
// If you are getting this error then you are trying to allocate memory for an incomplete type
CompileTimeAssert(sizeof(T) != 0, "incomplete type");
CompileTimeAssert(align != 0, "incomplete type");
return static_cast<T*> (UNITY_MALLOC_ALIGNED (m_label, size * sizeof(T), align));
}
T* deallocate (T* data)
{
Assert(owns_data());
UNITY_FREE (m_label, data);
return NULL;
}
T* reallocate (T* data, size_t size)
{
// If you are getting this error then you are trying to allocate memory for an incomplete type
CompileTimeAssert(sizeof(T) != 0, "incomplete type");
CompileTimeAssert(align != 0, "incomplete type");
Assert(owns_data());
int alignof = static_cast<int>(align);
return static_cast<T*> (UNITY_REALLOC_ALIGNED(m_label, data, size * sizeof(T), alignof));
}
T* m_data;
MemLabelId m_label;
size_t m_size;
size_t m_capacity;
};
|