1 #ifndef hamr_malloc_allocator_h
2 #define hamr_malloc_allocator_h
4 #include "hamr_config.h"
16 template <
typename T,
typename E =
void>
21 class HAMR_EXPORT
malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
34 void operator()(T *ptr);
46 #if defined(HAMR_VERBOSE)
49 std::cerr <<
"created malloc_deleter for array of " << n
50 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
51 <<
" at " << m_ptr << std::endl;
59 malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
64 #if defined(HAMR_VERBOSE)
67 std::cerr <<
"malloc_deleter deleting array of " << m_elem
68 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
69 <<
" at " << m_ptr << std::endl;
74 for (
size_t i = 0; i < m_elem; ++i)
87 class HAMR_EXPORT
malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
100 void operator()(T *ptr);
108 template <
typename T>
112 #if defined(HAMR_VERBOSE)
115 std::cerr <<
"created malloc_deleter for array of " << n
116 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
117 <<
" at " << m_ptr << std::endl;
123 template <
typename T>
125 malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
128 assert(ptr == m_ptr);
130 #if defined(HAMR_VERBOSE)
133 std::cerr <<
"malloc_deleter deleting array of " << m_elem
134 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
135 <<
" at " << m_ptr << std::endl;
148 template <
typename T,
typename E =
void>
152 template <
typename T>
153 struct HAMR_EXPORT
malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
159 static std::shared_ptr<T> allocate(
size_t n);
167 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
174 template <
typename U>
175 static std::shared_ptr<T> allocate(
size_t n,
const U *vals);
179 template <
typename T>
185 T *ptr = (T*)
malloc(n*
sizeof(T));
188 for (
size_t i = 0; i < n; ++i)
191 #if defined(HAMR_VERBOSE)
194 std::cerr <<
"malloc_allocator allocating array of " << n
195 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
196 <<
" at " << ptr << std::endl;
201 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
205 template <
typename T>
207 malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
208 ::allocate(
size_t n,
const T &val)
211 T *ptr = (T*)
malloc(n*
sizeof(T));
214 for (
size_t i = 0; i < n; ++i)
215 new (&ptr[i]) T(val);
217 #if defined(HAMR_VERBOSE)
220 std::cerr <<
"malloc_allocator allocating array of " << n
221 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
222 <<
" at " << ptr <<
" initialized to " << val << std::endl;
227 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
231 template <
typename T>
232 template <
typename U>
234 malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
235 ::allocate(
size_t n,
const U *vals)
238 T *ptr = (T*)
malloc(n*
sizeof(T));
241 for (
size_t i = 0; i < n; ++i)
242 new (&ptr[i]) T(vals[i]);
244 #if defined(HAMR_VERBOSE)
247 std::cerr <<
"malloc_allocator allocating array of " << n
248 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
249 <<
" initialized from array of objects of type "
250 <<
typeid(U).name() <<
sizeof(U) <<
" at " << vals
256 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
263 template <
typename T>
264 struct HAMR_EXPORT
malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
270 static std::shared_ptr<T> allocate(
size_t n);
277 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
284 template <
typename U>
285 static std::shared_ptr<T> allocate(
size_t n,
const U *vals);
289 template <
typename T>
294 size_t n_bytes = n*
sizeof(T);
297 T *ptr = (T*)
malloc(n_bytes);
300 #if defined(HAMR_INIT_ALLOC)
301 memset(ptr, 0, n_bytes);
304 #if defined(HAMR_VERBOSE)
307 std::cerr <<
"malloc_allocator allocating array of " << n
308 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
309 <<
" at " << ptr << std::endl;
314 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
318 template <
typename T>
320 malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
321 ::allocate(
size_t n,
const T &val)
323 size_t n_bytes = n*
sizeof(T);
326 T *ptr = (T*)
malloc(n_bytes);
329 for (
size_t i = 0; i < n; ++i)
332 #if defined(HAMR_VERBOSE)
335 std::cerr <<
"malloc_allocator allocating array of " << n
336 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
337 <<
" at " << ptr <<
" initialized to " << val << std::endl;
342 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
346 template <
typename T>
347 template <
typename U>
349 malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
350 ::allocate(
size_t n,
const U *vals)
352 size_t n_bytes = n*
sizeof(T);
355 T *ptr = (T*)
malloc(n_bytes);
358 for (
size_t i = 0; i < n; ++i)
361 #if defined(HAMR_VERBOSE)
364 std::cerr <<
"malloc_allocator allocating array of " << n
365 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
366 <<
" at " << ptr <<
" initialized from an array of numbers of type "
367 <<
typeid(U).name() <<
sizeof(U) <<
" at " << vals << std::endl;
372 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));