Skip to content

Support C++17 aligned new statement #1582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ image:
- Visual Studio 2017
- Visual Studio 2015
test: off
skip_branch_with_pr: true
build:
parallel: true
platform:
Expand Down
5 changes: 4 additions & 1 deletion include/pybind11/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,14 @@ struct type_record {
/// How large is the underlying C++ type?
size_t type_size = 0;

/// What is the alignment of the underlying C++ type?
size_t type_align = 0;

/// How large is the type's holder?
size_t holder_size = 0;

/// The global operator new can be overridden with a class-specific variant
void *(*operator_new)(size_t) = ::operator new;
void *(*operator_new)(size_t) = nullptr;

/// Function pointer to class_<..>::init_instance
void (*init_instance)(instance *, const void *) = nullptr;
Expand Down
12 changes: 11 additions & 1 deletion include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,17 @@ class type_caster_generic {
// Lazy allocation for unallocated values:
if (vptr == nullptr) {
auto *type = v_h.type ? v_h.type : typeinfo;
vptr = type->operator_new(type->type_size);
if (type->operator_new) {
vptr = type->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;
}
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct internals {
struct type_info {
PyTypeObject *type;
const std::type_info *cpptype;
size_t type_size, holder_size_in_ptrs;
size_t type_size, type_align, holder_size_in_ptrs;
void *(*operator_new)(size_t);
void (*init_instance)(instance *, const void *);
void (*dealloc)(value_and_holder &v_h);
Expand All @@ -138,7 +138,7 @@ struct type_info {
};

/// Tracks the `internals` and `type_info` ABI version independent of the main library version
#define PYBIND11_INTERNALS_VERSION 2
#define PYBIND11_INTERNALS_VERSION 3

#if defined(WITH_THREAD)
# define PYBIND11_INTERNALS_KIND ""
Expand Down
23 changes: 19 additions & 4 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ class generic_type : public object {
tinfo->type = (PyTypeObject *) m_ptr;
tinfo->cpptype = rec.type;
tinfo->type_size = rec.type_size;
tinfo->type_align = rec.type_align;
tinfo->operator_new = rec.operator_new;
tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
tinfo->init_instance = rec.init_instance;
Expand Down Expand Up @@ -987,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)

Expand Down Expand Up @@ -1054,6 +1065,7 @@ class class_ : public detail::generic_type {
record.name = name;
record.type = &typeid(type);
record.type_size = sizeof(conditional_t<has_alias, type_alias, type>);
record.type_align = alignof(conditional_t<has_alias, type_alias, type>&);
record.holder_size = sizeof(holder_type);
record.init_instance = init_instance;
record.dealloc = dealloc;
Expand Down Expand Up @@ -1329,7 +1341,10 @@ class class_ : public detail::generic_type {
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;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "local_bindings.h"
#include <pybind11/stl.h>

#if defined(_MSC_VER)
# pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier
#endif

// test_brace_initialization
struct NoBraceInitialization {
NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {}
Expand Down Expand Up @@ -354,6 +358,15 @@ TEST_SUBMODULE(class_, m) {
[](StringWrapper) -> NotRegistered { return {}; });
py::class_<StringWrapper>(m, "StringWrapper").def(py::init<std::string>());
py::implicitly_convertible<std::string, StringWrapper>();

#if defined(PYBIND11_CPP17)
struct alignas(1024) Aligned {
std::uintptr_t ptr() const { return (uintptr_t) this; }
};
py::class_<Aligned>(m, "Aligned")
.def(py::init<>())
.def("ptr", &Aligned::ptr);
#endif
}

template <int N> class BreaksBase { public: virtual ~BreaksBase() = default; };
Expand Down
6 changes: 6 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,9 @@ def test_error_after_conversions():
m.test_error_after_conversions("hello")
assert str(exc_info.value).startswith(
"Unable to convert function return value to a Python type!")


def test_aligned():
if hasattr(m, "Aligned"):
p = m.Aligned().ptr()
assert p % 1024 == 0