HAMR
The Heterogeneous Accelerator Memory Resource
hamr_new_allocator.h
1 #ifndef hamr_new_allocator_h
2 #define hamr_new_allocator_h
3 
4 #include <iostream>
5 #include <type_traits>
6 #include <memory>
7 #include <typeinfo>
8 #include <cassert>
9 #include <cstring>
10 
11 namespace hamr
12 {
13 
14 /// a deleter for arrays allocated with new
15 template <typename T>
16 class HAMR_EXPORT new_deleter
17 {
18 public:
19  /** constructs the deleter
20  * @param[in] ptr the pointer to the array to delete
21  * @param[in] the number of elements in the array
22  */
23  new_deleter(T *ptr, size_t n);
24 
25  /** deletes the array
26  * @param[in] ptr the pointer to the array to delete. must be the same as
27  * that passed during construction.
28  */
29  void operator()(T *ptr);
30 
31 private:
32  T *m_ptr;
33  size_t m_elem;
34 };
35 
36 // --------------------------------------------------------------------------
37 template <typename T>
38 new_deleter<T>::new_deleter(T *ptr, size_t n) : m_ptr(ptr), m_elem(n)
39 {
40 #if defined(HAMR_VERBOSE)
41  if (hamr::get_verbose())
42  {
43  std::cerr << "created new_deleter for array of " << n
44  << " objects of type " << typeid(T).name() << std::endl;
45  }
46 #endif
47 }
48 
49 // --------------------------------------------------------------------------
50 template <typename T>
52 {
53  assert(ptr == m_ptr);
54 
55 #if defined(HAMR_VERBOSE)
56  if (hamr::get_verbose())
57  {
58  std::cerr << "new_deleter deleting array of " << m_elem
59  << " objects of type " << typeid(T).name() << std::endl;
60  }
61 #endif
62 
63  delete [] ptr;
64 }
65 
66 
67 
68 
69 
70 /// a class for allocating arrays with new
71 template <typename T>
72 struct HAMR_EXPORT new_allocator
73 {
74  /** allocate an array of n elements.
75  * @param[in] n the number of elements to allocate
76  * @returns a shared pointer to the array that holds a deleter for the memory
77  */
78  static std::shared_ptr<T> allocate(size_t n);
79 
80  /** allocate an array of n elements.
81  * @param[in] n the number of elements to allocate
82  * @param[in] val a value to initialize the elements to
83  * @returns a shared pointer to the array that holds a deleter for the memory
84  */
85  static std::shared_ptr<T> allocate(size_t n, const T &val);
86 
87  /** allocate an array of n elements.
88  * @param[in] n the number of elements to allocate
89  * @param[in] vals an array of n values to initialize the elements with
90  * @returns a shared pointer to the array that holds a deleter for the memory
91  */
92  template <typename U>
93  static std::shared_ptr<T> allocate(size_t n, const U *vals);
94 };
95 
96 // --------------------------------------------------------------------------
97 template <typename T>
98 std::shared_ptr<T> new_allocator<T>::allocate(size_t n)
99 {
100 #if defined(HAMR_VERBOSE)
101  if (hamr::get_verbose())
102  {
103  std::cerr << "new_allocator allocating array of " << n
104  << " objects of type " << typeid(T).name() << std::endl;
105  }
106 #endif
107 
108  // allocate
109  T *ptr = new T[n];
110 
111  // package
112  return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
113 }
114 
115 // --------------------------------------------------------------------------
116 template <typename T>
117 std::shared_ptr<T> new_allocator<T>::allocate(size_t n, const T &val)
118 {
119 #if defined(HAMR_VERBOSE)
120  if (hamr::get_verbose())
121  {
122  std::cerr << "new_allocator allocating array of " << n
123  << " objects of type " << typeid(T).name() << " initialized"
124  << std::endl;
125  }
126 #endif
127 
128  // allocate
129  T *ptr = (T*)new unsigned char[n*sizeof(T)];
130 
131  // construct
132  for (size_t i = 0; i < n; ++i)
133  new (&ptr[i]) T(val);
134 
135  // package
136  return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
137 }
138 
139 // --------------------------------------------------------------------------
140 template <typename T>
141 template <typename U>
142 std::shared_ptr<T> new_allocator<T>::allocate(size_t n, const U *vals)
143 {
144 #if defined(HAMR_VERBOSE)
145  if (hamr::get_verbose())
146  {
147  std::cerr << "new_allocator allocating array of " << n
148  << " objects of type " << typeid(T).name() << " initialized"
149  << std::endl;
150  }
151 #endif
152 
153  // allocate
154  T *ptr = (T*)new unsigned char[n*sizeof(T)];
155 
156  // construct
157  for (size_t i = 0; i < n; ++i)
158  new (&ptr[i]) T(vals[i]);
159 
160  // package
161  return std::shared_ptr<T>(ptr, new_deleter<T>(ptr, n));
162 }
163 
164 };
165 
166 #endif
hamr::new_deleter
a deleter for arrays allocated with new
Definition: hamr_new_allocator.h:16
hamr::get_verbose
constexpr HAMR_EXPORT int get_verbose()
returns the value of the HAMR_VERBOSE environment variable
Definition: hamr_env.h:14
hamr::new_deleter::new_deleter
new_deleter(T *ptr, size_t n)
Definition: hamr_new_allocator.h:38
hamr::new_allocator::allocate
static std::shared_ptr< T > allocate(size_t n)
Definition: hamr_new_allocator.h:98
hamr::new_deleter::operator()
void operator()(T *ptr)
Definition: hamr_new_allocator.h:51
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:19
hamr::new_allocator
a class for allocating arrays with new
Definition: hamr_new_allocator.h:72