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
|
/*
* Copyright 2011-2013 Blender Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __UTIL_VECTOR_H__
#define __UTIL_VECTOR_H__
/* Vector */
#include <cassert>
#include <cstring>
#include <vector>
#include "util/util_aligned_malloc.h"
#include "util/util_guarded_allocator.h"
#include "util/util_types.h"
CCL_NAMESPACE_BEGIN
/* Vector
*
* Own subclass-ed vestion of std::vector. Subclass is needed because:
*
* - Use own allocator which keeps track of used/peak memory.
* - Have method to ensure capacity is re-set to 0.
*/
template<typename value_type,
typename allocator_type = GuardedAllocator<value_type> >
class vector : public std::vector<value_type, allocator_type>
{
public:
typedef std::vector<value_type, allocator_type> BaseClass;
/* Inherit all constructors from base class. */
using BaseClass::vector;
void free_memory(void)
{
BaseClass::resize(0);
BaseClass::shrink_to_fit();
}
/* Some external API might demand working with std::vector. */
operator std::vector<value_type>()
{
return std::vector<value_type>(this->begin(), this->end());
}
};
/* Array
*
* Simplified version of vector, serving multiple purposes:
* - somewhat faster in that it does not clear memory on resize/alloc,
* this was actually showing up in profiles quite significantly. it
* also does not run any constructors/destructors
* - if this is used, we are not tempted to use inefficient operations
* - aligned allocation for CPU native data types */
template<typename T, size_t alignment = MIN_ALIGNMENT_CPU_DATA_TYPES>
class array
{
public:
array()
: data_(NULL),
datasize_(0),
capacity_(0)
{}
explicit array(size_t newsize)
{
if(newsize == 0) {
data_ = NULL;
datasize_ = 0;
capacity_ = 0;
}
else {
data_ = mem_allocate(newsize);
datasize_ = newsize;
capacity_ = datasize_;
}
}
array(const array& from)
{
if(from.datasize_ == 0) {
data_ = NULL;
datasize_ = 0;
capacity_ = 0;
}
else {
data_ = mem_allocate(from.datasize_);
memcpy(data_, from.data_, from.datasize_*sizeof(T));
datasize_ = from.datasize_;
capacity_ = datasize_;
}
}
array& operator=(const array& from)
{
if(this != &from) {
resize(from.size());
memcpy((void*)data_, from.data_, datasize_*sizeof(T));
}
return *this;
}
array& operator=(const vector<T>& from)
{
resize(from.size());
if(from.size() > 0) {
memcpy(data_, &from[0], datasize_*sizeof(T));
}
return *this;
}
~array()
{
mem_free(data_, capacity_);
}
bool operator==(const array<T>& other) const
{
if(datasize_ != other.datasize_) {
return false;
}
return memcmp(data_, other.data_, datasize_*sizeof(T)) == 0;
}
bool operator!=(const array<T>& other) const
{
return !(*this == other);
}
void steal_data(array& from)
{
if(this != &from) {
clear();
data_ = from.data_;
datasize_ = from.datasize_;
capacity_ = from.capacity_;
from.data_ = NULL;
from.datasize_ = 0;
from.capacity_ = 0;
}
}
T *steal_pointer()
{
T *ptr = data_;
data_ = NULL;
clear();
return ptr;
}
T* resize(size_t newsize)
{
if(newsize == 0) {
clear();
}
else if(newsize != datasize_) {
if(newsize > capacity_) {
T *newdata = mem_allocate(newsize);
if(newdata == NULL) {
/* Allocation failed, likely out of memory. */
clear();
return NULL;
}
else if(data_ != NULL) {
memcpy((void *)newdata,
data_,
((datasize_ < newsize)? datasize_: newsize)*sizeof(T));
mem_free(data_, capacity_);
}
data_ = newdata;
capacity_ = newsize;
}
datasize_ = newsize;
}
return data_;
}
T* resize(size_t newsize, const T& value)
{
size_t oldsize = size();
resize(newsize);
for(size_t i = oldsize; i < size(); i++) {
data_[i] = value;
}
return data_;
}
void clear()
{
if(data_ != NULL) {
mem_free(data_, capacity_);
data_ = NULL;
}
datasize_ = 0;
capacity_ = 0;
}
size_t empty() const
{
return datasize_ == 0;
}
size_t size() const
{
return datasize_;
}
T* data()
{
return data_;
}
const T* data() const
{
return data_;
}
T& operator[](size_t i) const
{
assert(i < datasize_);
return data_[i];
}
void reserve(size_t newcapacity)
{
if(newcapacity > capacity_) {
T *newdata = mem_allocate(newcapacity);
if(data_ != NULL) {
memcpy(newdata, data_, ((datasize_ < newcapacity)? datasize_: newcapacity)*sizeof(T));
mem_free(data_, capacity_);
}
data_ = newdata;
capacity_ = newcapacity;
}
}
size_t capacity() const
{
return capacity_;
}
// do not use this method unless you are sure the code is not performance critical
void push_back_slow(const T& t)
{
if(capacity_ == datasize_)
{
reserve(datasize_ == 0 ? 1 : (size_t)((datasize_ + 1) * 1.2));
}
data_[datasize_++] = t;
}
void push_back_reserved(const T& t)
{
assert(datasize_ < capacity_);
push_back_slow(t);
}
void append(const array<T>& from)
{
if(from.size()) {
size_t old_size = size();
resize(old_size + from.size());
memcpy(data_ + old_size, from.data(), sizeof(T) * from.size());
}
}
protected:
inline T* mem_allocate(size_t N)
{
if(N == 0) {
return NULL;
}
T *mem = (T*)util_aligned_malloc(sizeof(T)*N, alignment);
if(mem != NULL) {
util_guarded_mem_alloc(sizeof(T)*N);
}
else {
throw std::bad_alloc();
}
return mem;
}
inline void mem_free(T *mem, size_t N)
{
if(mem != NULL) {
util_guarded_mem_free(sizeof(T)*N);
util_aligned_free(mem);
}
}
T *data_;
size_t datasize_;
size_t capacity_;
};
CCL_NAMESPACE_END
#endif /* __UTIL_VECTOR_H__ */
|