Go to the documentation of this file.
10 #ifndef ALLOC_FUNC_HPP
11 #define ALLOC_FUNC_HPP
31 if (num_elements > SIZE_MAX / element_size)
MallocError(SIZE_MAX);
57 static inline T *
MallocT(
size_t num_elements)
64 if (num_elements == 0)
return nullptr;
67 CheckAllocationConstraints<T>(num_elements);
69 T *t_ptr = (T*)malloc(num_elements *
sizeof(T));
70 if (t_ptr ==
nullptr)
MallocError(num_elements *
sizeof(T));
85 static inline T *
CallocT(
size_t num_elements)
92 if (num_elements == 0)
return nullptr;
94 T *t_ptr = (T*)calloc(num_elements,
sizeof(T));
95 if (t_ptr ==
nullptr)
MallocError(num_elements *
sizeof(T));
110 template <
typename T>
111 static inline T *
ReallocT(T *t_ptr,
size_t num_elements)
118 if (num_elements == 0) {
124 CheckAllocationConstraints<T>(num_elements);
126 t_ptr = (T*)realloc(
static_cast<void *
>(t_ptr), num_elements *
sizeof(T));
127 if (t_ptr ==
nullptr)
ReallocError(num_elements *
sizeof(T));
132 #define AllocaM(T, num_elements) \
133 (CheckAllocationConstraints<T>(num_elements), \
134 (T*)alloca((num_elements) * sizeof(T)))
static void CheckAllocationConstraints(size_t element_size, size_t num_elements)
Checks whether allocating memory would overflow size_t.
void NORETURN ReallocError(size_t size)
Function to exit with an error message after realloc() have failed.
static T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
void NORETURN MallocError(size_t size)
Function to exit with an error message after malloc() or calloc() have failed.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
static T * CallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.