Atoms Crowd  7.0.0
DetourAlloc.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 DETOURALLOCATOR_H
20 #define DETOURALLOCATOR_H
21 
22 #include <AtomsUtils/Globals.h>
23 
24 namespace AtomsUtils {
25 
29 {
32 };
33 
35 // @param[in] size The size, in bytes of memory, to allocate.
36 // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
37 // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
39 typedef void* (dtAllocFunc)(int size, dtAllocHint hint);
40 
44 typedef void (dtFreeFunc)(void* ptr);
45 
49 ATOMSUTILS_EXPORT void dtAllocSetCustom(dtAllocFunc *allocFunc, dtFreeFunc *freeFunc);
50 
56 ATOMSUTILS_EXPORT void* dtAlloc(int size, dtAllocHint hint);
57 
61 ATOMSUTILS_EXPORT void dtFree(void* ptr);
62 
63 void dtMemCpy(void* dst, void* src, int size);
64 
67 template<class T> class dtScopedDelete
68 {
69  T* ptr;
70  inline T* operator=(T* p);
71 public:
72 
74  inline dtScopedDelete() : ptr(0) {}
75  inline dtScopedDelete(int n) { ptr = n ? (T*)dtAlloc(sizeof(T)*n, DT_ALLOC_TEMP) : 0; }
76 
79  inline dtScopedDelete(T* p) : ptr(p) {}
80  inline ~dtScopedDelete() { dtFree(ptr); }
81 
84  inline operator T*() { return ptr; }
85  inline T* get() { return ptr; }
86 };
87 
90 {
91  int* m_data;
92  int m_size, m_cap;
93  inline dtIntArray(const dtIntArray&);
94  inline dtIntArray& operator=(const dtIntArray&);
95 public:
96 
98  inline dtIntArray() : m_data(0), m_size(0), m_cap(0) {}
99 
102  inline dtIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
103  inline ~dtIntArray() { dtFree(m_data); }
104 
107  void resize(int n);
108 
111  inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
112 
115  inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
116 
120  inline const int& operator[](int i) const { return m_data[i]; }
121 
125  inline int& operator[](int i) { return m_data[i]; }
126 
128  inline int size() const { return m_size; }
129 
130  inline int* getData() const { return m_data; }
131 
132  void copy(const dtIntArray& src);
133 
134  bool contains(int v) const
135  {
136  for (int i = 0; i < m_size; i++)
137  if (m_data[i] == v)
138  return true;
139 
140  return false;
141  }
142 };
143 
145 template<class T>
147 {
148  T* m_data;
149  int m_size, m_cap;
150  inline dtChunkArray(const dtChunkArray&);
151  inline dtChunkArray& operator=(const dtChunkArray&);
152 public:
153 
155  inline dtChunkArray() : m_data(0), m_size(0), m_cap(0) {}
156 
159  inline dtChunkArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
160  inline ~dtChunkArray() { dtFree(m_data); }
161 
164  void resize(int n);
165 
168  inline void push(T item) { resize(m_size+1); m_data[m_size-1] = item; }
169 
172  inline T pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
173 
177  inline const T& operator[](int i) const { return m_data[i]; }
178 
182  inline T& operator[](int i) { return m_data[i]; }
183 
185  inline int size() const { return m_size; }
186 };
187 
188 template<class T>
190 {
191  if (n > m_cap)
192  {
193  if (!m_cap) m_cap = n;
194  while (m_cap < n) m_cap += 32;
195  T* newData = (T*)dtAlloc(m_cap*sizeof(T), DT_ALLOC_TEMP);
196  if (m_size && newData) dtMemCpy(newData, m_data, m_size*sizeof(T));
197  dtFree(m_data);
198  m_data = newData;
199  }
200  m_size = n;
201 }
202 
203 }
204 
205 #endif
A simple dynamic array of integers.
Definition: DetourAlloc.h:147
void push(T item)
Definition: DetourAlloc.h:168
void resize(int n)
Definition: DetourAlloc.h:189
T & operator[](int i)
Definition: DetourAlloc.h:182
dtChunkArray(int n)
Definition: DetourAlloc.h:159
T pop()
Definition: DetourAlloc.h:172
dtChunkArray()
Constructs an instance with an initial array size of zero.
Definition: DetourAlloc.h:155
const T & operator[](int i) const
Definition: DetourAlloc.h:177
int size() const
The current size of the integer array.
Definition: DetourAlloc.h:185
A simple dynamic array of integers.
Definition: DetourAlloc.h:90
int size() const
The current size of the integer array.
Definition: DetourAlloc.h:128
int pop()
Definition: DetourAlloc.h:115
int & operator[](int i)
Definition: DetourAlloc.h:125
dtIntArray(int n)
Definition: DetourAlloc.h:102
const int & operator[](int i) const
Definition: DetourAlloc.h:120
dtIntArray()
Constructs an instance with an initial array size of zero.
Definition: DetourAlloc.h:98
void push(int item)
Definition: DetourAlloc.h:111
Definition: DetourAlloc.h:68
dtScopedDelete(T *p)
Definition: DetourAlloc.h:79
dtScopedDelete()
Constructs an instance with a null pointer.
Definition: DetourAlloc.h:74
AtomsCore namespace.
Definition: Base64.h:13
ATOMSUTILS_EXPORT void * dtAlloc(int size, dtAllocHint hint)
dtAllocHint
Definition: DetourAlloc.h:29
@ DT_ALLOC_TEMP
Memory used temporarily within a function.
Definition: DetourAlloc.h:31
@ DT_ALLOC_PERM
Memory persist after a function call.
Definition: DetourAlloc.h:30
void() dtFreeFunc(void *ptr)
Definition: DetourAlloc.h:44
ATOMSUTILS_EXPORT void dtFree(void *ptr)
ATOMSUTILS_EXPORT void dtAllocSetCustom(dtAllocFunc *allocFunc, dtFreeFunc *freeFunc)
void *() dtAllocFunc(int size, dtAllocHint hint)
A memory allocation function.
Definition: DetourAlloc.h:39