only use over-aligned new/delete if needed

This commit is contained in:
Wenzel Jakob 2018-11-09 12:32:12 +01:00
parent 5c11b1c871
commit 1423623205
3 changed files with 24 additions and 9 deletions

View File

@ -3,6 +3,7 @@ image:
- Visual Studio 2017
- Visual Studio 2015
test: off
skip_branch_with_pr: true
build:
parallel: true
platform:

View File

@ -574,12 +574,13 @@ public:
if (type->operator_new) {
vptr = type->operator_new(type->type_size);
} else {
#if !defined(PYBIND11_CPP17)
vptr = ::operator new(type->type_size);
#else
#if defined(PYBIND11_CPP17)
if (type->type_align > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
vptr = ::operator new(type->type_size,
(std::align_val_t) type->type_align);
else
#endif
vptr = ::operator new(type->type_size);
}
}
value = vptr;

View File

@ -988,11 +988,21 @@ template <typename T> struct has_operator_delete_size<T, void_t<decltype(static_
: std::true_type { };
/// Call class-specific delete if it exists or global otherwise. Can also be an overload set.
template <typename T, enable_if_t<has_operator_delete<T>::value, int> = 0>
void call_operator_delete(T *p, size_t) { T::operator delete(p); }
void call_operator_delete(T *p, size_t, size_t) { T::operator delete(p); }
template <typename T, enable_if_t<!has_operator_delete<T>::value && has_operator_delete_size<T>::value, int> = 0>
void call_operator_delete(T *p, size_t s) { T::operator delete(p, s); }
void call_operator_delete(T *p, size_t s, size_t) { T::operator delete(p, s); }
inline void call_operator_delete(void *p, size_t) { ::operator delete(p); }
inline void call_operator_delete(void *p, size_t s, size_t a) {
(void)s; (void)a;
#if defined(PYBIND11_CPP17)
if (a > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
::operator delete(p, s, std::align_val_t(a));
else
::operator delete(p, s);
#else
::operator delete(p);
#endif
}
NAMESPACE_END(detail)
@ -1331,7 +1341,10 @@ private:
v_h.set_holder_constructed(false);
}
else {
detail::call_operator_delete(v_h.value_ptr<type>(), v_h.type->type_size);
detail::call_operator_delete(v_h.value_ptr<type>(),
v_h.type->type_size,
v_h.type->type_align
);
}
v_h.value_ptr() = nullptr;
}