10 #ifndef ALLOC_FUNC_HPP
11 #define ALLOC_FUNC_HPP
31 if (num_elements > SIZE_MAX / element_size)
MallocError(SIZE_MAX);
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));
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>
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));
T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
void ReallocError(size_t size)
Function to exit with an error message after realloc() have failed.
T * CallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type.
T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
void MallocError(size_t size)
Function to exit with an error message after malloc() or calloc() have failed.
void CheckAllocationConstraints(size_t element_size, size_t num_elements)
Checks whether allocating memory would overflow size_t.
void free(const void *ptr)
Version of the standard free that accepts const pointers.