diff --git a/erb/v1/cpp03_zone.hpp.erb b/erb/v1/cpp03_zone.hpp.erb index 7dfc35c0..dd873a69 100644 --- a/erb/v1/cpp03_zone.hpp.erb +++ b/erb/v1/cpp03_zone.hpp.erb @@ -10,8 +10,17 @@ #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 + +#include + <% GENERATION_LIMIT = 15 %> namespace msgpack { @@ -192,10 +201,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..09d78be0 100644 --- a/include/msgpack/v1/detail/cpp03_zone.hpp +++ b/include/msgpack/v1/detail/cpp03_zone.hpp @@ -10,8 +10,17 @@ #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 + +#include + namespace msgpack { @@ -237,10 +246,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..df744e31 100644 --- a/include/msgpack/v1/detail/cpp11_zone.hpp +++ b/include/msgpack/v1/detail/cpp11_zone.hpp @@ -14,10 +14,13 @@ #include "msgpack/cpp_config.hpp" #include "msgpack/zone_decl.hpp" +#include #include #include #include +#include + namespace msgpack { /// @cond @@ -230,10 +233,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);