Planeshift
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 
25 {
28 };
29 
31 // @param[in] size The size, in bytes of memory, to allocate.
32 // @param[in] rcAllocHint A hint to the allocator on how long the memory is expected to be in use.
33 // @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
35 typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
36 
40 typedef void (rcFreeFunc)(void* ptr);
41 
45 void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
46 
52 void* rcAlloc(int size, rcAllocHint hint);
53 
57 void rcFree(void* ptr);
58 
59 
62 {
63  int* m_data;
64  int m_size, m_cap;
65  inline rcIntArray(const rcIntArray&);
66  inline rcIntArray& operator=(const rcIntArray&);
67 public:
68 
70  inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
71 
74  inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
75  inline ~rcIntArray() { rcFree(m_data); }
76 
79  void resize(int n);
80 
83  inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
84 
87  inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
88 
92  inline const int& operator[](int i) const { return m_data[i]; }
93 
97  inline int& operator[](int i) { return m_data[i]; }
98 
100  inline int size() const { return m_size; }
101 };
102 
105 template<class T> class rcScopedDelete
106 {
107  T* ptr;
108  inline T* operator=(T* p);
109 public:
110 
112  inline rcScopedDelete() : ptr(0) {}
113 
116  inline rcScopedDelete(T* p) : ptr(p) {}
117  inline ~rcScopedDelete() { rcFree(ptr); }
118 
121  inline operator T*() { return ptr; }
122 };
123 
124 #endif
rcIntArray(int n)
Constructs an instance initialized to the specified size.
Definition: RecastAlloc.h:74
int & operator[](int i)
The value at the specified array index.
Definition: RecastAlloc.h:97
int size() const
The current size of the integer array.
Definition: RecastAlloc.h:100
rcAllocHint
Provides hint values to the memory allocator on how long the memory is expected to be used...
Definition: RecastAlloc.h:24
A simple helper class used to delete an array when it goes out of scope.
Definition: RecastAlloc.h:105
void *( rcAllocFunc)(int size, rcAllocHint hint)
A memory allocation function.
Definition: RecastAlloc.h:35
void rcFree(void *ptr)
Deallocates a memory block.
void * rcAlloc(int size, rcAllocHint hint)
Allocates a memory block.
int pop()
Returns the value at the end of the array and reduces the size by one.
Definition: RecastAlloc.h:87
const int & operator[](int i) const
The value at the specified array index.
Definition: RecastAlloc.h:92
Memory will persist after a function call.
Definition: RecastAlloc.h:26
void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc)
Sets the base custom allocation functions to be used by Recast.
rcScopedDelete()
Constructs an instance with a null pointer.
Definition: RecastAlloc.h:112
rcScopedDelete(T *p)
Constructs an instance with the specified pointer.
Definition: RecastAlloc.h:116
Memory used temporarily within a function.
Definition: RecastAlloc.h:27
rcIntArray()
Constructs an instance with an initial array size of zero.
Definition: RecastAlloc.h:70
void( rcFreeFunc)(void *ptr)
A memory deallocation function.
Definition: RecastAlloc.h:40
void resize(int n)
Specifies the new size of the integer array.
void push(int item)
Push the specified integer onto the end of the array and increases the size by one.
Definition: RecastAlloc.h:83
A simple dynamic array of integers.
Definition: RecastAlloc.h:61