From 38d690acc80f2b9fec134fa147bbff43000f69aa Mon Sep 17 00:00:00 2001 From: rubidium Date: Tue, 22 Jan 2008 12:09:12 +0000 Subject: (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. --- src/core/alloc_func.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/core/alloc_func.hpp') diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp index 5d88e64e8..97e598598 100644 --- a/src/core/alloc_func.hpp +++ b/src/core/alloc_func.hpp @@ -81,4 +81,35 @@ template 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 +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(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 */ -- cgit v1.2.3-70-g09d2