Skip to content

Commit

Permalink
Merge pull request #965 from redboltz/port_962_to_cpp
Browse files Browse the repository at this point in the history
Ported #962 to C++.
  • Loading branch information
redboltz authored Aug 29, 2021
2 parents 76f5af0 + ca9f25b commit c1b3d75
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
14 changes: 12 additions & 2 deletions erb/v1/cpp03_zone.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>
#include <cstdlib>
#include <memory>
#include <vector>

#include <boost/assert.hpp>

<% GENERATION_LIMIT = 15 %>
namespace msgpack {

Expand Down Expand Up @@ -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<char*>(
reinterpret_cast<size_t>(
(ptr + (align - 1))) / align * align);
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
);
}

inline void* zone::allocate_align(size_t size, size_t align)
Expand Down
7 changes: 7 additions & 0 deletions include/msgpack/sysdep.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>
#else
Expand Down
14 changes: 12 additions & 2 deletions include/msgpack/v1/detail/cpp03_zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>
#include <cstdlib>
#include <memory>
#include <vector>

#include <boost/assert.hpp>


namespace msgpack {

Expand Down Expand Up @@ -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<char*>(
reinterpret_cast<size_t>(
(ptr + (align - 1))) / align * align);
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
);
}

inline void* zone::allocate_align(size_t size, size_t align)
Expand Down
8 changes: 6 additions & 2 deletions include/msgpack/v1/detail/cpp11_zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
#include "msgpack/cpp_config.hpp"
#include "msgpack/zone_decl.hpp"

#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>

#include <boost/assert.hpp>

namespace msgpack {

/// @cond
Expand Down Expand Up @@ -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<char*>(
reinterpret_cast<size_t>(
(ptr + (align - 1))) / align * align);
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
);
}

inline void* zone::allocate_align(size_t size, size_t align)
Expand Down
6 changes: 3 additions & 3 deletions test/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
char* buf_a = (char*)z.allocate_align(1);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % sizeof(int));
Expand All @@ -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<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);
char* buf_a = (char*)z.allocate_align(1, align);
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % align);
Expand Down

0 comments on commit c1b3d75

Please sign in to comment.