2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
/** @file alloc_func.hpp Functions related to the allocation of memory */
|
|
|
|
|
|
|
|
#ifndef ALLOC_FUNC_HPP
|
|
|
|
#define ALLOC_FUNC_HPP
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/*
|
2008-02-11 20:23:38 +00:00
|
|
|
* Functions to exit badly with an error message.
|
|
|
|
* It has to be linked so the error messages are not
|
|
|
|
* duplicated in each object file making the final
|
|
|
|
* binary needlessly large.
|
|
|
|
*/
|
2011-01-22 09:53:15 +00:00
|
|
|
|
2008-04-18 21:38:36 +00:00
|
|
|
void NORETURN MallocError(size_t size);
|
|
|
|
void NORETURN ReallocError(size_t size);
|
2008-02-11 20:23:38 +00:00
|
|
|
|
2011-09-03 12:45:45 +00:00
|
|
|
/**
|
|
|
|
* Checks whether allocating memory would overflow size_t.
|
|
|
|
*
|
|
|
|
* @param element_size Size of the structure to allocate.
|
|
|
|
* @param num_elements Number of elements to allocate.
|
|
|
|
*/
|
|
|
|
static inline void CheckAllocationConstraints(size_t element_size, size_t num_elements)
|
|
|
|
{
|
|
|
|
if (num_elements > SIZE_MAX / element_size) MallocError(SIZE_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether allocating memory would overflow size_t.
|
|
|
|
*
|
|
|
|
* @tparam T Structure to allocate.
|
|
|
|
* @param num_elements Number of elements to allocate.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static inline void CheckAllocationConstraints(size_t num_elements)
|
|
|
|
{
|
|
|
|
CheckAllocationConstraints(sizeof(T), num_elements);
|
|
|
|
}
|
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
2009-09-19 15:17:47 +00:00
|
|
|
* @tparam T the type of the variable(s) to allocation.
|
2007-12-25 09:48:53 +00:00
|
|
|
* @param num_elements the number of elements to allocate of the given type.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
2007-12-25 09:48:53 +00:00
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T *MallocT(size_t num_elements)
|
2007-12-25 09:48:53 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* MorphOS cannot handle 0 elements allocations, or rather that always
|
2019-04-10 21:07:06 +00:00
|
|
|
* returns nullptr. So we do that for *all* allocations, thus causing it
|
2007-12-25 09:48:53 +00:00
|
|
|
* to behave the same on all OSes.
|
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
if (num_elements == 0) return nullptr;
|
2007-12-25 09:48:53 +00:00
|
|
|
|
2011-09-02 20:54:51 +00:00
|
|
|
/* Ensure the size does not overflow. */
|
2011-09-03 12:45:45 +00:00
|
|
|
CheckAllocationConstraints<T>(num_elements);
|
2011-09-02 20:54:51 +00:00
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
|
2007-12-25 09:48:53 +00:00
|
|
|
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.
|
2009-09-19 15:17:47 +00:00
|
|
|
* @tparam T the type of the variable(s) to allocation.
|
2007-12-25 09:48:53 +00:00
|
|
|
* @param num_elements the number of elements to allocate of the given type.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
2007-12-25 09:48:53 +00:00
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T *CallocT(size_t num_elements)
|
2007-12-25 09:48:53 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* MorphOS cannot handle 0 elements allocations, or rather that always
|
2019-04-10 21:07:06 +00:00
|
|
|
* returns nullptr. So we do that for *all* allocations, thus causing it
|
2007-12-25 09:48:53 +00:00
|
|
|
* to behave the same on all OSes.
|
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
if (num_elements == 0) return nullptr;
|
2007-12-25 09:48:53 +00:00
|
|
|
|
|
|
|
T *t_ptr = (T*)calloc(num_elements, sizeof(T));
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t_ptr == nullptr) MallocError(num_elements * sizeof(T));
|
2007-12-25 09:48:53 +00:00
|
|
|
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.
|
2009-01-29 02:42:36 +00:00
|
|
|
* @note the pointer to the data may change, but the data will remain valid.
|
2009-09-19 15:17:47 +00:00
|
|
|
* @tparam T the type of the variable(s) to allocation.
|
2007-12-25 09:48:53 +00:00
|
|
|
* @param t_ptr the previous allocation to extend/shrink.
|
|
|
|
* @param num_elements the number of elements to allocate of the given type.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return nullptr when num_elements == 0, non-nullptr otherwise.
|
2007-12-25 09:48:53 +00:00
|
|
|
*/
|
2008-06-22 15:21:51 +00:00
|
|
|
template <typename T>
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline T *ReallocT(T *t_ptr, size_t num_elements)
|
2007-12-25 09:48:53 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* MorphOS cannot handle 0 elements allocations, or rather that always
|
2019-04-10 21:07:06 +00:00
|
|
|
* returns nullptr. So we do that for *all* allocations, thus causing it
|
2007-12-25 09:48:53 +00:00
|
|
|
* to behave the same on all OSes.
|
|
|
|
*/
|
|
|
|
if (num_elements == 0) {
|
|
|
|
free(t_ptr);
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2007-12-25 09:48:53 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 20:54:51 +00:00
|
|
|
/* Ensure the size does not overflow. */
|
2011-09-03 12:45:45 +00:00
|
|
|
CheckAllocationConstraints<T>(num_elements);
|
2011-09-02 20:54:51 +00:00
|
|
|
|
2018-05-13 17:34:57 +00:00
|
|
|
t_ptr = (T*)realloc(static_cast<void *>(t_ptr), num_elements * sizeof(T));
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t_ptr == nullptr) ReallocError(num_elements * sizeof(T));
|
2007-12-25 09:48:53 +00:00
|
|
|
return t_ptr;
|
|
|
|
}
|
|
|
|
|
2008-06-10 21:59:22 +00:00
|
|
|
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
|
2011-09-02 20:54:51 +00:00
|
|
|
#define AllocaM(T, num_elements) \
|
2011-09-03 12:45:45 +00:00
|
|
|
(CheckAllocationConstraints<T>(num_elements), \
|
2011-09-02 20:54:51 +00:00
|
|
|
(T*)alloca((num_elements) * sizeof(T)))
|
2008-06-10 21:59:22 +00:00
|
|
|
|
2007-12-25 09:48:53 +00:00
|
|
|
#endif /* ALLOC_FUNC_HPP */
|