(svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.

This commit is contained in:
rubidium
2008-01-22 12:09:12 +00:00
parent 30bac58bde
commit 59ec5e62ce
2 changed files with 121 additions and 136 deletions

View File

@@ -81,4 +81,35 @@ template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements)
return t_ptr;
}
/**
* A small 'wrapper' for allocations that can be done on most OSes on the
* stack, but are just too large to fit in the stack on devices with a small
* stack such as the NDS.
* So when it is possible a stack allocation is made, otherwise a heap
* allocation is made and this is freed once the struct goes out of scope.
* @param T the type to make the allocation for
* @param length the amount of items to allocate
*/
template <typename T, size_t length>
struct SmallStackSafeStackAlloc {
#if !defined(__NDS__)
/** Storing the data on the stack */
T data[length];
#else
/** Storing it on the heap */
T *data;
/** Allocating the memory */
SmallStackSafeStackAlloc() : data(MallocT<T>(length)) {}
/** And freeing when it goes out of scope */
~SmallStackSafeStackAlloc() { free(data); }
#endif
/**
* Gets a pointer to the data stored in this wrapper.
* @return the pointer.
*/
operator T* () { return data; }
};
#endif /* ALLOC_FUNC_HPP */