1 #ifndef hamr_malloc_allocator_h
2 #define hamr_malloc_allocator_h
4 #include "hamr_config.h"
17 template <
typename T,
typename E =
void>
22 class HAMR_EXPORT
malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
35 void operator()(T *ptr);
47 #if defined(HAMR_VERBOSE)
50 std::cerr <<
"created malloc_deleter for array of " << n
51 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
52 <<
" at " << m_ptr << std::endl;
60 malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
65 #if defined(HAMR_VERBOSE)
68 std::cerr <<
"malloc_deleter deleting array of " << m_elem
69 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
70 <<
" at " << m_ptr << std::endl;
75 for (
size_t i = 0; i < m_elem; ++i)
88 class HAMR_EXPORT
malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
101 void operator()(T *ptr);
109 template <
typename T>
113 #if defined(HAMR_VERBOSE)
116 std::cerr <<
"created malloc_deleter for array of " << n
117 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
118 <<
" at " << m_ptr << std::endl;
124 template <
typename T>
126 malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
129 assert(ptr == m_ptr);
131 #if defined(HAMR_VERBOSE)
134 std::cerr <<
"malloc_deleter deleting array of " << m_elem
135 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
136 <<
" at " << m_ptr << std::endl;
149 template <
typename T,
typename E =
void>
153 template <
typename T>
154 struct HAMR_EXPORT
malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
160 static std::shared_ptr<T> allocate(
size_t n);
168 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
175 template <
typename U>
176 static std::shared_ptr<T> allocate(
size_t n,
const U *vals);
180 template <
typename T>
186 T *ptr = (T*)
malloc(n*
sizeof(T));
189 for (
size_t i = 0; i < n; ++i)
192 #if defined(HAMR_VERBOSE)
195 std::cerr <<
"malloc_allocator allocating array of " << n
196 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
197 <<
" at " << ptr << std::endl;
202 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
206 template <
typename T>
208 malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
209 ::allocate(
size_t n,
const T &val)
212 T *ptr = (T*)
malloc(n*
sizeof(T));
215 for (
size_t i = 0; i < n; ++i)
216 new (&ptr[i]) T(val);
218 #if defined(HAMR_VERBOSE)
221 std::cerr <<
"malloc_allocator allocating array of " << n
222 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
223 <<
" at " << ptr <<
" initialized to " << val << std::endl;
228 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
232 template <
typename T>
233 template <
typename U>
235 malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
236 ::allocate(
size_t n,
const U *vals)
239 T *ptr = (T*)
malloc(n*
sizeof(T));
242 for (
size_t i = 0; i < n; ++i)
243 new (&ptr[i]) T(vals[i]);
245 #if defined(HAMR_VERBOSE)
248 std::cerr <<
"malloc_allocator allocating array of " << n
249 <<
" objects of type " <<
typeid(T).name() <<
sizeof(T)
250 <<
" initialized from array of objects of type "
251 <<
typeid(U).name() <<
sizeof(U) <<
" at " << vals
257 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
264 template <
typename T>
265 struct HAMR_EXPORT
malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
271 static std::shared_ptr<T> allocate(
size_t n);
278 static std::shared_ptr<T> allocate(
size_t n,
const T &val);
285 template <
typename U>
286 static std::shared_ptr<T> allocate(
size_t n,
const U *vals);
290 template <
typename T>
295 size_t n_bytes = n*
sizeof(T);
298 T *ptr = (T*)
malloc(n_bytes);
301 #if defined(HAMR_INIT_ALLOC)
302 memset(ptr, 0, n_bytes);
305 #if defined(HAMR_VERBOSE)
308 std::cerr <<
"malloc_allocator allocating array of " << n
309 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
310 <<
" at " << ptr << std::endl;
315 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
319 template <
typename T>
321 malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
322 ::allocate(
size_t n,
const T &val)
324 size_t n_bytes = n*
sizeof(T);
327 T *ptr = (T*)
malloc(n_bytes);
330 for (
size_t i = 0; i < n; ++i)
333 #if defined(HAMR_VERBOSE)
336 std::cerr <<
"malloc_allocator allocating array of " << n
337 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
338 <<
" at " << ptr <<
" initialized to " << val << std::endl;
343 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));
347 template <
typename T>
348 template <
typename U>
350 malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
351 ::allocate(
size_t n,
const U *vals)
353 size_t n_bytes = n*
sizeof(T);
356 T *ptr = (T*)
malloc(n_bytes);
359 for (
size_t i = 0; i < n; ++i)
362 #if defined(HAMR_VERBOSE)
365 std::cerr <<
"malloc_allocator allocating array of " << n
366 <<
" numbers of type " <<
typeid(T).name() <<
sizeof(T)
367 <<
" at " << ptr <<
" initialized from an array of numbers of type "
368 <<
typeid(U).name() <<
sizeof(U) <<
" at " << vals << std::endl;
373 return std::shared_ptr<T>(ptr, malloc_deleter<T>(ptr, n));