HAMR
The Heterogeneous Accelerator Memory Resource
hamr_python_deleter.h
1 #ifndef hamr_python_deleter_h
2 #define hamr_python_deleter_h
3 
4 #include "hamr_config.h"
5 #include <Python.h>
6 
7 namespace hamr
8 {
9 
10 /// a deleter for memory managed from within Python
11 /** This class manages an array allocated by a Python code. In the functor's
12  * constructor a refrence to a user provdied Python object is stolen. When the
13  * functor is invoked, a reference to this Python object is released. It is up
14  * to the Python object to free the memory. One may use a PyCapsule to
15  * implement custom delete methods if they are needed.
16  */
17 template <typename T>
18 class HAMR_EXPORT python_deleter
19 {
20 public:
21  /** constructs the deleter. A reference to obj is stolen by this constructor.
22  * @param[in] ptr a pointer to shared data
23  * @param[in] n_elem the number of elements of type T shared
24  * @param[in] obj a PyObject who's reference count will be decremented when
25  * the data shared from Python is no longer needed.
26  */
27  python_deleter(T *ptr, size_t n_elem, PyObject *obj);
28 
29  /** deletes the array
30  * @param[in] ptr the pointer to the array to delete. must be the same as
31  * that passed during construction.
32  */
33  void operator()(T *ptr);
34 
35 private:
36  T *m_ptr;
37  size_t m_elem;
38  PyObject *m_object;
39 };
40 
41 }
42 
43 #if !defined(HAMR_SEPARATE_IMPL)
44 #include "hamr_python_deleter_impl.h"
45 #endif
46 
47 #endif
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:13
hamr::python_deleter
a deleter for memory managed from within Python
Definition: hamr_python_deleter.h:18