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
00021 void NORETURN MallocError(size_t size);
00022 void NORETURN ReallocError(size_t size);
00023
00034 template <typename T>
00035 static FORCEINLINE T *MallocT(size_t num_elements)
00036 {
00037
00038
00039
00040
00041
00042 if (num_elements == 0) return NULL;
00043
00044 T *t_ptr = (T*)malloc(num_elements * sizeof(T));
00045 if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00046 return t_ptr;
00047 }
00048
00059 template <typename T>
00060 static FORCEINLINE T *CallocT(size_t num_elements)
00061 {
00062
00063
00064
00065
00066
00067 if (num_elements == 0) return NULL;
00068
00069 T *t_ptr = (T*)calloc(num_elements, sizeof(T));
00070 if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
00071 return t_ptr;
00072 }
00073
00085 template <typename T>
00086 static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
00087 {
00088
00089
00090
00091
00092
00093 if (num_elements == 0) {
00094 free(t_ptr);
00095 return NULL;
00096 }
00097
00098 t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
00099 if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
00100 return t_ptr;
00101 }
00102
00104 #define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
00105
00106 #endif