summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-01-22 12:09:12 +0000
committerrubidium <rubidium@openttd.org>2008-01-22 12:09:12 +0000
commit38d690acc80f2b9fec134fa147bbff43000f69aa (patch)
treec481b2db791059dc572d0f5d881837280e8590f2 /src/core
parent5e4f4bcdc5a1a3e73acf00f10facc57be87b2800 (diff)
downloadopenttd-38d690acc80f2b9fec134fa147bbff43000f69aa.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.hpp31
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 */