4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file alloc_func.hpp Functions related to the allocation of memory */
12 #ifndef ALLOC_FUNC_HPP
13 #define ALLOC_FUNC_HPP
16 * Functions to exit badly with an error message.
17 * It has to be linked so the error messages are not
18 * duplicated in each object file making the final
19 * binary needlessly large.
22 void NORETURN
MallocError(size_t size
);
23 void NORETURN
ReallocError(size_t size
);
26 * Checks whether allocating memory would overflow size_t.
28 * @param element_size Size of the structure to allocate.
29 * @param num_elements Number of elements to allocate.
31 static inline void CheckAllocationConstraints(size_t element_size
, size_t num_elements
)
33 if (num_elements
> SIZE_MAX
/ element_size
) MallocError(SIZE_MAX
);
37 * Checks whether allocating memory would overflow size_t.
39 * @tparam T Structure to allocate.
40 * @param num_elements Number of elements to allocate.
43 static inline void CheckAllocationConstraints(size_t num_elements
)
45 CheckAllocationConstraints(sizeof(T
), num_elements
);
49 * Simplified allocation function that allocates the specified number of
50 * elements of the given type. It also explicitly casts it to the requested
52 * @note throws an error when there is no memory anymore.
53 * @note the memory contains garbage data (i.e. possibly non-zero values).
54 * @tparam T the type of the variable(s) to allocation.
55 * @param num_elements the number of elements to allocate of the given type.
56 * @return nullptr when num_elements == 0, non-nullptr otherwise.
59 static inline T
*MallocT(size_t num_elements
)
62 * MorphOS cannot handle 0 elements allocations, or rather that always
63 * returns nullptr. So we do that for *all* allocations, thus causing it
64 * to behave the same on all OSes.
66 if (num_elements
== 0) return nullptr;
68 /* Ensure the size does not overflow. */
69 CheckAllocationConstraints
<T
>(num_elements
);
71 T
*t_ptr
= (T
*)malloc(num_elements
* sizeof(T
));
72 if (t_ptr
== nullptr) MallocError(num_elements
* sizeof(T
));
77 * Simplified allocation function that allocates the specified number of
78 * elements of the given type. It also explicitly casts it to the requested
80 * @note throws an error when there is no memory anymore.
81 * @note the memory contains all zero values.
82 * @tparam T the type of the variable(s) to allocation.
83 * @param num_elements the number of elements to allocate of the given type.
84 * @return nullptr when num_elements == 0, non-nullptr otherwise.
87 static inline T
*CallocT(size_t num_elements
)
90 * MorphOS cannot handle 0 elements allocations, or rather that always
91 * returns nullptr. So we do that for *all* allocations, thus causing it
92 * to behave the same on all OSes.
94 if (num_elements
== 0) return nullptr;
96 T
*t_ptr
= (T
*)calloc(num_elements
, sizeof(T
));
97 if (t_ptr
== nullptr) MallocError(num_elements
* sizeof(T
));
102 * Simplified reallocation function that allocates the specified number of
103 * elements of the given type. It also explicitly casts it to the requested
104 * type. It extends/shrinks the memory allocation given in t_ptr.
105 * @note throws an error when there is no memory anymore.
106 * @note the pointer to the data may change, but the data will remain valid.
107 * @tparam T the type of the variable(s) to allocation.
108 * @param t_ptr the previous allocation to extend/shrink.
109 * @param num_elements the number of elements to allocate of the given type.
110 * @return nullptr when num_elements == 0, non-nullptr otherwise.
112 template <typename T
>
113 static inline T
*ReallocT(T
*t_ptr
, size_t num_elements
)
116 * MorphOS cannot handle 0 elements allocations, or rather that always
117 * returns nullptr. So we do that for *all* allocations, thus causing it
118 * to behave the same on all OSes.
120 if (num_elements
== 0) {
125 /* Ensure the size does not overflow. */
126 CheckAllocationConstraints
<T
>(num_elements
);
128 t_ptr
= (T
*)realloc(static_cast<void *>(t_ptr
), num_elements
* sizeof(T
));
129 if (t_ptr
== nullptr) ReallocError(num_elements
* sizeof(T
));
133 /** alloca() has to be called in the parent function, so define AllocaM() as a macro */
134 #define AllocaM(T, num_elements) \
135 (CheckAllocationConstraints<T>(num_elements), \
136 (T*)alloca((num_elements) * sizeof(T)))
138 #endif /* ALLOC_FUNC_HPP */