| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
enum rcAllocHint |
| 30 |
{ |
| 31 |
RC_ALLOC_PERM, |
| 32 |
RC_ALLOC_TEMP |
| 33 |
}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
typedef void* (rcAllocFunc)(size_t size, rcAllocHint hint); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
typedef void (rcFreeFunc)(void* ptr); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
void* rcAlloc(size_t size, rcAllocHint hint); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
void rcFree(void* ptr); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct rcNewTag {}; |
| 77 |
inline void* operator new(size_t, const rcNewTag&, void* p) { return p; } |
| 78 |
inline void operator delete(void*, const rcNewTag&, void*) {} |
| 79 |
|
| •80 |
|
| 81 |
|
| 82 |
typedef intptr_t rcSizeType; |
| 83 |
#define RC_SIZE_MAX INTPTR_MAX |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#if defined(__GNUC__) || defined(__clang__) |
| 88 |
#define rcLikely(x) __builtin_expect((x), true) |
| 89 |
#define rcUnlikely(x) __builtin_expect((x), false) |
| 90 |
#else |
| 91 |
#define rcLikely(x) (x) |
| 92 |
#define rcUnlikely(x) (x) |
| 93 |
#endif |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
template <typename T, rcAllocHint H> |
| 104 |
class rcVectorBase { |
| 105 |
rcSizeType m_size; |
| 106 |
rcSizeType m_cap; |
| 107 |
T* m_data; |
| 108 |
|
| 109 |
static void construct(T* p, const T& v) { ::new(rcNewTag(), (void*)p) T(v); } |
| 110 |
static void construct(T* p) { ::new(rcNewTag(), (void*)p) T; } |
| 111 |
static void construct_range(T* begin, T* end); |
| 112 |
static void construct_range(T* begin, T* end, const T& value); |
| 113 |
static void copy_range(T* dst, const T* begin, const T* end); |
| 114 |
void destroy_range(rcSizeType begin, rcSizeType end); |
| 115 |
|
| 116 |
T* allocate_and_copy(rcSizeType size); |
| 117 |
void resize_impl(rcSizeType size, const T* value); |
| 118 |
|
| 119 |
rcSizeType get_new_capacity(rcSizeType min_capacity); |
| 120 |
public: |
| 121 |
typedef rcSizeType size_type; |
| 122 |
typedef T value_type; |
| 123 |
|
| 124 |
rcVectorBase() : m_size(0), m_cap(0), m_data(0) {} |
| 125 |
rcVectorBase(const rcVectorBase<T, H>& other) : m_size(0), m_cap(0), m_data(0) { assign(other.begin(), other.end()); } |
| 126 |
explicit rcVectorBase(rcSizeType count) : m_size(0), m_cap(0), m_data(0) { resize(count); } |
| 127 |
rcVectorBase(rcSizeType count, const T& value) : m_size(0), m_cap(0), m_data(0) { resize(count, value); } |
| 128 |
rcVectorBase(const T* begin, const T* end) : m_size(0), m_cap(0), m_data(0) { assign(begin, end); } |
| 129 |
~rcVectorBase() { destroy_range(0, m_size); rcFree(m_data); } |
| 130 |
|
| 131 |
|
| 132 |
bool reserve(rcSizeType size); |
| 133 |
|
| 134 |
void assign(rcSizeType count, const T& value) { clear(); resize(count, value); } |
| 135 |
void assign(const T* begin, const T* end); |
| 136 |
|
| 137 |
void resize(rcSizeType size) { resize_impl(size, NULL); } |
| 138 |
void resize(rcSizeType size, const T& value) { resize_impl(size, &value); } |
| 139 |
|
| 140 |
void clear() { destroy_range(0, m_size); m_size = 0; } |
| 141 |
|
| 142 |
void push_back(const T& value); |
| 143 |
void pop_back() { rcAssert(m_size > 0); back().~T(); m_size--; } |
| 144 |
|
| 145 |
rcSizeType size() const { return m_size; } |
| 146 |
rcSizeType capacity() const { return m_cap; } |
| 147 |
bool empty() const { return size() == 0; } |
| 148 |
|
| 149 |
const T& operator[](rcSizeType i) const { rcAssert(i >= 0 && i < m_size); return m_data[i]; } |
| 150 |
T& operator[](rcSizeType i) { rcAssert(i >= 0 && i < m_size); return m_data[i]; } |
| 151 |
|
| 152 |
const T& front() const { rcAssert(m_size); return m_data[0]; } |
| 153 |
T& front() { rcAssert(m_size); return m_data[0]; } |
| 154 |
const T& back() const { rcAssert(m_size); return m_data[m_size - 1]; } |
| 155 |
T& back() { rcAssert(m_size); return m_data[m_size - 1]; } |
| 156 |
const T* data() const { return m_data; } |
| 157 |
T* data() { return m_data; } |
| 158 |
|
| 159 |
T* begin() { return m_data; } |
| 160 |
T* end() { return m_data + m_size; } |
| 161 |
const T* begin() const { return m_data; } |
| 162 |
const T* end() const { return m_data + m_size; } |
| 163 |
|
| 164 |
void swap(rcVectorBase<T, H>& other); |
| 165 |
|
| 166 |
|
| 167 |
rcVectorBase& operator=(const rcVectorBase<T, H>& other); |
| 168 |
}; |
| 169 |
|
| 170 |
template<typename T, rcAllocHint H> |
| 171 |
bool rcVectorBase<T, H>::reserve(rcSizeType count) { |
| 172 |
if (count <= m_cap) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
T* new_data = allocate_and_copy(count); |
| 176 |
if (!new_data) { |
| 177 |
return false; |
| 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 |
template <typename T, rcAllocHint H> |
| 186 |
T* rcVectorBase<T, H>::allocate_and_copy(rcSizeType size) { |
| 187 |
rcAssert(RC_SIZE_MAX / static_cast<rcSizeType>(sizeof(T)) >= size); |
| 188 |
T* new_data = static_cast<T*>(rcAlloc(sizeof(T) * size, H)); |
| 189 |
if (new_data) { |
| 190 |
copy_range(new_data, m_data, m_data + m_size); |
| 191 |
} |
| 192 |
return new_data; |
| 193 |
} |
| 194 |
template <typename T, rcAllocHint H> |
| 195 |
void rcVectorBase<T, H>::assign(const T* begin, const T* end) { |
| 196 |
clear(); |
| 197 |
reserve(end - begin); |
| 198 |
m_size = end - begin; |
| 199 |
copy_range(m_data, begin, end); |
| 200 |
} |
| 201 |
template <typename T, rcAllocHint H> |
| 202 |
void rcVectorBase<T, H>::push_back(const T& value) { |
| 203 |
|
| 204 |
|
| 205 |
if (rcLikely(m_size < m_cap)) { |
| 206 |
construct(m_data + m_size++, value); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
const rcSizeType new_cap = get_new_capacity(m_cap + 1); |
| 211 |
T* data = allocate_and_copy(new_cap); |
| 212 |
|
| 213 |
|
| 214 |
construct(data + m_size, value); |
| 215 |
destroy_range(0, m_size); |
| 216 |
m_size++; |
| 217 |
m_cap = new_cap; |
| 218 |
rcFree(m_data); |
| 219 |
m_data = data; |
| 220 |
} |
| 221 |
|
| 222 |
template <typename T, rcAllocHint H> |
| 223 |
rcSizeType rcVectorBase<T, H>::get_new_capacity(rcSizeType min_capacity) { |
| 224 |
rcAssert(min_capacity <= RC_SIZE_MAX); |
| 225 |
if (rcUnlikely(m_cap >= RC_SIZE_MAX / 2)) |
| 226 |
return RC_SIZE_MAX; |
| 227 |
return 2 * m_cap > min_capacity ? 2 * m_cap : min_capacity; |
| 228 |
} |
| 229 |
|
| 230 |
template <typename T, rcAllocHint H> |
| 231 |
void rcVectorBase<T, H>::resize_impl(rcSizeType size, const T* value) { |
| 232 |
if (size < m_size) { |
| 233 |
destroy_range(size, m_size); |
| 234 |
m_size = size; |
| 235 |
} else if (size > m_size) { |
| 236 |
if (size <= m_cap) { |
| 237 |
if (value) { |
| 238 |
construct_range(m_data + m_size, m_data + size, *value); |
| 239 |
} else { |
| 240 |
construct_range(m_data + m_size, m_data + size); |
| 241 |
} |
| 242 |
m_size = size; |
| 243 |
} else { |
| 244 |
const rcSizeType new_cap = get_new_capacity(size); |
| 245 |
T* new_data = allocate_and_copy(new_cap); |
| 246 |
|
| 247 |
|
| 248 |
if (value) { |
| 249 |
construct_range(new_data + m_size, new_data + size, *value); |
| 250 |
} else { |
| 251 |
construct_range(new_data + m_size, new_data + size); |
| 252 |
} |
| 253 |
destroy_range(0, m_size); |
| 254 |
rcFree(m_data); |
| 255 |
m_data = new_data; |
| 256 |
m_cap = new_cap; |
| 257 |
m_size = size; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
template <typename T, rcAllocHint H> |
| 262 |
void rcVectorBase<T, H>::swap(rcVectorBase<T, H>& other) { |
| 263 |
|
| 264 |
rcSizeType tmp_cap = other.m_cap; |
| 265 |
rcSizeType tmp_size = other.m_size; |
| 266 |
T* tmp_data = other.m_data; |
| 267 |
|
| 268 |
other.m_cap = m_cap; |
| 269 |
other.m_size = m_size; |
| 270 |
other.m_data = m_data; |
| 271 |
|
| 272 |
m_cap = tmp_cap; |
| 273 |
m_size = tmp_size; |
| 274 |
m_data = tmp_data; |
| 275 |
} |
| 276 |
|
| 277 |
template <typename T, rcAllocHint H> |
| 278 |
void rcVectorBase<T, H>::construct_range(T* begin, T* end) { |
| 279 |
for (T* p = begin; p < end; p++) { |
| 280 |
construct(p); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
template <typename T, rcAllocHint H> |
| 285 |
void rcVectorBase<T, H>::construct_range(T* begin, T* end, const T& value) { |
| 286 |
for (T* p = begin; p < end; p++) { |
| 287 |
construct(p, value); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
template <typename T, rcAllocHint H> |
| 292 |
void rcVectorBase<T, H>::copy_range(T* dst, const T* begin, const T* end) { |
| 293 |
for (rcSizeType i = 0 ; i < end - begin; i++) { |
| 294 |
construct(dst + i, begin[i]); |
| 295 |
} |
| 296 |
} |
| 297 |
template <typename T, rcAllocHint H> |
| 298 |
void rcVectorBase<T, H>::destroy_range(rcSizeType begin, rcSizeType end) { |
| 299 |
for (rcSizeType i = begin; i < end; i++) { |
| 300 |
m_data[i].~T(); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
template <typename T> |
| 305 |
class rcTempVector : public rcVectorBase<T, RC_ALLOC_TEMP> { |
| 306 |
typedef rcVectorBase<T, RC_ALLOC_TEMP> Base; |
| 307 |
public: |
| 308 |
rcTempVector() : Base() {} |
| 309 |
explicit rcTempVector(rcSizeType size) : Base(size) {} |
| 310 |
rcTempVector(rcSizeType size, const T& value) : Base(size, value) {} |
| 311 |
rcTempVector(const rcTempVector<T>& other) : Base(other) {} |
| 312 |
rcTempVector(const T* begin, const T* end) : Base(begin, end) {} |
| 313 |
}; |
| 314 |
template <typename T> |
| 315 |
class rcPermVector : public rcVectorBase<T, RC_ALLOC_PERM> { |
| 316 |
typedef rcVectorBase<T, RC_ALLOC_PERM> Base; |
| 317 |
public: |
| 318 |
rcPermVector() : Base() {} |
| 319 |
explicit rcPermVector(rcSizeType size) : Base(size) {} |
| 320 |
rcPermVector(rcSizeType size, const T& value) : Base(size, value) {} |
| 321 |
rcPermVector(const rcPermVector<T>& other) : Base(other) {} |
| 322 |
rcPermVector(const T* begin, const T* end) : Base(begin, end) {} |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
class rcIntArray |
| 328 |
{ |
| 329 |
rcTempVector<int> m_impl; |
| 330 |
public: |
| 331 |
rcIntArray() {} |
| 332 |
rcIntArray(int n) : m_impl(n, 0) {} |
| 333 |
void push(int item) { m_impl.push_back(item); } |
| 334 |
void resize(int size) { m_impl.resize(size); } |
| 335 |
void clear() { m_impl.clear(); } |
| 336 |
int pop() |
| 337 |
{ |
| 338 |
int v = m_impl.back(); |
| 339 |
m_impl.pop_back(); |
| 340 |
return v; |
| 341 |
} |
| 342 |
int size() const { return static_cast<int>(m_impl.size()); } |
| 343 |
int& operator[](int index) { return m_impl[index]; } |
| 344 |
int operator[](int index) const { return m_impl[index]; } |
| 345 |
}; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
template<class T> class rcScopedDelete |
| 350 |
{ |
| 351 |
T* ptr; |
| 352 |
public: |
| 353 |
|
| 354 |
|
| 355 |
inline rcScopedDelete() : ptr(0) {} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
inline rcScopedDelete(T* p) : ptr(p) {} |
| 360 |
inline ~rcScopedDelete() { rcFree(ptr); } |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
inline operator T*() { return ptr; } |
| 365 |
|
| 366 |
private: |
| 367 |
|
| 368 |
rcScopedDelete(const rcScopedDelete&); |
| 369 |
rcScopedDelete& operator=(const rcScopedDelete&); |
| 370 |
}; |
| 371 |
|
| 372 |
#endif |