diff options
author | rubidium <rubidium@openttd.org> | 2008-01-22 12:09:12 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2008-01-22 12:09:12 +0000 |
commit | 59ec5e62ce645e09657764344108cae3b7b03632 (patch) | |
tree | c481b2db791059dc572d0f5d881837280e8590f2 /src/core | |
parent | 30bac58bde83b356fbb884033f380881190ef69b (diff) | |
download | openttd-59ec5e62ce645e09657764344108cae3b7b03632.tar.xz |
(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.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/alloc_func.hpp | 31 |
1 files changed, 31 insertions, 0 deletions
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 <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 */ |