Merge pull request #965 from redboltz/port_962_to_cpp

Ported #962 to C++.
This commit is contained in:
Takatoshi Kondo 2021-08-29 22:12:36 +09:00 committed by GitHub
commit c1b3d7530f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 9 deletions

View File

@ -10,8 +10,17 @@
#ifndef MSGPACK_V1_CPP03_ZONE_HPP #ifndef MSGPACK_V1_CPP03_ZONE_HPP
#define 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 "msgpack/zone_decl.hpp"
#include <stdint.h>
#include <cstdlib>
#include <memory>
#include <vector>
#include <boost/assert.hpp>
<% GENERATION_LIMIT = 15 %> <% GENERATION_LIMIT = 15 %>
namespace msgpack { 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) 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 return
reinterpret_cast<char*>( reinterpret_cast<char*>(
reinterpret_cast<size_t>( reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
(ptr + (align - 1))) / align * align); );
} }
inline void* zone::allocate_align(size_t size, size_t align) inline void* zone::allocate_align(size_t size, size_t align)

View File

@ -26,6 +26,13 @@
typedef unsigned __int32 uint32_t; typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t; typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_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 #elif defined(_MSC_VER) // && _MSC_VER >= 1600
# include <stdint.h> # include <stdint.h>
#else #else

View File

@ -10,8 +10,17 @@
#ifndef MSGPACK_V1_CPP03_ZONE_HPP #ifndef MSGPACK_V1_CPP03_ZONE_HPP
#define 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 "msgpack/zone_decl.hpp"
#include <stdint.h>
#include <cstdlib>
#include <memory>
#include <vector>
#include <boost/assert.hpp>
namespace msgpack { 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) 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 return
reinterpret_cast<char*>( reinterpret_cast<char*>(
reinterpret_cast<size_t>( reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
(ptr + (align - 1))) / align * align); );
} }
inline void* zone::allocate_align(size_t size, size_t align) inline void* zone::allocate_align(size_t size, size_t align)

View File

@ -14,10 +14,13 @@
#include "msgpack/cpp_config.hpp" #include "msgpack/cpp_config.hpp"
#include "msgpack/zone_decl.hpp" #include "msgpack/zone_decl.hpp"
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <boost/assert.hpp>
namespace msgpack { namespace msgpack {
/// @cond /// @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) 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 return
reinterpret_cast<char*>( reinterpret_cast<char*>(
reinterpret_cast<size_t>( reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
(ptr + (align - 1))) / align * align); );
} }
inline void* zone::allocate_align(size_t size, size_t align) inline void* zone::allocate_align(size_t size, size_t align)

View File

@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(allocate_align)
msgpack::zone z; msgpack::zone z;
char* start = (char*)z.allocate_align(1); char* start = (char*)z.allocate_align(1);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(start) % sizeof(int)); BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(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); z.allocate_no_align(s);
char* buf_a = (char*)z.allocate_align(1); char* buf_a = (char*)z.allocate_align(1);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % sizeof(int)); BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % sizeof(int));
@ -18,10 +18,10 @@ BOOST_AUTO_TEST_CASE(allocate_align)
BOOST_AUTO_TEST_CASE(allocate_align_custom) BOOST_AUTO_TEST_CASE(allocate_align_custom)
{ {
msgpack::zone z; 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); char* start = (char*)z.allocate_align(1, align);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(start) % align); BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(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); z.allocate_no_align(s);
char* buf_a = (char*)z.allocate_align(1, align); char* buf_a = (char*)z.allocate_align(1, align);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % align); BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % align);