RavEngine
Loading...
Searching...
No Matches
RecastAlloc.h
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 <stddef.h>
23#include <stdint.h>
24
25#include <RecastAssert.h>
26
29enum rcAllocHint
30{
31 RC_ALLOC_PERM,
32 RC_ALLOC_TEMP
33};
34
36// @param[in] size The size, in bytes of memory, to allocate.
37// @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
38// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
40typedef void* (rcAllocFunc)(size_t size, rcAllocHint hint);
41
45typedef void (rcFreeFunc)(void* ptr);
46
50void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
51
57void* rcAlloc(size_t size, rcAllocHint hint);
58
62void rcFree(void* ptr);
63
67struct rcNewTag {};
68inline void* operator new(size_t, const rcNewTag&, void* p) { return p; }
69inline void operator delete(void*, const rcNewTag&, void*) {}
70
73typedef intptr_t rcSizeType;
74#define RC_SIZE_MAX INTPTR_MAX
75
78#if defined(__GNUC__) || defined(__clang__)
79#define rcLikely(x) __builtin_expect((x), true)
80#define rcUnlikely(x) __builtin_expect((x), false)
81#else
82#define rcLikely(x) (x)
83#define rcUnlikely(x) (x)
84#endif
85
94template <typename T, rcAllocHint H>
96 rcSizeType m_size;
97 rcSizeType m_cap;
98 T* m_data;
99 // Constructs a T at the give address with either the copy constructor or the default.
100 static void construct(T* p, const T& v) { ::new(rcNewTag(), (void*)p) T(v); }
101 static void construct(T* p) { ::new(rcNewTag(), (void*)p) T; }
102 static void construct_range(T* begin, T* end);
103 static void construct_range(T* begin, T* end, const T& value);
104 static void copy_range(T* dst, const T* begin, const T* end);
105 void destroy_range(rcSizeType begin, rcSizeType end);
106 // Creates an array of the given size, copies all of this vector's data into it, and returns it.
107 T* allocate_and_copy(rcSizeType size);
108 void resize_impl(rcSizeType size, const T* value);
109 public:
110 typedef rcSizeType size_type;
111 typedef T value_type;
112
113 rcVectorBase() : m_size(0), m_cap(0), m_data(0) {};
114 rcVectorBase(const rcVectorBase<T, H>& other) : m_size(0), m_cap(0), m_data(0) { assign(other.begin(), other.end()); }
115 explicit rcVectorBase(rcSizeType count) : m_size(0), m_cap(0), m_data(0) { resize(count); }
116 rcVectorBase(rcSizeType count, const T& value) : m_size(0), m_cap(0), m_data(0) { resize(count, value); }
117 rcVectorBase(const T* begin, const T* end) : m_size(0), m_cap(0), m_data(0) { assign(begin, end); }
118 ~rcVectorBase() { destroy_range(0, m_size); rcFree(m_data); }
119
120 // Unlike in std::vector, we return a bool to indicate whether the alloc was successful.
121 bool reserve(rcSizeType size);
122
123 void assign(rcSizeType count, const T& value) { clear(); resize(count, value); }
124 void assign(const T* begin, const T* end);
125
126 void resize(rcSizeType size) { resize_impl(size, NULL); }
127 void resize(rcSizeType size, const T& value) { resize_impl(size, &value); }
128 // Not implemented as resize(0) because resize requires T to be default-constructible.
129 void clear() { destroy_range(0, m_size); m_size = 0; }
130
131 void push_back(const T& value);
132 void pop_back() { rcAssert(m_size > 0); back().~T(); m_size--; }
133
134 rcSizeType size() const { return m_size; }
135 rcSizeType capacity() const { return m_cap; }
136 bool empty() const { return size() == 0; }
137
138 const T& operator[](rcSizeType i) const { rcAssert(i >= 0 && i < m_size); return m_data[i]; }
139 T& operator[](rcSizeType i) { rcAssert(i >= 0 && i < m_size); return m_data[i]; }
140
141 const T& front() const { rcAssert(m_size); return m_data[0]; }
142 T& front() { rcAssert(m_size); return m_data[0]; }
143 const T& back() const { rcAssert(m_size); return m_data[m_size - 1]; };
144 T& back() { rcAssert(m_size); return m_data[m_size - 1]; };
145 const T* data() const { return m_data; }
146 T* data() { return m_data; }
147
148 T* begin() { return m_data; }
149 T* end() { return m_data + m_size; }
150 const T* begin() const { return m_data; }
151 const T* end() const { return m_data + m_size; }
152
153 void swap(rcVectorBase<T, H>& other);
154
155 // Explicitly deleted.
156 rcVectorBase& operator=(const rcVectorBase<T, H>& other);
157};
158
159template<typename T, rcAllocHint H>
160bool rcVectorBase<T, H>::reserve(rcSizeType count) {
161 if (count <= m_cap) {
162 return true;
163 }
164 T* new_data = allocate_and_copy(count);
165 if (!new_data) {
166 return false;
167 }
168 destroy_range(0, m_size);
169 rcFree(m_data);
170 m_data = new_data;
171 m_cap = count;
172 return true;
173}
174template <typename T, rcAllocHint H>
175T* rcVectorBase<T, H>::allocate_and_copy(rcSizeType size) {
176 rcAssert(RC_SIZE_MAX / static_cast<rcSizeType>(sizeof(T)) >= size);
177 T* new_data = static_cast<T*>(rcAlloc(sizeof(T) * size, H));
178 if (new_data) {
179 copy_range(new_data, m_data, m_data + m_size);
180 }
181 return new_data;
182}
183template <typename T, rcAllocHint H>
184void rcVectorBase<T, H>::assign(const T* begin, const T* end) {
185 clear();
186 reserve(end - begin);
187 m_size = end - begin;
188 copy_range(m_data, begin, end);
189}
190template <typename T, rcAllocHint H>
191void rcVectorBase<T, H>::push_back(const T& value) {
192 // rcLikely increases performance by ~50% on BM_rcVector_PushPreallocated,
193 // and by ~2-5% on BM_rcVector_Push.
194 if (rcLikely(m_size < m_cap)) {
195 construct(m_data + m_size++, value);
196 return;
197 }
198
199 rcAssert(RC_SIZE_MAX / 2 >= m_size);
200 rcSizeType new_cap = m_size ? 2*m_size : 1;
201 T* data = allocate_and_copy(new_cap);
202 // construct between allocate and destroy+free in case value is
203 // in this vector.
204 construct(data + m_size, value);
205 destroy_range(0, m_size);
206 m_size++;
207 m_cap = new_cap;
208 rcFree(m_data);
209 m_data = data;
210}
211template <typename T, rcAllocHint H>
212void rcVectorBase<T, H>::resize_impl(rcSizeType size, const T* value) {
213 if (size < m_size) {
214 destroy_range(size, m_size);
215 m_size = size;
216 } else if (size > m_size) {
217 T* new_data = allocate_and_copy(size);
218 // We defer deconstructing/freeing old data until after constructing
219 // new elements in case "value" is there.
220 if (value) {
221 construct_range(new_data + m_size, new_data + size, *value);
222 } else {
223 construct_range(new_data + m_size, new_data + size);
224 }
225 destroy_range(0, m_size);
226 rcFree(m_data);
227 m_data = new_data;
228 m_cap = size;
229 m_size = size;
230 }
231}
232template <typename T, rcAllocHint H>
234 // TODO: Reorganize headers so we can use rcSwap here.
235 rcSizeType tmp_cap = other.m_cap;
236 rcSizeType tmp_size = other.m_size;
237 T* tmp_data = other.m_data;
238
239 other.m_cap = m_cap;
240 other.m_size = m_size;
241 other.m_data = m_data;
242
243 m_cap = tmp_cap;
244 m_size = tmp_size;
245 m_data = tmp_data;
246}
247// static
248template <typename T, rcAllocHint H>
249void rcVectorBase<T, H>::construct_range(T* begin, T* end) {
250 for (T* p = begin; p < end; p++) {
251 construct(p);
252 }
253}
254// static
255template <typename T, rcAllocHint H>
256void rcVectorBase<T, H>::construct_range(T* begin, T* end, const T& value) {
257 for (T* p = begin; p < end; p++) {
258 construct(p, value);
259 }
260}
261// static
262template <typename T, rcAllocHint H>
263void rcVectorBase<T, H>::copy_range(T* dst, const T* begin, const T* end) {
264 for (rcSizeType i = 0 ; i < end - begin; i++) {
265 construct(dst + i, begin[i]);
266 }
267}
268template <typename T, rcAllocHint H>
269void rcVectorBase<T, H>::destroy_range(rcSizeType begin, rcSizeType end) {
270 for (rcSizeType i = begin; i < end; i++) {
271 m_data[i].~T();
272 }
273}
274
275template <typename T>
276class rcTempVector : public rcVectorBase<T, RC_ALLOC_TEMP> {
278public:
279 rcTempVector() : Base() {}
280 explicit rcTempVector(rcSizeType size) : Base(size) {}
281 rcTempVector(rcSizeType size, const T& value) : Base(size, value) {}
282 rcTempVector(const rcTempVector<T>& other) : Base(other) {}
283 rcTempVector(const T* begin, const T* end) : Base(begin, end) {}
284};
285template <typename T>
286class rcPermVector : public rcVectorBase<T, RC_ALLOC_PERM> {
288public:
289 rcPermVector() : Base() {}
290 explicit rcPermVector(rcSizeType size) : Base(size) {}
291 rcPermVector(rcSizeType size, const T& value) : Base(size, value) {}
292 rcPermVector(const rcPermVector<T>& other) : Base(other) {}
293 rcPermVector(const T* begin, const T* end) : Base(begin, end) {}
294};
295
296
299{
300 rcTempVector<int> m_impl;
301public:
302 rcIntArray() {}
303 rcIntArray(int n) : m_impl(n, 0) {}
304 void push(int item) { m_impl.push_back(item); }
305 void resize(int size) { m_impl.resize(size); }
306 int pop()
307 {
308 int v = m_impl.back();
309 m_impl.pop_back();
310 return v;
311 }
312 int size() const { return static_cast<int>(m_impl.size()); }
313 int& operator[](int index) { return m_impl[index]; }
314 int operator[](int index) const { return m_impl[index]; }
315};
316
319template<class T> class rcScopedDelete
320{
321 T* ptr;
322public:
323
325 inline rcScopedDelete() : ptr(0) {}
326
329 inline rcScopedDelete(T* p) : ptr(p) {}
330 inline ~rcScopedDelete() { rcFree(ptr); }
331
334 inline operator T*() { return ptr; }
335
336private:
337 // Explicitly disabled copy constructor and copy assignment operator.
339 rcScopedDelete& operator=(const rcScopedDelete&);
340};
341
342#endif
Legacy class. Prefer rcVector<int>.
Definition RecastAlloc.h:299
Definition RecastAlloc.h:286
Definition RecastAlloc.h:320
rcScopedDelete()
Constructs an instance with a null pointer.
Definition RecastAlloc.h:325
rcScopedDelete(T *p)
Definition RecastAlloc.h:329
Definition RecastAlloc.h:276
Definition RecastAlloc.h:95
Definition RecastAlloc.h:67