Skip to content

Commit c1b3d75

Browse files
authored
Merge pull request #965 from redboltz/port_962_to_cpp
Ported #962 to C++.
2 parents 76f5af0 + ca9f25b commit c1b3d75

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

erb/v1/cpp03_zone.hpp.erb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
#ifndef MSGPACK_V1_CPP03_ZONE_HPP
1111
#define MSGPACK_V1_CPP03_ZONE_HPP
1212

13+
#include "msgpack/versioning.hpp"
14+
#include "msgpack/cpp_config.hpp"
1315
#include "msgpack/zone_decl.hpp"
1416

17+
#include <stdint.h>
18+
#include <cstdlib>
19+
#include <memory>
20+
#include <vector>
21+
22+
#include <boost/assert.hpp>
23+
1524
<% GENERATION_LIMIT = 15 %>
1625
namespace msgpack {
1726

@@ -192,10 +201,11 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_
192201

193202
inline char* zone::get_aligned(char* ptr, size_t align)
194203
{
204+
BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0)
195205
return
196206
reinterpret_cast<char*>(
197-
reinterpret_cast<size_t>(
198-
(ptr + (align - 1))) / align * align);
207+
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
208+
);
199209
}
200210

201211
inline void* zone::allocate_align(size_t size, size_t align)

include/msgpack/sysdep.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
typedef unsigned __int32 uint32_t;
2727
typedef signed __int64 int64_t;
2828
typedef unsigned __int64 uint64_t;
29+
# if defined(_WIN64)
30+
typedef signed __int64 intptr_t;
31+
typedef unsigned __int64 uintptr_t;
32+
# else
33+
typedef signed __int32 intptr_t;
34+
typedef unsigned __int32 uintptr_t;
35+
# endif
2936
#elif defined(_MSC_VER) // && _MSC_VER >= 1600
3037
# include <stdint.h>
3138
#else

include/msgpack/v1/detail/cpp03_zone.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
#ifndef MSGPACK_V1_CPP03_ZONE_HPP
1111
#define MSGPACK_V1_CPP03_ZONE_HPP
1212

13+
#include "msgpack/versioning.hpp"
14+
#include "msgpack/cpp_config.hpp"
1315
#include "msgpack/zone_decl.hpp"
1416

17+
#include <stdint.h>
18+
#include <cstdlib>
19+
#include <memory>
20+
#include <vector>
21+
22+
#include <boost/assert.hpp>
23+
1524

1625
namespace msgpack {
1726

@@ -237,10 +246,11 @@ inline zone::zone(size_t chunk_size) /* throw() */ :m_chunk_size(chunk_size), m_
237246

238247
inline char* zone::get_aligned(char* ptr, size_t align)
239248
{
249+
BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0)
240250
return
241251
reinterpret_cast<char*>(
242-
reinterpret_cast<size_t>(
243-
(ptr + (align - 1))) / align * align);
252+
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
253+
);
244254
}
245255

246256
inline void* zone::allocate_align(size_t size, size_t align)

include/msgpack/v1/detail/cpp11_zone.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
#include "msgpack/cpp_config.hpp"
1515
#include "msgpack/zone_decl.hpp"
1616

17+
#include <cstdint>
1718
#include <cstdlib>
1819
#include <memory>
1920
#include <vector>
2021

22+
#include <boost/assert.hpp>
23+
2124
namespace msgpack {
2225

2326
/// @cond
@@ -230,10 +233,11 @@ inline zone::zone(size_t chunk_size) noexcept:m_chunk_size(chunk_size), m_chunk_
230233

231234
inline char* zone::get_aligned(char* ptr, size_t align)
232235
{
236+
BOOST_ASSERT(align != 0 && (align & (align - 1)) == 0); // align must be 2^n (n >= 0)
233237
return
234238
reinterpret_cast<char*>(
235-
reinterpret_cast<size_t>(
236-
(ptr + (align - 1))) / align * align);
239+
reinterpret_cast<uintptr_t>(ptr + (align - 1)) & ~static_cast<uintptr_t>(align - 1)
240+
);
237241
}
238242

239243
inline void* zone::allocate_align(size_t size, size_t align)

test/zone.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ BOOST_AUTO_TEST_CASE(allocate_align)
88
msgpack::zone z;
99
char* start = (char*)z.allocate_align(1);
1010
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(start) % sizeof(int));
11-
for (std::size_t s = 1; s < sizeof(int); ++s) {
11+
for (std::size_t s = 1; s < sizeof(int); s <<= 1) {
1212
z.allocate_no_align(s);
1313
char* buf_a = (char*)z.allocate_align(1);
1414
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % sizeof(int));
@@ -18,10 +18,10 @@ BOOST_AUTO_TEST_CASE(allocate_align)
1818
BOOST_AUTO_TEST_CASE(allocate_align_custom)
1919
{
2020
msgpack::zone z;
21-
for (std::size_t align = 1; align < 64; ++align) {
21+
for (std::size_t align = 1; align < 64; align <<= 1) {
2222
char* start = (char*)z.allocate_align(1, align);
2323
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(start) % align);
24-
for (std::size_t s = 1; s < align; ++s) {
24+
for (std::size_t s = 1; s < align; s <<= 1) {
2525
z.allocate_no_align(s);
2626
char* buf_a = (char*)z.allocate_align(1, align);
2727
BOOST_CHECK_EQUAL(0ul, reinterpret_cast<std::size_t>(buf_a) % align);

0 commit comments

Comments
 (0)