alloc_func.hpp

Go to the documentation of this file.
00001 /* $Id: alloc_func.hpp 23640 2011-12-20 17:57:56Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef ALLOC_FUNC_HPP
00013 #define ALLOC_FUNC_HPP
00014 
00015 /*
00016  * Functions to exit badly with an error message.
00017  * It has to be linked so the error messages are not
00018  * duplicated in each object file making the final
00019  * binary needlessly large.
00020  */
00021 
00022 void NORETURN MallocError(size_t size);
00023 void NORETURN ReallocError(size_t size);
00024 
00031 static inline void CheckAllocationConstraints(size_t element_size, size_t num_elements)
00032 {
00033   if (num_elements > SIZE_MAX / element_size) MallocError(SIZE_MAX);
00034 }
00035 
00042 template <typename T>
00043 static inline void CheckAllocationConstraints(size_t num_elements)
00044 {
00045   CheckAllocationConstraints(sizeof(T), num_elements);
00046 }
00047 
00058 template <typename T>
00059 static inline T *MallocT(size_t num_elements)
00060 {
00061   /*
00062    * MorphOS cannot handle 0 elements allocations, or rather that always
00063    * returns NULL. So we do that for *all* allocations, thus causing it
00064    * to behave the same on all OSes.
00065    */
00066   if (num_elements == 0) return NULL;
00067 
00068   /* Ensure the size does not overflow. */
00069   CheckAllocationConstraints<T>(num_elements);
00070 
00071   T *t_ptr = (T*)malloc(num_elements * sizeof(T));
00072   if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00073   return t_ptr;
00074 }
00075 
00086 template <typename T>
00087 static inline T *CallocT(size_t num_elements)
00088 {
00089   /*
00090    * MorphOS cannot handle 0 elements allocations, or rather that always
00091    * returns NULL. So we do that for *all* allocations, thus causing it
00092    * to behave the same on all OSes.
00093    */
00094   if (num_elements == 0) return NULL;
00095 
00096   T *t_ptr = (T*)calloc(num_elements, sizeof(T));
00097   if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00098   return t_ptr;
00099 }
00100 
00112 template <typename T>
00113 static inline T *ReallocT(T *t_ptr, size_t num_elements)
00114 {
00115   /*
00116    * MorphOS cannot handle 0 elements allocations, or rather that always
00117    * returns NULL. So we do that for *all* allocations, thus causing it
00118    * to behave the same on all OSes.
00119    */
00120   if (num_elements == 0) {
00121     free(t_ptr);
00122     return NULL;
00123   }
00124 
00125   /* Ensure the size does not overflow. */
00126   CheckAllocationConstraints<T>(num_elements);
00127 
00128   t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
00129   if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
00130   return t_ptr;
00131 }
00132 
00134 #define AllocaM(T, num_elements) \
00135   (CheckAllocationConstraints<T>(num_elements), \
00136   (T*)alloca((num_elements) * sizeof(T)))
00137 
00138 #endif /* ALLOC_FUNC_HPP */