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 "hamr_gil_state.h"
6 
7 #include <Python.h>
8 #include <iostream>
9 
10 namespace hamr
11 {
12 
13 /// a deleter for memory managed from within Python
14 /** This class manages an array allocated by a Python code. In the functor's
15  * constructor a refrence to a user provdied Python object is stolen. When the
16  * functor is invoked, a reference to this Python object is released. It is up
17  * to the Python object to free the memory. One may use a PyCapsule to
18  * implement custom delete methods if they are needed.
19  */
20 template <typename T>
21 class HAMR_EXPORT python_deleter
22 {
23 public:
24  /** constructs the deleter. A reference to obj is stolen by this constructor.
25  * @param[in] ptr a pointer to shared data
26  * @param[in] n_elem the number of elements of type T shared
27  * @param[in] obj a PyObject who's reference count will be decremented when
28  * the data shared from Python is no longer needed.
29  */
30  python_deleter(T *ptr, size_t n_elem, PyObject *obj);
31 
32  /** deletes the array
33  * @param[in] ptr the pointer to the array to delete. must be the same as
34  * that passed during construction.
35  */
36  void operator()(T *ptr);
37 
38 private:
39  T *m_ptr;
40  size_t m_elem;
41  PyObject *m_object;
42 };
43 
44 // --------------------------------------------------------------------------
45 template <typename T>
46 python_deleter<T>::python_deleter(T *ptr, size_t n, PyObject *obj)
47  : m_ptr(ptr), m_elem(n), m_object(obj)
48 {
49 #if defined(HAMR_VERBOSE)
50  if (hamr::get_verbose())
51  {
52  std::cerr << "created python_deleter for array of " << n
53  << " objects of type " << typeid(T).name() << sizeof(T)
54  << " holding a reference to " << m_object << std::endl;
55  }
56 #endif
57  hamr::gil_state gil;
58  Py_INCREF(obj);
59 }
60 
61 // --------------------------------------------------------------------------
62 template <typename T>
64 {
65  (void)ptr;
66  assert(ptr == m_ptr);
67 #if defined(HAMR_VERBOSE)
68  if (hamr::get_verbose())
69  {
70  std::cerr << "python_deleter deleting array of " << m_elem
71  << " objects of type " << typeid(T).name() << sizeof(T)
72  << " release reference to " << m_object << std::endl;
73  }
74 #endif
75  hamr::gil_state gil;
76  Py_DECREF(m_object);
77 }
78 
79 }
80 #endif
hamr::python_deleter::operator()
void operator()(T *ptr)
Definition: hamr_python_deleter.h:63
hamr::gil_state
A RAII helper for managing the Python GIL.
Definition: hamr_gil_state.h:13
hamr::get_verbose
constexpr HAMR_EXPORT int get_verbose()
returns the value of the HAMR_VERBOSE environment variable
Definition: hamr_env.h:14
hamr::python_deleter::python_deleter
python_deleter(T *ptr, size_t n_elem, PyObject *obj)
Definition: hamr_python_deleter.h:46
hamr
heterogeneous accelerator memory resource
Definition: hamr_buffer.h:40
hamr::python_deleter
a deleter for memory managed from within Python
Definition: hamr_python_deleter.h:21