2
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
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.
22
/// Provides hint values to the memory allocator on how long the
23
/// memory is expected to be used.
26
RC_ALLOC_PERM, ///< Memory will persist after a function call.
27
RC_ALLOC_TEMP ///< Memory used temporarily within a function.
30
/// A memory allocation function.
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.
34
/// @see rcAllocSetCustom
35
typedef void* (rcAllocFunc)(int size, rcAllocHint hint);
37
/// A memory deallocation function.
38
/// @see rcAllocSetCustom
40
typedef void (rcFreeFunc)(void* ptr);
42
/// Sets the base custom allocation functions to be used by Recast.
43
/// @param[in] allocFunc The memory allocation function to be used by #rcAlloc
44
/// @param[in] freeFunc The memory de-allocation function to be used by #rcFree
45
void rcAllocSetCustom(rcAllocFunc *allocFunc, rcFreeFunc *freeFunc);
47
/// Allocates a memory block.
48
/// @param[in] size The size, in bytes of memory, to allocate.
49
/// @param[in] hint A hint to the allocator on how long the memory is expected to be in use.
50
/// @return A pointer to the beginning of the allocated memory block, or null if the allocation failed.
51
void* rcAlloc(int size, rcAllocHint hint);
53
/// Deallocates a memory block.
54
/// @param[in] ptr A pointer to a memory block previously allocated using #rcAlloc.
55
void rcFree(void* ptr);
58
/// A simple dynamic array of integers.
63
inline rcIntArray(const rcIntArray&);
64
inline rcIntArray& operator=(const rcIntArray&);
67
/// Constructs an instance with an initial array size of zero.
68
inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
70
/// Constructs an instance initialized to the specified size.
71
/// @param[in] n The initial size of the integer array.
72
inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(0) { resize(n); }
73
inline ~rcIntArray() { rcFree(m_data); }
75
/// Specifies the new size of the integer array.
76
/// @param[in] n The new size of the integer array.
79
/// Push the specified integer onto the end of the array and increases the size by one.
80
/// @param[in] item The new value.
81
inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
83
/// Returns the value at the end of the array and reduces the size by one.
84
/// @return The value at the end of the array.
85
inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
87
/// The value at the specified array index.
88
/// @warning Does not provide overflow protection.
89
/// @param[in] i The index of the value.
90
inline const int& operator[](int i) const { return m_data[i]; }
92
/// The value at the specified array index.
93
/// @warning Does not provide overflow protection.
94
/// @param[in] i The index of the value.
95
inline int& operator[](int i) { return m_data[i]; }
97
/// The current size of the integer array.
98
inline int size() const { return m_size; }
101
/// A simple helper class used to delete an array when it goes out of scope.
102
/// @note This class is rarely if ever used by the end user.
103
template<class T> class rcScopedDelete
106
inline T* operator=(T* p);
109
/// Constructs an instance with a null pointer.
110
inline rcScopedDelete() : ptr(0) {}
112
/// Constructs an instance with the specified pointer.
113
/// @param[in] p An pointer to an allocated array.
114
inline rcScopedDelete(T* p) : ptr(p) {}
115
inline ~rcScopedDelete() { rcFree(ptr); }
117
/// The root array pointer.
118
/// @return The root array pointer.
119
inline operator T*() { return ptr; }