Skip to content

Commit b988d70

Browse files
dlaugtstefanseefeld
authored andcommitted
Alignment fixes
1 parent b3a28d7 commit b988d70

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

include/boost/python/make_constructor.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ namespace detail
6161
typedef objects::pointer_holder<Ptr,value_type> holder;
6262
typedef objects::instance<holder> instance_t;
6363

64-
void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder));
64+
void* memory = holder::allocate(this->m_self, offsetof(instance_t, storage), sizeof(holder),
65+
boost::python::detail::alignment_of<holder>::value);
6566
try {
6667
#if defined(BOOST_NO_CXX11_SMART_PTR)
6768
(new (memory) holder(x))->install(this->m_self);

src/object/class.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,9 @@ void* instance_holder::allocate(PyObject* self_, std::size_t holder_offset, std:
766766
throw std::bad_alloc();
767767

768768
const uintptr_t x = reinterpret_cast<uintptr_t>(base_storage) + sizeof(alignment_marker_t);
769-
//this has problems for x -> max(void *)
770-
//const size_t padding = alignment - ((x + sizeof(alignment_marker_t)) % alignment);
771-
//only works for alignments with alignments of powers of 2, but no edge conditions
772-
const uintptr_t padding = alignment == 1 ? 0 : ( alignment - (x & (alignment - 1)) );
769+
// Padding required to align the start of a data structure is: (alignment - (x % alignment)) % alignment
770+
// Since the alignment is a power of two, the formula can be simplified with bitwise AND operator as follow:
771+
const uintptr_t padding = (alignment - (x & (alignment - 1))) & (alignment - 1);
773772
const size_t aligned_offset = sizeof(alignment_marker_t) + padding;
774773
void* const aligned_storage = (char *)base_storage + aligned_offset;
775774
BOOST_ASSERT((char *) aligned_storage + holder_size <= (char *)base_storage + base_allocation);

0 commit comments

Comments
 (0)