79 t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
79 t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); |
80 if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T)); |
80 if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T)); |
81 return t_ptr; |
81 return t_ptr; |
82 } |
82 } |
83 |
83 |
|
84 /** |
|
85 * A small 'wrapper' for allocations that can be done on most OSes on the |
|
86 * stack, but are just too large to fit in the stack on devices with a small |
|
87 * stack such as the NDS. |
|
88 * So when it is possible a stack allocation is made, otherwise a heap |
|
89 * allocation is made and this is freed once the struct goes out of scope. |
|
90 * @param T the type to make the allocation for |
|
91 * @param length the amount of items to allocate |
|
92 */ |
|
93 template <typename T, size_t length> |
|
94 struct SmallStackSafeStackAlloc { |
|
95 #if !defined(__NDS__) |
|
96 /** Storing the data on the stack */ |
|
97 T data[length]; |
|
98 #else |
|
99 /** Storing it on the heap */ |
|
100 T *data; |
|
101 |
|
102 /** Allocating the memory */ |
|
103 SmallStackSafeStackAlloc() : data(MallocT<T>(length)) {} |
|
104 /** And freeing when it goes out of scope */ |
|
105 ~SmallStackSafeStackAlloc() { free(data); } |
|
106 #endif |
|
107 |
|
108 /** |
|
109 * Gets a pointer to the data stored in this wrapper. |
|
110 * @return the pointer. |
|
111 */ |
|
112 operator T* () { return data; } |
|
113 }; |
|
114 |
84 #endif /* ALLOC_FUNC_HPP */ |
115 #endif /* ALLOC_FUNC_HPP */ |