TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RecastAlloc.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
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)
Definition: RecastAlloc.h:74
int & operator[](int i)
Definition: RecastAlloc.h:97
int size() const
The current size of the integer array.
Definition: RecastAlloc.h:100
int m_size
Definition: RecastAlloc.h:64
T * operator=(T *p)
rcAllocHint
Definition: RecastAlloc.h:24
Definition: RecastAlloc.h:105
T * ptr
Definition: RecastAlloc.h:107
int * m_data
Definition: RecastAlloc.h:63
void rcFree(void *ptr)
Definition: RecastAlloc.cpp:55
int m_cap
Definition: RecastAlloc.h:64
void * rcAlloc(int size, rcAllocHint hint)
Definition: RecastAlloc.cpp:44
rcIntArray & operator=(const rcIntArray &)
int pop()
Definition: RecastAlloc.h:87
void *( rcAllocFunc)(int size, rcAllocHint hint)
A memory allocation function.
Definition: RecastAlloc.h:35
const int & operator[](int i) const
Definition: RecastAlloc.h:92
Memory will persist after a function call.
Definition: RecastAlloc.h:26
~rcIntArray()
Definition: RecastAlloc.h:75
void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc)
Definition: RecastAlloc.cpp:37
rcScopedDelete()
Constructs an instance with a null pointer.
Definition: RecastAlloc.h:112
rcScopedDelete(T *p)
Definition: RecastAlloc.h:116
Memory used temporarily within a function.
Definition: RecastAlloc.h:27
void( rcFreeFunc)(void *ptr)
Definition: RecastAlloc.h:40
rcIntArray()
Constructs an instance with an initial array size of zero.
Definition: RecastAlloc.h:70
void resize(int n)
Definition: RecastAlloc.cpp:75
void push(int item)
Definition: RecastAlloc.h:83
A simple dynamic array of integers.
Definition: RecastAlloc.h:61
~rcScopedDelete()
Definition: RecastAlloc.h:117