Skip to content

Commit de80de3

Browse files
committed
Refactor regular_sizeof function and fix type conversion warnings in test_pmr_new.cpp
1 parent 397e25a commit de80de3

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

include/libpmr/new.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@
2222
LIBPMR_NAMESPACE_BEG_
2323

2424
constexpr inline std::size_t regular_level(std::size_t s) noexcept {
25-
if (s <= 128 ) return 0;
26-
if (s <= 1024 ) return 1;
27-
if (s <= 8192 ) return 2;
28-
if (s <= 65536) return 3;
29-
return 4;
25+
return (s <= 128 ) ? 0 :
26+
(s <= 1024 ) ? 1 :
27+
(s <= 8192 ) ? 2 :
28+
(s <= 65536) ? 3 : 4;
29+
}
30+
31+
constexpr inline std::size_t regular_sizeof_impl(std::size_t l, std::size_t s) noexcept {
32+
return (l == 0) ? std::max<std::size_t>(::LIBIMP::round_up<std::size_t>(s, 8), regular_head_size) :
33+
(l == 1) ? ::LIBIMP::round_up<std::size_t>(s, 128) :
34+
(l == 2) ? ::LIBIMP::round_up<std::size_t>(s, 1024) :
35+
(l == 3) ? ::LIBIMP::round_up<std::size_t>(s, 8192) : (std::numeric_limits<std::size_t>::max)();
3036
}
3137

3238
constexpr inline std::size_t regular_sizeof(std::size_t s) noexcept {
33-
switch (regular_level(s)) {
34-
case 0 : return std::max<std::size_t>(::LIBIMP::round_up<std::size_t>(s, 8), regular_head_size);
35-
case 1 : return ::LIBIMP::round_up<std::size_t>(s, 128);
36-
case 2 : return ::LIBIMP::round_up<std::size_t>(s, 1024);
37-
case 3 : return ::LIBIMP::round_up<std::size_t>(s, 8192);
38-
default: return (std::numeric_limits<std::size_t>::max)();
39-
}
39+
return regular_sizeof_impl(regular_level(s), s);
4040
}
4141

4242
template <typename T>
@@ -179,7 +179,11 @@ void delete$(T *p) noexcept {
179179
::LIBIMP::destroy(p);
180180
auto *mem_res = regular_resource<regular_sizeof<T>()>::get();
181181
if (mem_res == nullptr) return;
182+
#if defined(LIBIMP_CC_MSVC_2015)
183+
mem_res->deallocate(p, sizeof(T));
184+
#else
182185
mem_res->deallocate(p, sizeof(T), alignof(T));
186+
#endif
183187
}
184188

185189
LIBPMR_NAMESPACE_END_

test/pmr/test_pmr_new.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ template <std::size_t Pts, std::size_t N>
4242
void test_new$array() {
4343
std::array<void *, Pts> pts;
4444
using T = std::array<char, N>;
45-
for (int i = 0; i < pts.size(); ++i) {
45+
for (int i = 0; i < (int)pts.size(); ++i) {
4646
auto p = pmr::new$<T>();
4747
pts[i] = p;
4848
std::memset(p, i, sizeof(T));
4949
}
50-
for (int i = 0; i < pts.size(); ++i) {
50+
for (int i = 0; i < (int)pts.size(); ++i) {
5151
T tmp;
5252
std::memset(&tmp, i, sizeof(T));
5353
ASSERT_EQ(std::memcmp(pts[i], &tmp, sizeof(T)), 0);

0 commit comments

Comments
 (0)