From 860f7fce55c23e2a5f4c803b50f793505ea0dbb1 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sun, 29 Aug 2021 12:40:28 +0900 Subject: [PATCH 1/2] Ported #962 to C++. Improved alignment calculation logic. Fixed test for zone. Now, align parameter must be 2^n (n >=0). e.g. 1,2,4,8,16, ... --- erb/v1/cpp03_zone.hpp.erb | 13 +++++++++++-- include/msgpack/sysdep.hpp | 7 +++++++ include/msgpack/v1/detail/cpp03_zone.hpp | 13 +++++++++++-- include/msgpack/v1/detail/cpp11_zone.hpp | 7 +++++-- test/zone.cpp | 6 +++--- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/erb/v1/cpp03_zone.hpp.erb b/erb/v1/cpp03_zone.hpp.erb index 7dfc35c0..3272e8de 100644 --- a/erb/v1/cpp03_zone.hpp.erb +++ b/erb/v1/cpp03_zone.hpp.erb @@ -10,8 +10,16 @@ #ifndef MSGPACK_V1_CPP03_ZONE_HPP #define MSGPACK_V1_CPP03_ZONE_HPP +#include "msgpack/versioning.hpp" +#include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include +#include +#include + +#include + <% GENERATION_LIMIT = 15 %> namespace msgpack { @@ -192,10 +200,11 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_ inline char* zone::get_aligned(char* ptr, size_t align) { + BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( - reinterpret_cast( - (ptr + (align - 1))) / align * align); + reinterpret_cast(ptr + (align - 1)) & ~static_cast(align - 1) + ); } inline void* zone::allocate_align(size_t size, size_t align) diff --git a/include/msgpack/sysdep.hpp b/include/msgpack/sysdep.hpp index 9f95b60b..ec71dead 100644 --- a/include/msgpack/sysdep.hpp +++ b/include/msgpack/sysdep.hpp @@ -26,6 +26,13 @@ typedef unsigned __int32 uint32_t; typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; +# if defined(_WIN64) + typedef signed __int64 intptr_t; + typedef unsigned __int64 uintptr_t; +# else + typedef signed __int32 intptr_t; + typedef unsigned __int32 uintptr_t; +# endif #elif defined(_MSC_VER) // && _MSC_VER >= 1600 # include #else diff --git a/include/msgpack/v1/detail/cpp03_zone.hpp b/include/msgpack/v1/detail/cpp03_zone.hpp index 21b3c5e8..9f1c93f7 100644 --- a/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/include/msgpack/v1/detail/cpp03_zone.hpp @@ -10,8 +10,16 @@ #ifndef MSGPACK_V1_CPP03_ZONE_HPP #define MSGPACK_V1_CPP03_ZONE_HPP +#include "msgpack/versioning.hpp" +#include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include +#include +#include + +#include + namespace msgpack { @@ -237,10 +245,11 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_ inline char* zone::get_aligned(char* ptr, size_t align) { + BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( - reinterpret_cast( - (ptr + (align - 1))) / align * align); + reinterpret_cast(ptr + (align - 1)) & ~static_cast(align - 1) + ); } inline void* zone::allocate_align(size_t size, size_t align) diff --git a/include/msgpack/v1/detail/cpp11_zone.hpp b/include/msgpack/v1/detail/cpp11_zone.hpp index c3cf3f6b..d04fd153 100644 --- a/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/include/msgpack/v1/detail/cpp11_zone.hpp @@ -18,6 +18,8 @@ #include #include +#include + namespace msgpack { /// @cond @@ -230,10 +232,11 @@ inline zone::zone(size_t chunk_size) noexcept:m_chunk_size(chunk_size), m_chunk_ inline char* zone::get_aligned(char* ptr, size_t align) { + BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0) return reinterpret_cast( - reinterpret_cast( - (ptr + (align - 1))) / align * align); + reinterpret_cast(ptr + (align - 1)) & ~static_cast(align - 1) + ); } inline void* zone::allocate_align(size_t size, size_t align) diff --git a/test/zone.cpp b/test/zone.cpp index 1ef288a2..438f69b2 100644 --- a/test/zone.cpp +++ b/test/zone.cpp @@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(allocate_align) msgpack::zone z; char* start = (char*)z.allocate_align(1); BOOST_CHECK_EQUAL(0ul, reinterpret_cast(start) % sizeof(int)); - for (std::size_t s = 1; s < sizeof(int); ++s) { + for (std::size_t s = 1; s < sizeof(int); s <<= 1) { z.allocate_no_align(s); char* buf_a = (char*)z.allocate_align(1); BOOST_CHECK_EQUAL(0ul, reinterpret_cast(buf_a) % sizeof(int)); @@ -18,10 +18,10 @@ BOOST_AUTO_TEST_CASE(allocate_align) BOOST_AUTO_TEST_CASE(allocate_align_custom) { msgpack::zone z; - for (std::size_t align = 1; align < 64; ++align) { + for (std::size_t align = 1; align < 64; align <<= 1) { char* start = (char*)z.allocate_align(1, align); BOOST_CHECK_EQUAL(0ul, reinterpret_cast(start) % align); - for (std::size_t s = 1; s < align; ++s) { + for (std::size_t s = 1; s < align; s <<= 1) { z.allocate_no_align(s); char* buf_a = (char*)z.allocate_align(1, align); BOOST_CHECK_EQUAL(0ul, reinterpret_cast(buf_a) % align); From ca9f25b51e106167e2a1fece1095eb0be887775e Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sun, 29 Aug 2021 21:19:41 +0900 Subject: [PATCH 2/2] Added file include. Added cstdint header for uintptr_t (for C++11 or later) Added stdint.h header for uintptr_t (for C++03) --- erb/v1/cpp03_zone.hpp.erb | 1 + include/msgpack/v1/detail/cpp03_zone.hpp | 1 + include/msgpack/v1/detail/cpp11_zone.hpp | 1 + 3 files changed, 3 insertions(+) diff --git a/erb/v1/cpp03_zone.hpp.erb b/erb/v1/cpp03_zone.hpp.erb index 3272e8de..dd873a69 100644 --- a/erb/v1/cpp03_zone.hpp.erb +++ b/erb/v1/cpp03_zone.hpp.erb @@ -14,6 +14,7 @@ #include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include #include #include #include diff --git a/include/msgpack/v1/detail/cpp03_zone.hpp b/include/msgpack/v1/detail/cpp03_zone.hpp index 9f1c93f7..09d78be0 100644 --- a/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/include/msgpack/v1/detail/cpp03_zone.hpp @@ -14,6 +14,7 @@ #include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include #include #include #include diff --git a/include/msgpack/v1/detail/cpp11_zone.hpp b/include/msgpack/v1/detail/cpp11_zone.hpp index d04fd153..df744e31 100644 --- a/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/include/msgpack/v1/detail/cpp11_zone.hpp @@ -14,6 +14,7 @@ #include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include #include #include #include