Support class-specific operator new and delete
Fixes #754.
diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h
index 450a55d..e38a1a3 100644
--- a/include/pybind11/attr.h
+++ b/include/pybind11/attr.h
@@ -174,6 +174,9 @@
/// How large is pybind11::instance<type>?
size_t instance_size = 0;
+ /// The global operator new can be overridden with a class-specific variant
+ void *(*operator_new)(size_t) = ::operator new;
+
/// Function pointer to class_<..>::init_holder
void (*init_holder)(PyObject *, const void *) = nullptr;
diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h
index 8684d7c..fe19075 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -25,6 +25,7 @@
struct type_info {
PyTypeObject *type;
size_t type_size;
+ void *(*operator_new)(size_t);
void (*init_holder)(PyObject *, const void *);
void (*dealloc)(PyObject *);
std::vector<PyObject *(*)(PyObject *, PyTypeObject *)> implicit_conversions;
diff --git a/include/pybind11/class_support.h b/include/pybind11/class_support.h
index 56abbf8..992703f 100644
--- a/include/pybind11/class_support.h
+++ b/include/pybind11/class_support.h
@@ -183,7 +183,7 @@
PyObject *self = type->tp_alloc(type, 0);
auto instance = (instance_essentials<void> *) self;
auto tinfo = get_type_info(type);
- instance->value = ::operator new(tinfo->type_size);
+ instance->value = tinfo->operator_new(tinfo->type_size);
instance->owned = true;
instance->holder_constructed = false;
get_internals().registered_instances.emplace(instance->value, self);
diff --git a/include/pybind11/common.h b/include/pybind11/common.h
index 9da1ae9..fba25c9 100644
--- a/include/pybind11/common.h
+++ b/include/pybind11/common.h
@@ -427,6 +427,13 @@
template <bool B> using bool_constant = std::integral_constant<bool, B>;
template <typename T> struct negation : bool_constant<!T::value> { };
+#ifdef __cpp_lib_void_t
+using std::void_t;
+#else
+template <typename...> struct void_t_impl { using type = void; };
+template <typename... Ts> using void_t = typename void_t_impl<Ts...>::type;
+#endif
+
/// Compile-time all/any/none of that check the boolean value of all template types
#ifdef __cpp_fold_expressions
template <class... Ts> using all_of = bool_constant<(Ts::value && ...)>;
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index 0c43850..42a19ac 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -803,6 +803,7 @@
auto *tinfo = new detail::type_info();
tinfo->type = (PyTypeObject *) m_ptr;
tinfo->type_size = rec.type_size;
+ tinfo->operator_new = rec.operator_new;
tinfo->init_holder = rec.init_holder;
tinfo->dealloc = rec.dealloc;
@@ -860,6 +861,18 @@
}
};
+/// Set the pointer to operator new if it exists. The cast is needed because it can be overloaded.
+template <typename T, typename = void_t<decltype(static_cast<void *(*)(size_t)>(T::operator new))>>
+void set_operator_new(type_record *r) { r->operator_new = &T::operator new; }
+
+template <typename> void set_operator_new(...) { }
+
+/// Call class-specific delete if it exists or global otherwise. Can also be an overload set.
+template <typename T, typename = void_t<decltype(static_cast<void (*)(void *)>(T::operator delete))>>
+void call_operator_delete(T *p) { T::operator delete(p); }
+
+inline void call_operator_delete(void *p) { ::operator delete(p); }
+
NAMESPACE_END(detail)
template <typename type_, typename... options>
@@ -905,6 +918,8 @@
record.dealloc = dealloc;
record.default_holder = std::is_same<holder_type, std::unique_ptr<type>>::value;
+ set_operator_new<type>(&record);
+
/* Register base classes specified via template arguments to class_, if any */
bool unused[] = { (add_base<options>(record), false)..., false };
(void) unused;
@@ -1125,7 +1140,7 @@
if (inst->holder_constructed)
inst->holder.~holder_type();
else if (inst->owned)
- ::operator delete(inst->value);
+ detail::call_operator_delete(inst->value);
}
static detail::function_record *get_function_record(handle h) {
diff --git a/tests/test_eigen.cpp b/tests/test_eigen.cpp
index eeaceac..f2ec8fd 100644
--- a/tests/test_eigen.cpp
+++ b/tests/test_eigen.cpp
@@ -60,6 +60,15 @@
return ret;
}
+struct CustomOperatorNew {
+ CustomOperatorNew() = default;
+
+ Eigen::Matrix4d a = Eigen::Matrix4d::Zero();
+ Eigen::Matrix4d b = Eigen::Matrix4d::Identity();
+
+ EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
+};
+
test_initializer eigen([](py::module &m) {
typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
@@ -277,4 +286,9 @@
// requiring a copy!) because the stride value can be safely ignored on a size-1 dimension.
m.def("iss738_f1", &adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>, py::arg().noconvert());
m.def("iss738_f2", &adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>, py::arg().noconvert());
+
+ py::class_<CustomOperatorNew>(m, "CustomOperatorNew")
+ .def(py::init<>())
+ .def_readonly("a", &CustomOperatorNew::a)
+ .def_readonly("b", &CustomOperatorNew::b);
});
diff --git a/tests/test_eigen.py b/tests/test_eigen.py
index f14d622..df08edf 100644
--- a/tests/test_eigen.py
+++ b/tests/test_eigen.py
@@ -614,3 +614,13 @@
assert np.all(iss738_f2(np.array([[1., 2, 3]])) == np.array([[1., 102, 203]]))
assert np.all(iss738_f2(np.array([[1.], [2], [3]])) == np.array([[1.], [12], [23]]))
+
+
+def test_custom_operator_new():
+ """Using Eigen types as member variables requires a class-specific
+ operator new with proper alignment"""
+ from pybind11_tests import CustomOperatorNew
+
+ o = CustomOperatorNew()
+ np.testing.assert_allclose(o.a, 0.0)
+ np.testing.assert_allclose(o.b.diagonal(), 1.0)