Recast Navigation
Navigation-mesh Toolset for Games
Loading...
Searching...
No Matches
RecastAlloc.h
Go to the documentation of this file.
1//
2// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
3//
4// This software is provided 'as-is', without any express or implied
5// warranty. In no event will the authors be held liable for any damages
6// arising from the use of this software.
7// Permission is granted to anyone to use this software for any purpose,
8// including commercial applications, and to alter it and redistribute it
9// freely, subject to the following restrictions:
10// 1. The origin of this software must not be misrepresented; you must not
11// claim that you wrote the original software. If you use this software
12// in a product, an acknowledgment in the product documentation would be
13// appreciated but is not required.
14// 2. Altered source versions must be plainly marked as such, and must not be
15// misrepresented as being the original software.
16// 3. This notice may not be removed or altered from any source distribution.
17//
18
19#ifndef RECASTALLOC_H
20#define RECASTALLOC_H
21
22#include "RecastAssert.h"
23
24#include <stdlib.h>
25#include <stdint.h>
26
34
40typedef void* (rcAllocFunc)(size_t size, rcAllocHint hint);
41
45typedef void (rcFreeFunc)(void* ptr);
46
51void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
52
58void* rcAlloc(size_t size, rcAllocHint hint);
59
65void rcFree(void* ptr);
66
70struct rcNewTag {};
71inline void* operator new(size_t, const rcNewTag&, void* p) { return p; }
72inline void operator delete(void*, const rcNewTag&, void*) {}
73
76typedef intptr_t rcSizeType;
77#define RC_SIZE_MAX INTPTR_MAX
78
81#if defined(__GNUC__) || defined(__clang__)
82#define rcLikely(x) __builtin_expect((x), true)
83#define rcUnlikely(x) __builtin_expect((x), false)
84#else
85#define rcLikely(x) (x)
86#define rcUnlikely(x) (x)
87#endif
88
97template <typename T, rcAllocHint H>
99{
100 rcSizeType m_size;
101 rcSizeType m_cap;
102 T* m_data;
103 // Constructs a T at the give address with either the copy constructor or the default.
104 static void construct(T* p, const T& v) { ::new(rcNewTag(), (void*)p) T(v); }
105 static void construct(T* p) { ::new(rcNewTag(), (void*)p) T; }
106 static void construct_range(T* begin, T* end);
107 static void construct_range(T* begin, T* end, const T& value);
108 static void copy_range(T* dst, const T* begin, const T* end);
109 void destroy_range(rcSizeType begin, rcSizeType end);
110 // Creates an array of the given size, copies all of this vector's data into it, and returns it.
111 T* allocate_and_copy(rcSizeType size);
112 void resize_impl(rcSizeType size, const T* value);
113 // Requires: min_capacity > m_cap.
114 rcSizeType get_new_capacity(rcSizeType min_capacity);
115 public:
117 typedef T value_type;
118
119 rcVectorBase() : m_size(0), m_cap(0), m_data(0) {}
120 rcVectorBase(const rcVectorBase<T, H>& other) : m_size(0), m_cap(0), m_data(0) { assign(other.begin(), other.end()); }
121 explicit rcVectorBase(rcSizeType count) : m_size(0), m_cap(0), m_data(0) { resize(count); }
122 rcVectorBase(rcSizeType count, const T& value) : m_size(0), m_cap(0), m_data(0) { resize(count, value); }
123 rcVectorBase(const T* begin, const T* end) : m_size(0), m_cap(0), m_data(0) { assign(begin, end); }
124 ~rcVectorBase() { destroy_range(0, m_size); rcFree(m_data); }
125
126 // Unlike in std::vector, we return a bool to indicate whether the alloc was successful.
128
129 void assign(rcSizeType count, const T& value) { clear(); resize(count, value); }
130 void assign(const T* begin, const T* end);
131
132 void resize(rcSizeType size) { resize_impl(size, NULL); }
133 void resize(rcSizeType size, const T& value) { resize_impl(size, &value); }
134 // Not implemented as resize(0) because resize requires T to be default-constructible.
135 void clear() { destroy_range(0, m_size); m_size = 0; }
136
137 void push_back(const T& value);
138 void pop_back() { rcAssert(m_size > 0); back().~T(); m_size--; }
139
140 rcSizeType size() const { return m_size; }
141 rcSizeType capacity() const { return m_cap; }
142 bool empty() const { return size() == 0; }
143
144 const T& operator[](rcSizeType i) const { rcAssert(i >= 0 && i < m_size); return m_data[i]; }
145 T& operator[](rcSizeType i) { rcAssert(i >= 0 && i < m_size); return m_data[i]; }
146
147 const T& front() const { rcAssert(m_size); return m_data[0]; }
148 T& front() { rcAssert(m_size); return m_data[0]; }
149 const T& back() const { rcAssert(m_size); return m_data[m_size - 1]; }
150 T& back() { rcAssert(m_size); return m_data[m_size - 1]; }
151 const T* data() const { return m_data; }
152 T* data() { return m_data; }
153
154 T* begin() { return m_data; }
155 T* end() { return m_data + m_size; }
156 const T* begin() const { return m_data; }
157 const T* end() const { return m_data + m_size; }
158
160
161 // Explicitly deleted.
163};
164
165template<typename T, rcAllocHint H>
167{
168 if (count <= m_cap)
169 {
170 return true;
171 }
172
173 T* new_data = allocate_and_copy(count);
174 if (!new_data)
175 {
176 return false;
177 }
178
179 destroy_range(0, m_size);
180 rcFree(m_data);
181 m_data = new_data;
182 m_cap = count;
183 return true;
184}
185
186template <typename T, rcAllocHint H>
188{
189 rcAssert(RC_SIZE_MAX / static_cast<rcSizeType>(sizeof(T)) >= size);
190 T* new_data = static_cast<T*>(rcAlloc(sizeof(T) * size, H));
191 if (new_data)
192 {
193 copy_range(new_data, m_data, m_data + m_size);
194 }
195 return new_data;
196}
197
198template <typename T, rcAllocHint H>
199void rcVectorBase<T, H>::assign(const T* begin, const T* end)
200{
201 clear();
202 reserve(end - begin);
203 m_size = end - begin;
204 copy_range(m_data, begin, end);
205}
206
207template <typename T, rcAllocHint H>
209{
210 // rcLikely increases performance by ~50% on BM_rcVector_PushPreallocated,
211 // and by ~2-5% on BM_rcVector_Push.
212 if (rcLikely(m_size < m_cap))
213 {
214 construct(m_data + m_size++, value);
215 return;
216 }
217
218 const rcSizeType new_cap = get_new_capacity(m_cap + 1);
219 T* data = allocate_and_copy(new_cap);
220 // construct between allocate and destroy+free in case value is
221 // in this vector.
222 construct(data + m_size, value);
223 destroy_range(0, m_size);
224 m_size++;
225 m_cap = new_cap;
226 rcFree(m_data);
227 m_data = data;
228}
229
230template <typename T, rcAllocHint H>
232{
233 rcAssert(min_capacity <= RC_SIZE_MAX);
234 if (rcUnlikely(m_cap >= RC_SIZE_MAX / 2))
235 {
236 return RC_SIZE_MAX;
237 }
238 return 2 * m_cap > min_capacity ? 2 * m_cap : min_capacity;
239}
240
241template <typename T, rcAllocHint H>
242void rcVectorBase<T, H>::resize_impl(rcSizeType size, const T* value)
243{
244 if (size < m_size)
245 {
246 destroy_range(size, m_size);
247 m_size = size;
248 }
249 else if (size > m_size)
250 {
251 if (size <= m_cap)
252 {
253 if (value)
254 {
255 construct_range(m_data + m_size, m_data + size, *value);
256 }
257 else
258 {
259 construct_range(m_data + m_size, m_data + size);
260 }
261 m_size = size;
262 }
263 else
264 {
265 const rcSizeType new_cap = get_new_capacity(size);
266 T* new_data = allocate_and_copy(new_cap);
267 // We defer deconstructing/freeing old data until after constructing
268 // new elements in case "value" is there.
269 if (value)
270 {
271 construct_range(new_data + m_size, new_data + size, *value);
272 }
273 else
274 {
275 construct_range(new_data + m_size, new_data + size);
276 }
277 destroy_range(0, m_size);
278 rcFree(m_data);
279 m_data = new_data;
280 m_cap = new_cap;
281 m_size = size;
282 }
283 }
284}
285
286template <typename T, rcAllocHint H>
288{
289 // TODO: Reorganize headers so we can use rcSwap here.
290 rcSizeType tmp_cap = other.m_cap;
291 rcSizeType tmp_size = other.m_size;
292 T* tmp_data = other.m_data;
293
294 other.m_cap = m_cap;
295 other.m_size = m_size;
296 other.m_data = m_data;
297
298 m_cap = tmp_cap;
299 m_size = tmp_size;
300 m_data = tmp_data;
301}
302
303// static
304template <typename T, rcAllocHint H>
305void rcVectorBase<T, H>::construct_range(T* begin, T* end)
306{
307 for (T* p = begin; p < end; p++)
308 {
309 construct(p);
310 }
311}
312
313// static
314template <typename T, rcAllocHint H>
315void rcVectorBase<T, H>::construct_range(T* begin, T* end, const T& value)
316{
317 for (T* p = begin; p < end; p++)
318 {
319 construct(p, value);
320 }
321}
322
323// static
324template <typename T, rcAllocHint H>
325void rcVectorBase<T, H>::copy_range(T* dst, const T* begin, const T* end)
326{
327 for (rcSizeType i = 0 ; i < end - begin; i++)
328 {
329 construct(dst + i, begin[i]);
330 }
331}
332
333template <typename T, rcAllocHint H>
335{
336 for (rcSizeType i = begin; i < end; i++)
337 {
338 m_data[i].~T();
339 }
340}
341
342template <typename T>
343class rcTempVector : public rcVectorBase<T, RC_ALLOC_TEMP>
344{
346public:
349 rcTempVector(rcSizeType size, const T& value) : Base(size, value) {}
350 rcTempVector(const rcTempVector<T>& other) : Base(other) {}
351 rcTempVector(const T* begin, const T* end) : Base(begin, end) {}
352};
353
354template <typename T>
355class rcPermVector : public rcVectorBase<T, RC_ALLOC_PERM>
356{
358public:
361 rcPermVector(rcSizeType size, const T& value) : Base(size, value) {}
362 rcPermVector(const rcPermVector<T>& other) : Base(other) {}
363 rcPermVector(const T* begin, const T* end) : Base(begin, end) {}
364};
365
368{
369 rcTempVector<int> m_impl;
370public:
372 rcIntArray(int n) : m_impl(n, 0) {}
373 void push(int item) { m_impl.push_back(item); }
374 void resize(int size) { m_impl.resize(size); }
375 void clear() { m_impl.clear(); }
376 int pop()
377 {
378 int v = m_impl.back();
379 m_impl.pop_back();
380 return v;
381 }
382 int size() const { return static_cast<int>(m_impl.size()); }
383 int& operator[](int index) { return m_impl[index]; }
384 int operator[](int index) const { return m_impl[index]; }
385};
386
389template<class T> class rcScopedDelete
390{
391 T* ptr;
392public:
393
395 inline rcScopedDelete() : ptr(0) {}
396
399 inline rcScopedDelete(T* p) : ptr(p) {}
400 inline ~rcScopedDelete() { rcFree(ptr); }
401
404 inline operator T*() { return ptr; }
405
406private:
407 // Explicitly disabled copy constructor and copy assignment operator.
409 rcScopedDelete& operator=(const rcScopedDelete&);
410};
411
412#endif
rcAllocHint
Provides hint values to the memory allocator on how long the memory is expected to be used.
Definition RecastAlloc.h:30
@ RC_ALLOC_TEMP
Memory used temporarily within a function.
Definition RecastAlloc.h:32
@ RC_ALLOC_PERM
Memory will persist after a function call.
Definition RecastAlloc.h:31
void * rcAlloc(size_t size, rcAllocHint hint)
Allocates a memory block.
Definition RecastAlloc.cpp:40
intptr_t rcSizeType
Signed to avoid warnings when comparing to int loop indexes, and common error with comparing to zero.
Definition RecastAlloc.h:76
void() rcFreeFunc(void *ptr)
A memory deallocation function.
Definition RecastAlloc.h:45
void rcFree(void *ptr)
Deallocates a memory block.
Definition RecastAlloc.cpp:45
#define RC_SIZE_MAX
Definition RecastAlloc.h:77
#define rcUnlikely(x)
Definition RecastAlloc.h:86
void *() rcAllocFunc(size_t size, rcAllocHint hint)
A memory allocation function.
Definition RecastAlloc.h:40
#define rcLikely(x)
Macros to hint to the compiler about the likeliest branch.
Definition RecastAlloc.h:85
void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc)
Sets the base custom allocation functions to be used by Recast.
Definition RecastAlloc.cpp:34
#define rcAssert(expression)
Definition RecastAssert.h:41
Legacy class. Prefer rcVector<int>.
Definition RecastAlloc.h:368
void clear()
Definition RecastAlloc.h:375
int pop()
Definition RecastAlloc.h:376
void resize(int size)
Definition RecastAlloc.h:374
rcIntArray()
Definition RecastAlloc.h:371
int & operator[](int index)
Definition RecastAlloc.h:383
int operator[](int index) const
Definition RecastAlloc.h:384
rcIntArray(int n)
Definition RecastAlloc.h:372
void push(int item)
Definition RecastAlloc.h:373
int size() const
Definition RecastAlloc.h:382
Definition RecastAlloc.h:356
rcPermVector()
Definition RecastAlloc.h:359
rcPermVector(const rcPermVector< T > &other)
Definition RecastAlloc.h:362
rcPermVector(rcSizeType size, const T &value)
Definition RecastAlloc.h:361
rcPermVector(const T *begin, const T *end)
Definition RecastAlloc.h:363
rcPermVector(rcSizeType size)
Definition RecastAlloc.h:360
A simple helper class used to delete an array when it goes out of scope.
Definition RecastAlloc.h:390
rcScopedDelete()
Constructs an instance with a null pointer.
Definition RecastAlloc.h:395
rcScopedDelete(T *p)
Constructs an instance with the specified pointer.
Definition RecastAlloc.h:399
~rcScopedDelete()
Definition RecastAlloc.h:400
Definition RecastAlloc.h:344
rcTempVector(rcSizeType size)
Definition RecastAlloc.h:348
rcTempVector(const rcTempVector< T > &other)
Definition RecastAlloc.h:350
rcTempVector(const T *begin, const T *end)
Definition RecastAlloc.h:351
rcTempVector(rcSizeType size, const T &value)
Definition RecastAlloc.h:349
rcTempVector()
Definition RecastAlloc.h:347
Variable-sized storage type.
Definition RecastAlloc.h:99
T * begin()
Definition RecastAlloc.h:154
bool reserve(rcSizeType size)
Definition RecastAlloc.h:166
T * end()
Definition RecastAlloc.h:155
const T * data() const
Definition RecastAlloc.h:151
void resize(rcSizeType size, const T &value)
Definition RecastAlloc.h:133
T * data()
Definition RecastAlloc.h:152
rcSizeType capacity() const
Definition RecastAlloc.h:141
T & back()
Definition RecastAlloc.h:150
void assign(const T *begin, const T *end)
Definition RecastAlloc.h:199
const T & operator[](rcSizeType i) const
Definition RecastAlloc.h:144
rcSizeType size_type
Definition RecastAlloc.h:116
rcVectorBase(rcSizeType count)
Definition RecastAlloc.h:121
const T * begin() const
Definition RecastAlloc.h:156
const T * end() const
Definition RecastAlloc.h:157
void pop_back()
Definition RecastAlloc.h:138
void resize(rcSizeType size)
Definition RecastAlloc.h:132
rcVectorBase()
Definition RecastAlloc.h:119
T & operator[](rcSizeType i)
Definition RecastAlloc.h:145
rcVectorBase(const rcVectorBase< T, H > &other)
Definition RecastAlloc.h:120
const T & back() const
Definition RecastAlloc.h:149
~rcVectorBase()
Definition RecastAlloc.h:124
T value_type
Definition RecastAlloc.h:117
void push_back(const T &value)
Definition RecastAlloc.h:208
rcVectorBase(const T *begin, const T *end)
Definition RecastAlloc.h:123
rcVectorBase & operator=(const rcVectorBase< T, H > &other)
rcVectorBase(rcSizeType count, const T &value)
Definition RecastAlloc.h:122
bool empty() const
Definition RecastAlloc.h:142
void clear()
Definition RecastAlloc.h:135
rcSizeType size() const
Definition RecastAlloc.h:140
T & front()
Definition RecastAlloc.h:148
void swap(rcVectorBase< T, H > &other)
Definition RecastAlloc.h:287
const T & front() const
Definition RecastAlloc.h:147
void assign(rcSizeType count, const T &value)
Definition RecastAlloc.h:129
An implementation of operator new usable for placement new.
Definition RecastAlloc.h:70