memory.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2019, CNRS
3  * Copyright 2018-2019, INRIA
4  */
5 
6 #ifndef __eigenpy_memory_hpp__
7 #define __eigenpy_memory_hpp__
8 
9 #include "eigenpy/fwd.hpp"
10 
22 #define EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(...) \
23 namespace boost { namespace python { namespace objects { \
24  template<> \
25  struct instance< value_holder<__VA_ARGS__> > \
26  { \
27  typedef value_holder<__VA_ARGS__> Data; \
28  PyObject_VAR_HEAD \
29  PyObject* dict; \
30  PyObject* weakrefs; \
31  instance_holder* objects; \
32  \
33  typedef type_with_alignment< \
34  ::boost::alignment_of<Data>::value \
35  >::type align_t; \
36  \
37  union \
38  { \
39  align_t align; \
40  char bytes[sizeof(Data) + 16]; \
41  } storage; \
42  }; \
43  \
44  template<class Derived> \
45  struct make_instance_impl<__VA_ARGS__, value_holder<__VA_ARGS__>, Derived> \
46  { \
47  typedef __VA_ARGS__ T; \
48  typedef value_holder<__VA_ARGS__> Holder; \
49  typedef objects::instance<Holder> instance_t; \
50  \
51  template <class Arg> \
52  static inline PyObject* execute(Arg & x) \
53  { \
54  BOOST_MPL_ASSERT((mpl::or_<is_class<T>, is_union<T> >)); \
55  \
56  PyTypeObject* type = Derived::get_class_object(x); \
57  \
58  if (type == 0) \
59  return python::detail::none(); \
60  \
61  PyObject* raw_result = type->tp_alloc(type, objects::additional_instance_size<Holder>::value); \
62  if (raw_result != 0) \
63  { \
64  python::detail::decref_guard protect(raw_result); \
65  instance_t* instance = (instance_t*)(void*)raw_result; \
66  Holder* holder = Derived::construct(&instance->storage, (PyObject*)instance, x); \
67  holder->install(raw_result); \
68  \
69  Py_ssize_t holder_offset = reinterpret_cast<Py_ssize_t>(holder) \
70  - reinterpret_cast<Py_ssize_t>(&instance->storage) \
71  + static_cast<Py_ssize_t>(offsetof(instance_t, storage)); \
72  Py_SIZE(instance) = holder_offset; \
73  \
74  protect.cancel(); \
75  } \
76  return raw_result; \
77  } \
78  }; \
79  \
80  template<> \
81  struct make_instance<__VA_ARGS__, value_holder<__VA_ARGS__> > \
82  : make_instance_impl<__VA_ARGS__, value_holder<__VA_ARGS__>, make_instance<__VA_ARGS__,value_holder<__VA_ARGS__> > > \
83  { \
84  template <class U> \
85  static inline PyTypeObject* get_class_object(U &) \
86  { \
87  return converter::registered<__VA_ARGS__>::converters.get_class_object(); \
88  } \
89  \
90  static inline value_holder<__VA_ARGS__>* construct(void* storage, PyObject* instance, reference_wrapper<__VA_ARGS__ const> x) \
91  { \
92  void* aligned_storage = reinterpret_cast<void*>((reinterpret_cast<size_t>(storage) & ~(size_t(15))) + 16); \
93  value_holder<__VA_ARGS__>* new_holder = new (aligned_storage) value_holder<__VA_ARGS__>(instance, x); \
94  return new_holder; \
95  } \
96  }; \
97  }}}
98 
99 #endif // __eigenpy_memory_hpp__


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59