Atoms Crowd  7.0.0
RecastAlloc.h
1 // Modified version of Recast/Detour's source file
2 
3 //
4 // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 // 1. The origin of this software must not be misrepresented; you must not
13 // claim that you wrote the original software. If you use this software
14 // in a product, an acknowledgment in the product documentation would be
15 // appreciated but is not required.
16 // 2. Altered source versions must be plainly marked as such, and must not be
17 // misrepresented as being the original software.
18 // 3. This notice may not be removed or altered from any source distribution.
19 //
20 
21 #ifndef RECASTALLOC_H
22 #define RECASTALLOC_H
23 
24 #include <AtomsUtils/Globals.h>
25 #include <cstring>
26 #include <cmath>
27 
28 namespace AtomsUtils
29 {
33 {
36 };
37 
39 // @param[in] size The size, in bytes of memory, to allocate.
40 // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
41 // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
43 typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
44 
48 typedef void (rcFreeFunc)(void* ptr);
49 
53 ATOMSUTILS_EXPORT void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
54 
60 void* rcAlloc(int size, rcAllocHint hint);
61 
65 void rcFree(void* ptr);
66 
67 void rcMemCpy(void* dst, void* src, int size);
68 
71 {
72  int* m_data;
73  int m_size, m_cap;
74  inline rcIntArray(const rcIntArray&);
75  inline rcIntArray& operator=(const rcIntArray&);
76 public:
77 
79  inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
80 
83  inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
84  inline ~rcIntArray() { rcFree(m_data); }
85 
88  void resize(int n);
89 
92  inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
93 
96  inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
97 
101  inline const int& operator[](int i) const { return m_data[i]; }
102 
106  inline int& operator[](int i) { return m_data[i]; }
107 
109  inline int size() const { return m_size; }
110 
111  // added contains()
112  bool contains(int n) const;
113 };
114 
117 template<class T> class rcScopedDelete
118 {
119  T* ptr;
120  int size;
121  inline T* operator=(T* p);
122 public:
123 
125  inline rcScopedDelete() : ptr(0) {}
126  inline rcScopedDelete(int n) { ptr = (T*)rcAlloc(sizeof(T)*n, RC_ALLOC_TEMP); size = n; }
127 
130  inline rcScopedDelete(T* p) : ptr(p) {}
131  inline ~rcScopedDelete() { rcFree(ptr); ptr = 0; size = 0; }
132 
135  inline operator T*() { return ptr; }
136 
138  bool resizeGrow(int n);
139 };
140 
141 template<class T>
143 {
144  if (n > size)
145  {
146  T* newData = (T*)rcAlloc(sizeof(T) * n, RC_ALLOC_TEMP);
147  if (n && newData) rcMemCpy(newData, ptr, sizeof(T) * n);
148  rcFree(ptr);
149  ptr = newData;
150  size = n;
151  }
152 
153  return (ptr != 0);
154 }
155 
160 template<class T>
162 {
163  int itemCount;
164  T* ptr;
165 
166  // on purpose made private to restrict copying
167  inline T* operator=(T* p);
168 public:
169 
171  inline rcScopedStructArrayDelete(const int n) : itemCount(n) { ptr = (T*)rcAlloc(sizeof(T)*n, RC_ALLOC_TEMP); }
172 
174  {
175  for (int itemIndex = 0; itemIndex < itemCount; ++itemIndex)
176  {
177  ptr[itemIndex].~T();
178  }
179  rcFree(ptr);
180  }
181 
184  inline operator T*() { return ptr; }
185 };
186 
187 }
188 #endif
A simple dynamic array of integers.
Definition: RecastAlloc.h:71
rcIntArray()
Constructs an instance with an initial array size of zero.
Definition: RecastAlloc.h:79
int & operator[](int i)
Definition: RecastAlloc.h:106
int size() const
The current size of the integer array.
Definition: RecastAlloc.h:109
int pop()
Definition: RecastAlloc.h:96
const int & operator[](int i) const
Definition: RecastAlloc.h:101
void push(int item)
Definition: RecastAlloc.h:92
rcIntArray(int n)
Definition: RecastAlloc.h:83
Definition: RecastAlloc.h:118
rcScopedDelete(T *p)
Definition: RecastAlloc.h:130
rcScopedDelete()
Constructs an instance with a null pointer.
Definition: RecastAlloc.h:125
bool resizeGrow(int n)
resize and copy existing memory (n = element count), doesn't destruct elements!
Definition: RecastAlloc.h:142
Definition: RecastAlloc.h:162
rcScopedStructArrayDelete(const int n)
Constructs an array of instances of T.
Definition: RecastAlloc.h:171
AtomsCore namespace.
Definition: Base64.h:13
void * rcAlloc(int size, rcAllocHint hint)
void *() rcAllocFunc(int size, rcAllocHint hint)
A memory allocation function.
Definition: RecastAlloc.h:43
rcAllocHint
Definition: RecastAlloc.h:33
@ RC_ALLOC_TEMP
Memory used temporarily within a function.
Definition: RecastAlloc.h:35
@ RC_ALLOC_PERM
Memory will persist after a function call.
Definition: RecastAlloc.h:34
void rcFree(void *ptr)
void() rcFreeFunc(void *ptr)
Definition: RecastAlloc.h:48
ATOMSUTILS_EXPORT void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc)