alloc_func.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef ALLOC_FUNC_HPP
00013 #define ALLOC_FUNC_HPP
00014
00015
00016
00017
00018
00019
00020
00021
00022 void NORETURN MallocError(size_t size);
00023 void NORETURN ReallocError(size_t size);
00024
00035 template <typename T>
00036 static FORCEINLINE T *MallocT(size_t num_elements)
00037 {
00038
00039
00040
00041
00042
00043 if (num_elements == 0) return NULL;
00044
00045 T *t_ptr = (T*)malloc(num_elements * sizeof(T));
00046 if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00047 return t_ptr;
00048 }
00049
00060 template <typename T>
00061 static FORCEINLINE T *CallocT(size_t num_elements)
00062 {
00063
00064
00065
00066
00067
00068 if (num_elements == 0) return NULL;
00069
00070 T *t_ptr = (T*)calloc(num_elements, sizeof(T));
00071 if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00072 return t_ptr;
00073 }
00074
00086 template <typename T>
00087 static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
00088 {
00089
00090
00091
00092
00093
00094 if (num_elements == 0) {
00095 free(t_ptr);
00096 return NULL;
00097 }
00098
00099 t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
00100 if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
00101 return t_ptr;
00102 }
00103
00105 #define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
00106
00107 #endif