OpenTTD Source 20241224-master-gf74b0cf984
alloc_func.hpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
10#ifndef ALLOC_FUNC_HPP
11#define ALLOC_FUNC_HPP
12
13/*
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.
18 */
19
20[[noreturn]] void MallocError(size_t size);
21[[noreturn]] void ReallocError(size_t size);
22
29inline void CheckAllocationConstraints(size_t element_size, size_t num_elements)
30{
31 if (num_elements > SIZE_MAX / element_size) MallocError(SIZE_MAX);
32}
33
40template <typename T>
41inline void CheckAllocationConstraints(size_t num_elements)
42{
43 CheckAllocationConstraints(sizeof(T), num_elements);
44}
45
56template <typename T>
57inline T *MallocT(size_t num_elements)
58{
59 /*
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.
63 */
64 if (num_elements == 0) return nullptr;
65
66 /* Ensure the size does not overflow. */
67 CheckAllocationConstraints<T>(num_elements);
68
69 T *t_ptr = (T*)malloc(num_elements * sizeof(T));
70 if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
71 return t_ptr;
72}
73
84template <typename T>
85inline T *CallocT(size_t num_elements)
86{
87 /*
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.
91 */
92 if (num_elements == 0) return nullptr;
93
94 T *t_ptr = (T*)calloc(num_elements, sizeof(T));
95 if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
96 return t_ptr;
97}
98
110template <typename T>
111inline T *ReallocT(T *t_ptr, size_t num_elements)
112{
113 /*
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.
117 */
118 if (num_elements == 0) {
119 free(t_ptr);
120 return nullptr;
121 }
122
123 /* Ensure the size does not overflow. */
124 CheckAllocationConstraints<T>(num_elements);
125
126 t_ptr = (T*)realloc(static_cast<void *>(t_ptr), num_elements * sizeof(T));
127 if (t_ptr == nullptr) ReallocError(num_elements * sizeof(T));
128 return t_ptr;
129}
130
131#endif /* ALLOC_FUNC_HPP */
T * CallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
void ReallocError(size_t size)
Function to exit with an error message after realloc() have failed.
T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
void MallocError(size_t size)
Function to exit with an error message after malloc() or calloc() have failed.
void CheckAllocationConstraints(size_t element_size, size_t num_elements)
Checks whether allocating memory would overflow size_t.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:334