SolidUtils
Alloc.hpp
Go to the documentation of this file.
1 
30 #ifndef SOLIDUTILS_INCLUDE_ALLOC_HPP
31 #define SOLIDUTILS_INCLUDE_ALLOC_HPP
32 
33 
34 
35 #include <cstring>
36 #include <new>
37 #include <string>
38 
39 
40 namespace sl
41 {
42 
43 
45  public std::bad_alloc
46 {
47  public:
49  size_t const numChunks,
50  size_t const chunkSize) noexcept :
51  std::bad_alloc(),
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("."))
57  {
58  // do nothing
59  }
60 
61  const char * what() const noexcept override
62  {
63  return m_msg.c_str();
64  }
65 
66  private:
67  std::string m_msg;
68 };
69 
70 
75 class Alloc
76 {
77  public:
90  template<typename T>
91  static T * uninitialized(
92  size_t const num)
93  {
94  constexpr size_t const chunkSize = sizeof(T);
95 
96  size_t const numBytes = chunkSize*num;
97 
98  if (numBytes > 0) {
99  T * const data = reinterpret_cast<T*>(malloc(numBytes));
100 
101  if (data == nullptr) {
102  throw NotEnoughMemoryException(num, chunkSize);
103  }
104  return data;
105  } else {
106  return nullptr;
107  }
108  }
109 
110 
122  template<typename T>
123  static T * initialized(
124  size_t const num,
125  T const val = static_cast<T>(0))
126  {
127  T * const data = uninitialized<T>(num);
128  for (size_t i = 0; i < num; ++i) {
129  data[i] = val;
130  }
131 
132  return data;
133  }
134 
146  template<typename T>
147  static T * duplicate(
148  T const * const ptr,
149  size_t const num)
150  {
151  T * const data = uninitialized<T>(num);
152  for (size_t i = 0; i < num; ++i) {
153  data[i] = ptr[i];
154  }
155 
156  return data;
157  }
158 
159 
169  template<typename T>
170  static void resize(
171  T ** const ptr,
172  size_t const num)
173  {
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) {
178  throw NotEnoughMemoryException(num, chunkSize);
179  }
180 
181  *ptr = newPtr;
182  }
183 
184 
191  template<typename T>
192  static void free(
193  T * const ptr) noexcept
194  {
195  if (ptr != nullptr) {
196  std::free(ptr);
197  }
198  }
199 };
200 
201 }
202 
203 
204 #endif
static void resize(T **const ptr, size_t const num)
Resize an allocation.
Definition: Alloc.hpp:170
Definition: Alloc.hpp:40
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&#39;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
Definition: Alloc.hpp:44