Skip to content

Commit ea5a132

Browse files
committed
refactor storage_ptr to appease Fil-C
1 parent 68aea8d commit ea5a132

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

include/boost/json/storage_ptr.hpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ class storage_ptr
8383
using default_resource =
8484
detail::default_resource;
8585

86-
std::uintptr_t i_;
86+
void* p_;
8787

8888
shared_resource*
8989
get_shared() const noexcept
9090
{
91+
auto const i = reinterpret_cast<std::uintptr_t>(p_);
92+
auto const c_ptr = reinterpret_cast<unsigned char*>(p_) - (i & 3);
9193
return static_cast<shared_resource*>(
92-
reinterpret_cast<container::pmr::memory_resource*>(
93-
i_ & ~3));
94+
reinterpret_cast<container::pmr::memory_resource*>(c_ptr));
9495
}
9596

9697
void
@@ -116,9 +117,9 @@ class storage_ptr
116117
template<class T>
117118
storage_ptr(
118119
detail::shared_resource_impl<T>* p) noexcept
119-
: i_(reinterpret_cast<std::uintptr_t>(
120-
static_cast<container::pmr::memory_resource*>(p)) + 1 +
121-
(json::is_deallocate_trivial<T>::value ? 2 : 0))
120+
: p_(reinterpret_cast<unsigned char*>(
121+
static_cast<container::pmr::memory_resource*>(p))
122+
+ 1 + (json::is_deallocate_trivial<T>::value ? 2 : 0))
122123
{
123124
BOOST_ASSERT(p);
124125
}
@@ -184,7 +185,7 @@ class storage_ptr
184185
@{
185186
*/
186187
storage_ptr() noexcept
187-
: i_(0)
188+
: p_(nullptr)
188189
{
189190
}
190191

@@ -201,9 +202,9 @@ class storage_ptr
201202
#endif
202203
>
203204
storage_ptr(T* r) noexcept
204-
: i_(reinterpret_cast<std::uintptr_t>(
205-
static_cast<container::pmr::memory_resource *>(r)) +
206-
(json::is_deallocate_trivial<T>::value ? 2 : 0))
205+
: p_(reinterpret_cast<unsigned char*>(
206+
static_cast<container::pmr::memory_resource*>(r))
207+
+ (json::is_deallocate_trivial<T>::value ? 2 : 0))
207208
{
208209
BOOST_ASSERT(r);
209210
}
@@ -217,8 +218,7 @@ class storage_ptr
217218
template<class V>
218219
storage_ptr(
219220
container::pmr::polymorphic_allocator<V> const& alloc) noexcept
220-
: i_(reinterpret_cast<std::uintptr_t>(
221-
alloc.resource()))
221+
: p_(alloc.resource())
222222
{
223223
}
224224

@@ -228,7 +228,7 @@ class storage_ptr
228228
*/
229229
storage_ptr(
230230
storage_ptr&& other) noexcept
231-
: i_(detail::exchange(other.i_, 0))
231+
: p_(detail::exchange(other.p_, nullptr))
232232
{
233233
}
234234

@@ -238,7 +238,7 @@ class storage_ptr
238238
*/
239239
storage_ptr(
240240
storage_ptr const& other) noexcept
241-
: i_(other.i_)
241+
: p_(other.p_)
242242
{
243243
addref();
244244
}
@@ -277,7 +277,7 @@ class storage_ptr
277277
storage_ptr&& other) noexcept
278278
{
279279
release();
280-
i_ = detail::exchange(other.i_, 0);
280+
p_ = detail::exchange(other.p_, nullptr);
281281
return *this;
282282
}
283283

@@ -287,7 +287,7 @@ class storage_ptr
287287
{
288288
other.addref();
289289
release();
290-
i_ = other.i_;
290+
p_ = other.p_;
291291
return *this;
292292
}
293293
/// @}
@@ -300,7 +300,8 @@ class storage_ptr
300300
bool
301301
is_shared() const noexcept
302302
{
303-
return (i_ & 1) != 0;
303+
auto i = reinterpret_cast<std::uintptr_t>(p_);
304+
return (i & 1) != 0;
304305
}
305306

306307
/** Check if calling `deallocate` on the memory resource has no effect.
@@ -313,7 +314,8 @@ class storage_ptr
313314
bool
314315
is_deallocate_trivial() const noexcept
315316
{
316-
return (i_ & 2) != 0;
317+
auto i = reinterpret_cast<std::uintptr_t>(p_);
318+
return (i & 2) != 0;
317319
}
318320

319321
/** Check if ownership of the memory resource is not shared and deallocate is trivial.
@@ -325,7 +327,8 @@ class storage_ptr
325327
bool
326328
is_not_shared_and_deallocate_is_trivial() const noexcept
327329
{
328-
return (i_ & 3) == 2;
330+
auto i = reinterpret_cast<std::uintptr_t>(p_);
331+
return (i & 3) == 2;
329332
}
330333

331334
/** Return a pointer to the memory resource.
@@ -342,10 +345,12 @@ class storage_ptr
342345
container::pmr::memory_resource*
343346
get() const noexcept
344347
{
345-
if(i_ != 0)
346-
return reinterpret_cast<
347-
container::pmr::memory_resource*>(i_ & ~3);
348-
return default_resource::get();
348+
if(!p_)
349+
return default_resource::get();
350+
351+
auto const i = reinterpret_cast<std::uintptr_t>(p_);
352+
auto const c_ptr = reinterpret_cast<unsigned char*>(p_) - (i & 3);
353+
return reinterpret_cast<container::pmr::memory_resource*>(c_ptr);
349354
}
350355

351356
/** Return a pointer to the memory resource.

0 commit comments

Comments
 (0)