2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Alignment fixes

This commit is contained in:
Daniel Laügt
2022-08-29 10:37:00 +02:00
committed by Stefan Seefeld
parent b3a28d7033
commit b988d70207
2 changed files with 5 additions and 5 deletions

View File

@@ -61,7 +61,8 @@ namespace detail
typedef objects::pointer_holder<Ptr,value_type> holder;
typedef objects::instance<holder> instance_t;
void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder));
void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder),
boost::python::detail::alignment_of<holder>::value);
try {
#if defined(BOOST_NO_CXX11_SMART_PTR)
(new (memory) holder(x))->install(this->m_self);

View File

@@ -766,10 +766,9 @@ void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std:
throw std::bad_alloc();
const uintptr_t x = reinterpret_cast<uintptr_t>(base_storage) + sizeof(alignment_marker_t);
//this has problems for x -> max(void *)
//const size_t padding = alignment - ((x + sizeof(alignment_marker_t)) % alignment);
//only works for alignments with alignments of powers of 2, but no edge conditions
const uintptr_t padding = alignment == 1 ? 0 : ( alignment - (x & (alignment - 1)) );
// Padding required to align the start of a data structure is: (alignment - (x % alignment)) % alignment
// Since the alignment is a power of two, the formula can be simplified with bitwise AND operator as follow:
const uintptr_t padding = (alignment - (x & (alignment - 1))) & (alignment - 1);
const size_t aligned_offset = sizeof(alignment_marker_t) + padding;
void* const aligned_storage = (char *)base_storage + aligned_offset;
BOOST_ASSERT((char *) aligned_storage + holder_size <= (char *)base_storage + base_allocation);