2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
8 /** @file alloc_func.hpp Functions related to the allocation of memory */
10 #ifndef ALLOC_FUNC_HPP
11 #define ALLOC_FUNC_HPP
14 * Functions to exit badly with an error message.
15 * It has to be linked so the error messages are not
16 * duplicated in each object file making the final
17 * binary needlessly large.
20 [[noreturn
]] void MallocError(size_t size
);
21 [[noreturn
]] void ReallocError(size_t size
);
24 * Checks whether allocating memory would overflow size_t.
26 * @param element_size Size of the structure to allocate.
27 * @param num_elements Number of elements to allocate.
29 inline void CheckAllocationConstraints(size_t element_size
, size_t num_elements
)
31 if (num_elements
> SIZE_MAX
/ element_size
) MallocError(SIZE_MAX
);
35 * Checks whether allocating memory would overflow size_t.
37 * @tparam T Structure to allocate.
38 * @param num_elements Number of elements to allocate.
41 inline void CheckAllocationConstraints(size_t num_elements
)
43 CheckAllocationConstraints(sizeof(T
), num_elements
);
47 * Simplified allocation function that allocates the specified number of
48 * elements of the given type. It also explicitly casts it to the requested
50 * @note throws an error when there is no memory anymore.
51 * @note the memory contains garbage data (i.e. possibly non-zero values).
52 * @tparam T the type of the variable(s) to allocation.
53 * @param num_elements the number of elements to allocate of the given type.
54 * @return nullptr when num_elements == 0, non-nullptr otherwise.
57 inline T
*MallocT(size_t num_elements
)
60 * MorphOS cannot handle 0 elements allocations, or rather that always
61 * returns nullptr. So we do that for *all* allocations, thus causing it
62 * to behave the same on all OSes.
64 if (num_elements
== 0) return nullptr;
66 /* Ensure the size does not overflow. */
67 CheckAllocationConstraints
<T
>(num_elements
);
69 T
*t_ptr
= (T
*)malloc(num_elements
* sizeof(T
));
70 if (t_ptr
== nullptr) MallocError(num_elements
* sizeof(T
));
75 * Simplified allocation function that allocates the specified number of
76 * elements of the given type. It also explicitly casts it to the requested
78 * @note throws an error when there is no memory anymore.
79 * @note the memory contains all zero values.
80 * @tparam T the type of the variable(s) to allocation.
81 * @param num_elements the number of elements to allocate of the given type.
82 * @return nullptr when num_elements == 0, non-nullptr otherwise.
85 inline T
*CallocT(size_t num_elements
)
88 * MorphOS cannot handle 0 elements allocations, or rather that always
89 * returns nullptr. So we do that for *all* allocations, thus causing it
90 * to behave the same on all OSes.
92 if (num_elements
== 0) return nullptr;
94 T
*t_ptr
= (T
*)calloc(num_elements
, sizeof(T
));
95 if (t_ptr
== nullptr) MallocError(num_elements
* sizeof(T
));
100 * Simplified reallocation function that allocates the specified number of
101 * elements of the given type. It also explicitly casts it to the requested
102 * type. It extends/shrinks the memory allocation given in t_ptr.
103 * @note throws an error when there is no memory anymore.
104 * @note the pointer to the data may change, but the data will remain valid.
105 * @tparam T the type of the variable(s) to allocation.
106 * @param t_ptr the previous allocation to extend/shrink.
107 * @param num_elements the number of elements to allocate of the given type.
108 * @return nullptr when num_elements == 0, non-nullptr otherwise.
110 template <typename T
>
111 inline T
*ReallocT(T
*t_ptr
, size_t num_elements
)
114 * MorphOS cannot handle 0 elements allocations, or rather that always
115 * returns nullptr. So we do that for *all* allocations, thus causing it
116 * to behave the same on all OSes.
118 if (num_elements
== 0) {
123 /* Ensure the size does not overflow. */
124 CheckAllocationConstraints
<T
>(num_elements
);
126 t_ptr
= (T
*)realloc(static_cast<void *>(t_ptr
), num_elements
* sizeof(T
));
127 if (t_ptr
== nullptr) ReallocError(num_elements
* sizeof(T
));
131 #endif /* ALLOC_FUNC_HPP */