HAMR
The Heterogeneous Accelerator Memory Resource
hamr_malloc_allocator.h
1 #ifndef hamr_malloc_allocator_h
2 #define hamr_malloc_allocator_h
3 
4 #include "hamr_config.h"
5 
6 #include <memory>
7 #include <type_traits>
8 
9 namespace hamr
10 {
11 /// a deleter for arrays allocated with malloc
12 template <typename T, typename E = void>
13 class malloc_deleter {};
14 
15 /// a deleter for arrays allocated with malloc, specialized for objects
16 template <typename T>
17 class HAMR_EXPORT malloc_deleter<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
18 {
19 public:
20  /** constructs the deleter
21  * @param[in] ptr the pointer to the array to delete
22  * @param[in] n the number of elements in the array
23  */
24  malloc_deleter(T *ptr, size_t n);
25 
26  /** deletes the array
27  * @param[in] ptr the pointer to the array to delete. must be the same as
28  * that passed during construction.
29  */
30  void operator()(T *ptr);
31 
32 private:
33  T *m_ptr;
34  size_t m_elem;
35 };
36 
37 
38 
39 
40 
41 
42 /// a deleter for arrays allocated with malloc, specialized for numbers
43 template <typename T>
44 class HAMR_EXPORT malloc_deleter<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
45 {
46 public:
47  /** constructs the deleter
48  * @param[in] ptr the pointer to the array to delete
49  * @param[in] n the number of elements in the array
50  */
51  malloc_deleter(T *ptr, size_t n);
52 
53  /** deletes the array
54  * @param[in] ptr the pointer to the array to delete. must be the same as
55  * that passed during construction.
56  */
57  void operator()(T *ptr);
58 
59 private:
60  T *m_ptr;
61  size_t m_elem;
62 };
63 
64 
65 
66 
67 
68 
69 /// a class for allocating arrays with malloc
70 template <typename T, typename E = void>
71 struct malloc_allocator {};
72 
73 /// a class for allocating arrays with malloc, specialized for objects
74 template <typename T>
75 struct HAMR_EXPORT malloc_allocator<T, typename std::enable_if<!std::is_arithmetic<T>::value>::type>
76 {
77  /** allocate an array of n elements.
78  * @param[in] n the number of elements to allocate
79  * @returns a shared pointer to the array that holds a deleter for the memory
80  */
81  static std::shared_ptr<T> allocate(size_t n) HAMR_EXPORT;
82 
83  /** allocate an array of n elements.
84  * @param[in] n the number of elements to allocate
85  * @param[in] val a value to initialize the elements to
86  * @returns a shared pointer to the array that holds a deleter for the memory
87  */
88 
89  static std::shared_ptr<T> allocate(size_t n, const T &val) HAMR_EXPORT;
90 
91  /** allocate an array of n elements.
92  * @param[in] n the number of elements to allocate
93  * @param[in] vals an array of n elements to initialize the elements with
94  * @returns a shared pointer to the array that holds a deleter for the memory
95  */
96  template <typename U>
97  static std::shared_ptr<T> allocate(size_t n, const U *vals) HAMR_EXPORT;
98 };
99 
100 
101 
102 
103 
104 /// a class for allocating arrays with malloc, specialized for numbers
105 template <typename T>
106 struct HAMR_EXPORT malloc_allocator<T, typename std::enable_if<std::is_arithmetic<T>::value>::type>
107 {
108  /** allocate an array of n elements.
109  * @param[in] n the number of elements to allocate
110  * @returns a shared pointer to the array that holds a deleter for the memory
111  */
112  static std::shared_ptr<T> allocate(size_t n) HAMR_EXPORT;
113 
114  /** allocate an array of n elements.
115  * @param[in] n the number of elements to allocate
116  * @param[in] val a value to initialize the elements to
117  * @returns a shared pointer to the array that holds a deleter for the memory
118  */
119  static std::shared_ptr<T> allocate(size_t n, const T &val) HAMR_EXPORT;
120 
121  /** allocate an array of n elements.
122  * @param[in] n the number of elements to allocate
123  * @param[in] vals an array of n elements to initialize the elements with
124  * @returns a shared pointer to the array that holds a deleter for the memory
125  */
126  template <typename U>
127  static std::shared_ptr<T> allocate(size_t n, const U *vals) HAMR_EXPORT;
128 };
129 
130 }
131 
132 #if !defined(HAMR_SEPARATE_IMPL)
133 #include "hamr_malloc_allocator_impl.h"
134 #endif
135 
136 #endif
hamr::malloc_allocator
a class for allocating arrays with malloc
Definition: hamr_malloc_allocator.h:71
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::malloc_deleter
a deleter for arrays allocated with malloc
Definition: hamr_malloc_allocator.h:13