Skip to content

Commit

Permalink
Alignment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlaugt authored and stefanseefeld committed Sep 22, 2024
1 parent b3a28d7 commit b988d70
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/boost/python/make_constructor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/object/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b988d70

Please sign in to comment.