Skip to content

Commit 2892f93

Browse files
committed
Account for alignment in make_observable
1 parent 9e4509a commit 2892f93

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

include/oup/observable_unique_ptr.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,11 @@ auto make_observable(Args&&... args) {
829829
}
830830
} else {
831831
// Pre-allocate memory
832-
constexpr std::size_t block_size = sizeof(control_block_type);
833-
constexpr std::size_t object_size = sizeof(object_type);
834-
constexpr std::size_t obj_offset = block_size;
832+
constexpr std::size_t block_size = sizeof(control_block_type);
833+
constexpr std::size_t object_size = sizeof(object_type);
834+
constexpr std::size_t object_align = alignof(object_type);
835+
constexpr std::size_t align_padding = object_align - 1 - ((block_size - 1) % object_align);
836+
constexpr std::size_t obj_offset = block_size + align_padding;
835837

836838
std::byte* buffer = reinterpret_cast<std::byte*>(operator new(obj_offset + object_size));
837839

0 commit comments

Comments
 (0)