Skip to content

Commit

Permalink
Fix alignment issue with heap_string
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 11, 2023
1 parent 62e3858 commit b5f5958
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 22 additions & 6 deletions include/jsoncons/detail/heap_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
namespace jsoncons {
namespace detail {

inline void*
align_up(void* ptr, std::size_t alignment) noexcept
{
return reinterpret_cast<void*>(~(alignment - 1) &
(reinterpret_cast<uintptr_t>(ptr) + alignment - 1));
}

template <class Extra,class Allocator>
struct heap_string_base
{
Expand Down Expand Up @@ -52,6 +59,7 @@ namespace detail {

pointer p_;
std::size_t length_;
uint16_t offset_;

~heap_string() noexcept = default;

Expand Down Expand Up @@ -114,23 +122,29 @@ namespace detail {
heap_string_type data;
char_type c[1];
};
typedef typename jsoncons_aligned_storage<sizeof(storage_t), alignof(storage_t)>::type json_storage_kind;
typedef typename jsoncons_aligned_storage<sizeof(storage_t), alignof(storage_t)>::type storage_type;

static size_t aligned_size(std::size_t n)
{
return sizeof(json_storage_kind) + n;
return sizeof(storage_type) + n;
}

public:

static pointer create(const char_type* s, std::size_t length, Extra extra, const Allocator& alloc)
{
std::size_t mem_size = aligned_size(length*sizeof(char_type));
std::size_t len = aligned_size(length*sizeof(char_type));

std::size_t align = alignof(storage_type);
std::size_t mem_len = align-1+len;

byte_allocator_type byte_alloc(alloc);
byte_pointer ptr = byte_alloc.allocate(mem_size);
byte_pointer ptr = byte_alloc.allocate(mem_len);

char* q = extension_traits::to_plain_pointer(ptr);

void* storage = align_up(q, align);

char* storage = extension_traits::to_plain_pointer(ptr);
heap_string_type* ps = new(storage)heap_string_type(extra, byte_alloc);

auto psa = launder_cast<storage_t*>(storage);
Expand All @@ -149,7 +163,9 @@ namespace detail {
{
heap_string_type* rawp = extension_traits::to_plain_pointer(ptr);

char* p = launder_cast<char*>(rawp);
char* q = launder_cast<char*>(rawp);

char* p = q - ptr->offset_;

std::size_t mem_size = aligned_size(ptr->length_*sizeof(char_type));
byte_allocator_type byte_alloc(ptr->get_allocator());
Expand Down
11 changes: 11 additions & 0 deletions test/corelib/src/detail/heap_string_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@

using namespace jsoncons;

using heap_string_factory_type = jsoncons::detail::heap_string_factory<char, null_type, std::allocator<char>>;
using pointer = typename heap_string_factory_type::pointer;

TEST_CASE("heap_string test")
{
std::string s("Hello World");

pointer ptr = heap_string_factory_type::create(s.data(), s.length(), null_type(), std::allocator<char>());
}


0 comments on commit b5f5958

Please sign in to comment.