Planeshift

RecastAlloc.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 // Permission is granted to anyone to use this software for any purpose,
00008 // including commercial applications, and to alter it and redistribute it
00009 // freely, subject to the following restrictions:
00010 // 1. The origin of this software must not be misrepresented; you must not
00011 //    claim that you wrote the original software. If you use this software
00012 //    in a product, an acknowledgment in the product documentation would be
00013 //    appreciated but is not required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 
00019 #ifndef RECASTALLOC_H
00020 #define RECASTALLOC_H
00021 
00024 enum rcAllocHint
00025 {
00026         RC_ALLOC_PERM,          
00027         RC_ALLOC_TEMP           
00028 };
00029 
00031 //  @param[in]          size                    The size, in bytes of memory, to allocate.
00032 //  @param[in]          rcAllocHint     A hint to the allocator on how long the memory is expected to be in use.
00033 //  @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
00035 typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
00036 
00040 typedef void (rcFreeFunc)(void* ptr);
00041 
00045 void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
00046 
00052 void* rcAlloc(int size, rcAllocHint hint);
00053 
00057 void rcFree(void* ptr);
00058 
00059 
00061 class rcIntArray
00062 {
00063         int* m_data;
00064         int m_size, m_cap;
00065         inline rcIntArray(const rcIntArray&);
00066         inline rcIntArray& operator=(const rcIntArray&);
00067 public:
00068 
00070         inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
00071 
00074         inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
00075         inline ~rcIntArray() { rcFree(m_data); }
00076 
00079         void resize(int n);
00080 
00083         inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
00084 
00087         inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
00088 
00092         inline const int& operator[](int i) const { return m_data[i]; }
00093 
00097         inline int& operator[](int i) { return m_data[i]; }
00098 
00100         inline int size() const { return m_size; }
00101 };
00102 
00105 template<class T> class rcScopedDelete
00106 {
00107         T* ptr;
00108         inline T* operator=(T* p);
00109 public:
00110 
00112         inline rcScopedDelete() : ptr(0) {}
00113 
00116         inline rcScopedDelete(T* p) : ptr(p) {}
00117         inline ~rcScopedDelete() { rcFree(ptr); }
00118 
00121         inline operator T*() { return ptr; }
00122 };
00123 
00124 #endif