diff options
author | rubidium <rubidium@openttd.org> | 2007-12-25 09:48:53 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-12-25 09:48:53 +0000 |
commit | 00c9d3ef20c83df43118a9ace5b8dd8eca26a126 (patch) | |
tree | 7d96120bc1feabf7069c9cd673a8da769e8f90fd /src/core | |
parent | 15c1af051752e2a44b0eacea419e72f6a897b491 (diff) | |
download | openttd-00c9d3ef20c83df43118a9ace5b8dd8eca26a126.tar.xz |
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/alloc_func.hpp | 84 | ||||
-rw-r--r-- | src/core/math_func.hpp | 12 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp new file mode 100644 index 000000000..5d88e64e8 --- /dev/null +++ b/src/core/alloc_func.hpp @@ -0,0 +1,84 @@ +/* $Id$ */ + +/** @file alloc_func.hpp Functions related to the allocation of memory */ + +#ifndef ALLOC_FUNC_HPP +#define ALLOC_FUNC_HPP + +/** + * Simplified allocation function that allocates the specified number of + * elements of the given type. It also explicitly casts it to the requested + * type. + * @note throws an error when there is no memory anymore. + * @note the memory contains garbage data (i.e. possibly non-zero values). + * @param T the type of the variable(s) to allocation. + * @param num_elements the number of elements to allocate of the given type. + * @return NULL when num_elements == 0, non-NULL otherwise. + */ +template <typename T> FORCEINLINE T* MallocT(size_t num_elements) +{ + /* + * MorphOS cannot handle 0 elements allocations, or rather that always + * returns NULL. So we do that for *all* allocations, thus causing it + * to behave the same on all OSes. + */ + if (num_elements == 0) return NULL; + + T *t_ptr = (T*)malloc(num_elements * sizeof(T)); + if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); + return t_ptr; +} + +/** + * Simplified allocation function that allocates the specified number of + * elements of the given type. It also explicitly casts it to the requested + * type. + * @note throws an error when there is no memory anymore. + * @note the memory contains all zero values. + * @param T the type of the variable(s) to allocation. + * @param num_elements the number of elements to allocate of the given type. + * @return NULL when num_elements == 0, non-NULL otherwise. + */ +template <typename T> FORCEINLINE T* CallocT(size_t num_elements) +{ + /* + * MorphOS cannot handle 0 elements allocations, or rather that always + * returns NULL. So we do that for *all* allocations, thus causing it + * to behave the same on all OSes. + */ + if (num_elements == 0) return NULL; + + T *t_ptr = (T*)calloc(num_elements, sizeof(T)); + if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T)); + return t_ptr; +} + +/** + * Simplified reallocation function that allocates the specified number of + * elements of the given type. It also explicitly casts it to the requested + * type. It extends/shrinks the memory allocation given in t_ptr. + * @note throws an error when there is no memory anymore. + * @note the memory contains all zero values. + * @param T the type of the variable(s) to allocation. + * @param t_ptr the previous allocation to extend/shrink. + * @param num_elements the number of elements to allocate of the given type. + * @return NULL when num_elements == 0, non-NULL otherwise. + */ +template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements) +{ + /* + * MorphOS cannot handle 0 elements allocations, or rather that always + * returns NULL. So we do that for *all* allocations, thus causing it + * to behave the same on all OSes. + */ + if (num_elements == 0) { + free(t_ptr); + return NULL; + } + + t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T)); + if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T)); + return t_ptr; +} + +#endif /* ALLOC_FUNC_HPP */ diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index c05e7ffeb..e20dc6065 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -211,4 +211,16 @@ template<typename T> static inline bool IsInsideMM(const T x, const uint min, co return (uint)(x - min) < (max - min); } +/** + * Type safe swap operation + * @param a variable to swap with b + * @param b variable to swap with a + */ +template<typename T> void Swap(T& a, T& b) +{ + T t = a; + a = b; + b = t; +} + #endif /* MATH_FUNC_HPP */ |