1
/* $Id: alloc_type.hpp 15649 2009-03-09 09:38:00Z rubidium $ */
3
/** @file alloc_type.hpp Helper types related to the allocation of memory */
8
#include "alloc_func.hpp"
11
* A small 'wrapper' for allocations that can be done on most OSes on the
12
* stack, but are just too large to fit in the stack on devices with a small
13
* stack such as the NDS.
14
* So when it is possible a stack allocation is made, otherwise a heap
15
* allocation is made and this is freed once the struct goes out of scope.
16
* @param T the type to make the allocation for
17
* @param length the amount of items to allocate
19
template <typename T, size_t length>
20
struct SmallStackSafeStackAlloc {
22
/** Storing the data on the stack */
25
/** Storing it on the heap */
27
/** The length (in elements) of data in this allocator. */
30
/** Allocating the memory */
31
SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
33
/** And freeing when it goes out of scope */
34
~SmallStackSafeStackAlloc()
41
* Gets a pointer to the data stored in this wrapper.
42
* @return the pointer.
44
FORCEINLINE operator T *()
50
* Gets a pointer to the data stored in this wrapper.
51
* @return the pointer.
53
FORCEINLINE T *operator -> ()
59
* Gets a pointer to the last data element stored in this wrapper.
60
* @note needed because endof does not work properly for pointers.
61
* @return the 'endof' pointer.
63
FORCEINLINE T *EndOf()
74
* A reusable buffer that can be used for places that temporary allocate
75
* a bit of memory and do that very often, or for places where static
76
* memory is allocated that might need to be reallocated sometimes.
78
* Every time Allocate or ZeroAllocate is called previous results of both
79
* functions will become invalid.
82
class ReusableBuffer {
84
T *buffer; ///< The real data buffer
85
size_t count; ///< Number of T elements in the buffer
88
/** Create a new buffer */
89
ReusableBuffer() : buffer(NULL), count(0) {}
90
/** Clear the buffer */
91
~ReusableBuffer() { free(this->buffer); }
94
* Get buffer of at least count times T.
95
* @note the buffer might be bigger
96
* @note calling this function invalidates any previous buffers given
97
* @param count the minimum buffer size
100
T *Allocate(size_t count)
102
if (this->count < count) {
104
this->buffer = MallocT<T>(count);
111
* Get buffer of at least count times T with zeroed memory.
112
* @note the buffer might be bigger
113
* @note calling this function invalidates any previous buffers given
114
* @param count the minimum buffer size
117
T *ZeroAllocate(size_t count)
119
if (this->count < count) {
121
this->buffer = CallocT<T>(count);
124
memset(this->buffer, 0, sizeof(T) * count);
130
* Get the currently allocated buffer.
133
FORCEINLINE const T *GetBuffer() const
140
* Base class that provides memory initialization on dynamically created objects.
141
* All allocated memory will be zeroed.
143
class ZeroedMemoryAllocator
146
ZeroedMemoryAllocator() {}
147
virtual ~ZeroedMemoryAllocator() {}
150
* Memory allocator for a single class instance.
151
* @param size the amount of bytes to allocate.
152
* @return the given amounts of bytes zeroed.
154
FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); }
157
* Memory allocator for an array of class instances.
158
* @param size the amount of bytes to allocate.
159
* @return the given amounts of bytes zeroed.
161
FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); }
164
* Memory release for a single class instance.
165
* @param ptr the memory to free.
166
* @param size the amount of allocated memory (unused).
168
* @warning The value of the \a size parameter can only be trusted for
169
* classes that have their own (virtual) destructor method.
171
FORCEINLINE void operator delete(void *ptr, size_t size) { free(ptr); }
174
* Memory release for an array of class instances.
175
* @param ptr the memory to free.
176
* @param size the amount of allocated memory (unused).
178
* @warning The value of the \a size parameter can only be trusted for
179
* classes that have their own (virtual) destructor method.
181
FORCEINLINE void operator delete[](void *ptr, size_t size) { free(ptr); }
184
#endif /* ALLOC_TYPE_HPP */