30 #ifndef SOLIDUTILS_INCLUDE_ALLOC_HPP 31 #define SOLIDUTILS_INCLUDE_ALLOC_HPP 49 size_t const numChunks,
50 size_t const chunkSize) noexcept :
52 m_msg(std::string(
"Failed to allocate ") + \
53 std::to_string(numChunks*chunkSize) + \
54 std::string(
" bytes in ") + std::to_string(numChunks) + \
55 std::string(
" chunks of size ") + \
56 std::to_string(chunkSize) + std::string(
"."))
61 const char * what()
const noexcept
override 94 constexpr
size_t const chunkSize =
sizeof(T);
96 size_t const numBytes = chunkSize*num;
99 T *
const data =
reinterpret_cast<T*
>(malloc(numBytes));
101 if (data ==
nullptr) {
125 T
const val = static_cast<T>(0))
127 T *
const data = uninitialized<T>(num);
128 for (
size_t i = 0; i < num; ++i) {
151 T *
const data = uninitialized<T>(num);
152 for (
size_t i = 0; i < num; ++i) {
174 constexpr
size_t const chunkSize =
sizeof(T);
175 T *
const newPtr =
reinterpret_cast<T*
>( \
176 std::realloc(*ptr, num*chunkSize));
177 if (newPtr ==
nullptr) {
193 T *
const ptr) noexcept
195 if (ptr !=
nullptr) {
static void resize(T **const ptr, size_t const num)
Resize an allocation.
Definition: Alloc.hpp:170
The Alloc class provides a set of static functions for allocating templated memory via malloc() and a...
Definition: Alloc.hpp:75
static T * initialized(size_t const num, T const val=static_cast< T >(0))
Allocate and initialize a block of memory to a constant value.
Definition: Alloc.hpp:123
static T * duplicate(T const *const ptr, size_t const num)
Allocate and and fill a block of memory from another block.
Definition: Alloc.hpp:147
static T * uninitialized(size_t const num)
Allocate a block of uninitialized memory that can be free'd with a call to Alloc::free(). If the amount of memory requested is 0, then nullptr will be returned.
Definition: Alloc.hpp:91
static void free(T *const ptr) noexcept
Free a block of memory allocated with this class.
Definition: Alloc.hpp:192