diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a6d464e473..85ed8222a5 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,6 +1,4 @@ add_files( - alloc_func.cpp - alloc_func.hpp alloc_type.hpp backup_type.hpp base_bitset_type.hpp diff --git a/src/core/alloc_func.cpp b/src/core/alloc_func.cpp deleted file mode 100644 index 00f9ecdb1a..0000000000 --- a/src/core/alloc_func.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 . - */ - -/** @file alloc_func.cpp Functions to 'handle' memory allocation errors */ - -#include "../stdafx.h" - -#include "../error_func.h" - -#include "../safeguards.h" - -/** - * Function to exit with an error message after malloc() or calloc() have failed - * @param size number of bytes we tried to allocate - */ -[[noreturn]] void MallocError(size_t size) -{ - FatalError("Out of memory. Cannot allocate {} bytes", size); -} - -/** - * Function to exit with an error message after realloc() have failed - * @param size number of bytes we tried to allocate - */ -[[noreturn]] void ReallocError(size_t size) -{ - FatalError("Out of memory. Cannot reallocate {} bytes", size); -} diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp deleted file mode 100644 index 3bc4dde683..0000000000 --- a/src/core/alloc_func.hpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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 . - */ - -/** @file alloc_func.hpp Functions related to the allocation of memory */ - -#ifndef ALLOC_FUNC_HPP -#define ALLOC_FUNC_HPP - -/* - * 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. - */ - -[[noreturn]] void MallocError(size_t size); -[[noreturn]] void ReallocError(size_t size); - -/** - * 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. - */ -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 -inline void CheckAllocationConstraints(size_t num_elements) -{ - CheckAllocationConstraints(sizeof(T), num_elements); -} - -/** - * 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). - * @tparam T the type of the variable(s) to allocation. - * @param num_elements the number of elements to allocate of the given type. - * @return nullptr when num_elements == 0, non-nullptr otherwise. - */ -template -inline T *MallocT(size_t num_elements) -{ - /* - * MorphOS cannot handle 0 elements allocations, or rather that always - * returns nullptr. So we do that for *all* allocations, thus causing it - * to behave the same on all OSes. - */ - if (num_elements == 0) return nullptr; - - /* Ensure the size does not overflow. */ - CheckAllocationConstraints(num_elements); - - T *t_ptr = (T*)malloc(num_elements * sizeof(T)); - if (t_ptr == nullptr) MallocError(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. - * @tparam T the type of the variable(s) to allocation. - * @param num_elements the number of elements to allocate of the given type. - * @return nullptr when num_elements == 0, non-nullptr otherwise. - */ -template -inline T *CallocT(size_t num_elements) -{ - /* - * MorphOS cannot handle 0 elements allocations, or rather that always - * returns nullptr. So we do that for *all* allocations, thus causing it - * to behave the same on all OSes. - */ - if (num_elements == 0) return nullptr; - - T *t_ptr = (T*)calloc(num_elements, sizeof(T)); - if (t_ptr == nullptr) MallocError(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 pointer to the data may change, but the data will remain valid. - * @tparam 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 nullptr when num_elements == 0, non-nullptr otherwise. - */ -template -inline T *ReallocT(T *t_ptr, size_t num_elements) -{ - /* - * MorphOS cannot handle 0 elements allocations, or rather that always - * returns nullptr. 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 nullptr; - } - - /* Ensure the size does not overflow. */ - CheckAllocationConstraints(num_elements); - - t_ptr = (T*)realloc(static_cast(t_ptr), num_elements * sizeof(T)); - if (t_ptr == nullptr) ReallocError(num_elements * sizeof(T)); - return t_ptr; -} - -#endif /* ALLOC_FUNC_HPP */ diff --git a/src/safeguards.h b/src/safeguards.h index daf941482a..ba8ab24eb5 100644 --- a/src/safeguards.h +++ b/src/safeguards.h @@ -10,22 +10,15 @@ * * Unsafe methods are, for example, strndup and strncpy because they may leave the * string without a null termination, but also strdup and strndup because they can - * return nullptr and then all strdups would need to be guarded against that instead - * of using the current MallocT/ReallocT/CallocT technique of just giving the user - * an error that too much memory was used instead of spreading that code though - * the whole code base. + * return nullptr and then all strdups would need to be guarded against. */ #ifndef SAFEGUARDS_H #define SAFEGUARDS_H -/* Use MallocT instead. */ +/* Use std::vector/std::unique_ptr/new instead. */ #define malloc SAFEGUARD_DO_NOT_USE_THIS_METHOD - -/* Use MallocT instead. */ #define calloc SAFEGUARD_DO_NOT_USE_THIS_METHOD - -/* Use ReallocT instead. */ #define realloc SAFEGUARD_DO_NOT_USE_THIS_METHOD /* Use std::string instead. */ diff --git a/src/spritecache.cpp b/src/spritecache.cpp index a81c5d4a32..a4eeb809ac 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -8,7 +8,6 @@ /** @file spritecache.cpp Caching of sprites. */ #include "stdafx.h" -#include "core/alloc_func.hpp" #include "random_access_file_type.h" #include "spriteloader/grf.hpp" #include "spriteloader/makeindexed.h" diff --git a/src/stdafx.h b/src/stdafx.h index e9d897c4ba..0584cb36fc 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -321,15 +321,6 @@ char (&ArraySizeHelper(T (&array)[N]))[N]; # define MAX_PATH 260 #endif -/** - * Version of the standard free that accepts const pointers. - * @param ptr The data to free. - */ -inline void free(const void *ptr) -{ - free(const_cast(ptr)); -} - #if defined(_MSC_VER) && !defined(_DEBUG) # define IGNORE_UNINITIALIZED_WARNING_START __pragma(warning(push)) __pragma(warning(disable:4700)) # define IGNORE_UNINITIALIZED_WARNING_STOP __pragma(warning(pop))