Skip to content

Commit

Permalink
Clean force_ptr usage and use launder_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
igaztanaga committed Apr 15, 2024
1 parent 56cc38f commit 978bbb1
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 76 deletions.
17 changes: 9 additions & 8 deletions include/boost/container/detail/advanced_insert_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <boost/move/utility_core.hpp>
#include <boost/move/detail/force_ptr.hpp>
#include <boost/move/detail/launder.hpp>
// other
#include <boost/assert.hpp>

Expand Down Expand Up @@ -138,8 +139,8 @@ struct insert_value_initialized_n_proxy
while (n){
--n;
storage_t v;
alloc_traits::construct(a, move_detail::force_ptr<value_type *>(&v));
value_type *vp = move_detail::force_ptr<value_type *>(&v);
alloc_traits::construct(a, (value_type*)&v);
value_type *vp = move_detail::launder_cast<value_type *>(&v);
value_destructor<Allocator> on_exit(a, *vp); (void)on_exit;
*p = ::boost::move(*vp);
++p;
Expand All @@ -165,8 +166,8 @@ struct insert_default_initialized_n_proxy
while (n){
--n;
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
alloc_traits::construct(a, move_detail::force_ptr<value_type *>(&v), default_init);
value_type *vp = move_detail::force_ptr<value_type *>(&v);
alloc_traits::construct(a, (value_type*)&v, default_init);
value_type *vp = move_detail::launder_cast<value_type *>(&v);
value_destructor<Allocator> on_exit(a, *vp); (void)on_exit;
*p = ::boost::move(*vp);
++p;
Expand Down Expand Up @@ -312,8 +313,8 @@ struct insert_emplace_proxy
{
BOOST_ASSERT(n ==1); (void)n;
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
alloc_traits::construct(a, move_detail::force_ptr<value_type *>(&v), ::boost::forward<Args>(get<IdxPack>(this->args_))...);
value_type *vp = move_detail::force_ptr<value_type *>(&v);
alloc_traits::construct(a, (value_type*)&v, ::boost::forward<Args>(get<IdxPack>(this->args_))...);
value_type *vp = move_detail::launder_cast<value_type *>(&v);
BOOST_CONTAINER_TRY{
*p = ::boost::move(*vp);
}
Expand Down Expand Up @@ -435,8 +436,8 @@ struct insert_emplace_proxy_arg##N\
{\
BOOST_ASSERT(n == 1); (void)n;\
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
alloc_traits::construct(a, move_detail::force_ptr<value_type *>(&v) BOOST_MOVE_I##N BOOST_MOVE_MFWD##N);\
value_type *vp = move_detail::force_ptr<value_type *>(&v);\
alloc_traits::construct(a, (value_type*)&v BOOST_MOVE_I##N BOOST_MOVE_MFWD##N);\
value_type *vp = move_detail::launder_cast<value_type *>(&v);\
BOOST_CONTAINER_TRY{\
*p = ::boost::move(*vp);\
}\
Expand Down
33 changes: 17 additions & 16 deletions include/boost/container/detail/flat_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/detail/iterator_to_raw_pointer.hpp>
#include <boost/move/detail/force_ptr.hpp>
#include <boost/move/detail/launder.hpp>
#include <boost/move/algo/adaptive_sort.hpp>
#include <boost/move/algo/detail/pdqsort.hpp>

Expand Down Expand Up @@ -975,8 +976,8 @@ class flat_tree
{
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::force_ptr<value_type *>(&v);
stored_allocator_traits::construct(a, (value_type *)(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::launder_cast<value_type *>(&v);
value_destructor<stored_allocator_type, value_type> d(a, *pval);
return this->insert_unique(::boost::move(*pval));
}
Expand All @@ -987,8 +988,8 @@ class flat_tree
//hint checked in insert_unique
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::force_ptr<value_type *>(&v);
stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::launder_cast<value_type *>(&v);
value_destructor<stored_allocator_type, value_type> d(a, *pval);
return this->insert_unique(hint, ::boost::move(*pval));
}
Expand All @@ -998,8 +999,8 @@ class flat_tree
{
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::force_ptr<value_type *>(&v);
stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::launder_cast<value_type *>(&v);
value_destructor<stored_allocator_type, value_type> d(a, *pval);
return this->insert_equal(::boost::move(*pval));
}
Expand All @@ -1010,8 +1011,8 @@ class flat_tree
//hint checked in insert_equal
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::force_ptr<value_type *>(&v);
stored_allocator_traits::construct(a, (value_type*)(&v), ::boost::forward<Args>(args)... );
value_type *pval = move_detail::launder_cast<value_type *>(&v);
value_destructor<stored_allocator_type, value_type> d(a, *pval);
return this->insert_equal(hint, ::boost::move(*pval));
}
Expand Down Expand Up @@ -1044,8 +1045,8 @@ class flat_tree
{\
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::force_ptr<value_type *>(&v);\
stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::launder_cast<value_type *>(&v);\
value_destructor<stored_allocator_type, value_type> d(a, *pval);\
return this->insert_unique(::boost::move(*pval));\
}\
Expand All @@ -1055,8 +1056,8 @@ class flat_tree
{\
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::force_ptr<value_type *>(&v);\
stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::launder_cast<value_type *>(&v);\
value_destructor<stored_allocator_type, value_type> d(a, *pval);\
return this->insert_unique(hint, ::boost::move(*pval));\
}\
Expand All @@ -1066,8 +1067,8 @@ class flat_tree
{\
typename dtl::aligned_storage<sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::force_ptr<value_type *>(&v);\
stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::launder_cast<value_type *>(&v);\
value_destructor<stored_allocator_type, value_type> d(a, *pval);\
return this->insert_equal(::boost::move(*pval));\
}\
Expand All @@ -1077,8 +1078,8 @@ class flat_tree
{\
typename dtl::aligned_storage <sizeof(value_type), dtl::alignment_of<value_type>::value>::type v;\
get_stored_allocator_noconst_return_t a = this->get_stored_allocator();\
stored_allocator_traits::construct(a, move_detail::force_ptr<value_type *>(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::force_ptr<value_type *>(&v);\
stored_allocator_traits::construct(a, (value_type *)(&v) BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
value_type *pval = move_detail::launder_cast<value_type *>(&v);\
value_destructor<stored_allocator_type, value_type> d(a, *pval);\
return this->insert_equal(hint, ::boost::move(*pval));\
}\
Expand Down
9 changes: 5 additions & 4 deletions include/boost/container/detail/node_alloc_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <boost/container/detail/construct_in_place.hpp>
#include <boost/container/detail/destroyers.hpp>
#include <boost/move/detail/iterator_to_raw_pointer.hpp>
#include <boost/move/detail/launder.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/placement_new.hpp>
#include <boost/move/detail/to_raw_pointer.hpp>
Expand Down Expand Up @@ -119,16 +120,16 @@ struct base_node
}

inline T &get_data()
{ return *move_detail::force_ptr<T*>(this->m_storage.data); }
{ return *move_detail::force_ptr<T*>(&this->m_storage); }

inline const T &get_data() const
{ return *move_detail::force_ptr<const T*>(this->m_storage.data); }
{ return *move_detail::launder_cast<const T*>(&this->m_storage); }

inline internal_type &get_real_data()
{ return *move_detail::force_ptr<internal_type*>(this->m_storage.data); }
{ return *move_detail::launder_cast<internal_type*>(&this->m_storage); }

inline const internal_type &get_real_data() const
{ return *move_detail::force_ptr<const internal_type*>(this->m_storage.data); }
{ return *move_detail::launder_cast<const internal_type*>(&this->m_storage); }

#if defined(BOOST_CONTAINER_DISABLE_ALIASING_WARNING)
#pragma GCC diagnostic pop
Expand Down
1 change: 0 additions & 1 deletion include/boost/container/detail/tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
#include <boost/move/detail/fwd_macros.hpp>
#endif
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/detail/force_ptr.hpp>



Expand Down
1 change: 0 additions & 1 deletion include/boost/container/devector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <boost/move/utility_core.hpp>
#include <boost/move/detail/to_raw_pointer.hpp>
#include <boost/move/algo/detail/merge.hpp>
#include <boost/move/detail/force_ptr.hpp>

//std
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
Expand Down
1 change: 0 additions & 1 deletion include/boost/container/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
# include <boost/move/detail/fwd_macros.hpp>
#endif
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/detail/force_ptr.hpp>

// intrusive
#include <boost/intrusive/pointer_traits.hpp>
Expand Down
7 changes: 4 additions & 3 deletions include/boost/container/node_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/container/detail/workaround.hpp>
#include <boost/container/detail/placement_new.hpp>
#include <boost/move/detail/to_raw_pointer.hpp>
#include <boost/move/detail/launder.hpp>
#include <boost/container/allocator_traits.hpp>
#include <boost/container/detail/mpl.hpp>

Expand Down Expand Up @@ -146,7 +147,7 @@ class node_handle
}

void destroy_alloc() BOOST_NOEXCEPT
{ static_cast<nallocator_type*>((void*)m_nalloc_storage.data)->~nallocator_type(); }
{ move_detail::launder_cast<nallocator_type*>(&m_nalloc_storage)->~nallocator_type(); }

node_pointer &get_node_pointer() BOOST_NOEXCEPT
{ return m_ptr; }
Expand Down Expand Up @@ -381,7 +382,7 @@ class node_handle
nallocator_type &node_alloc() BOOST_NOEXCEPT
{
BOOST_ASSERT(!empty());
return *static_cast<nallocator_type*>((void*)m_nalloc_storage.data);
return *move_detail::launder_cast<nallocator_type*>(&m_nalloc_storage);
}


Expand All @@ -391,7 +392,7 @@ class node_handle
const nallocator_type &node_alloc() const BOOST_NOEXCEPT
{
BOOST_ASSERT(!empty());
return *static_cast<const nallocator_type*>((const void*)m_nalloc_storage.data);
return *move_detail::launder_cast<const nallocator_type*>(&m_nalloc_storage);
}

//! <b>Effects</b>: x.swap(y).
Expand Down
2 changes: 1 addition & 1 deletion include/boost/container/slist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include <boost/move/detail/fwd_macros.hpp>
#endif
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/detail/force_ptr.hpp>

// std
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
#include <initializer_list>
Expand Down
5 changes: 2 additions & 3 deletions include/boost/container/small_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <boost/move/detail/fwd_macros.hpp>
#endif
#include <boost/move/detail/force_ptr.hpp>

//std
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
Expand Down Expand Up @@ -488,7 +487,7 @@ template<class T, class VoidAlloc, class Options>
inline typename small_vector_allocator<T, VoidAlloc, Options>::const_pointer
small_vector_allocator<T, VoidAlloc, Options>::internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
{
const vector_type& v = reinterpret_cast<const vector_type&>(*this);
const vector_type& v = *static_cast<const vector_type*>(static_cast<const void *>(this));
BOOST_ASSERT((std::size_t(this) % dtl::alignment_of< small_vector_storage_offset<T, allocator_type, Options> >::value) == 0);
const char *addr = reinterpret_cast<const char*>(&v);
typedef typename boost::intrusive::pointer_traits<pointer>::template rebind_pointer<const char>::type const_char_pointer;
Expand All @@ -501,7 +500,7 @@ template <class T, class VoidAlloc, class Options>
inline typename small_vector_allocator<T, VoidAlloc, Options>::pointer
small_vector_allocator<T, VoidAlloc, Options>::internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
{
vector_type& v = reinterpret_cast<vector_type&>(*this);
vector_type& v = *static_cast<vector_type*>(static_cast<void*>(this));
BOOST_ASSERT((std::size_t(this) % dtl::alignment_of< small_vector_storage_offset<T, allocator_type, Options> >::value) == 0);
char* addr = reinterpret_cast<char*>(&v);
typedef typename boost::intrusive::pointer_traits<pointer>::template rebind_pointer<char>::type char_pointer;
Expand Down
12 changes: 6 additions & 6 deletions include/boost/container/stable_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <boost/move/utility_core.hpp>
#include <boost/move/iterator.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/detail/force_ptr.hpp>
#include <boost/move/detail/launder.hpp>
// move/detail
#include <boost/move/detail/move_helpers.hpp>
#include <boost/move/detail/iterator_to_raw_pointer.hpp>
Expand Down Expand Up @@ -166,19 +166,19 @@ struct node
# endif

inline T &get_data()
{ return *boost::move_detail::force_ptr<T*>(this->m_storage.data); }
{ return *boost::move_detail::launder_cast<T*>(&this->m_storage); }

inline const T &get_data() const
{ return *boost::move_detail::force_ptr<const T*>(this->m_storage.data); }
{ return *boost::move_detail::launder_cast<const T*>(&this->m_storage); }

inline T *get_data_ptr()
{ return boost::move_detail::force_ptr<T*>(this->m_storage.data); }
{ return boost::move_detail::launder_cast<T*>(&this->m_storage); }

inline const T *get_data_ptr() const
{ return boost::move_detail::force_ptr<const T*>(this->m_storage.data); }
{ return boost::move_detail::launder_cast<const T*>(&this->m_storage); }

inline ~node()
{ boost::move_detail::force_ptr<T*>(this->m_storage.data)->~T(); }
{ boost::move_detail::launder_cast<T*>(&this->m_storage)->~T(); }

#if defined(BOOST_CONTAINER_DISABLE_ALIASING_WARNING)
#pragma GCC diagnostic pop
Expand Down
6 changes: 2 additions & 4 deletions include/boost/container/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <boost/container/detail/config_begin.hpp>
#include <boost/container/detail/workaround.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/move/detail/launder.hpp>
#include <boost/container/vector.hpp>

#include <cstddef>
Expand Down Expand Up @@ -63,10 +64,7 @@ class static_storage_allocator
{ return *this; }

inline T* internal_storage() const BOOST_NOEXCEPT_OR_NOTHROW
{ return const_cast<T*>(static_cast<const T*>(static_cast<const void*>(storage.data))); }

inline T* internal_storage() BOOST_NOEXCEPT_OR_NOTHROW
{ return static_cast<T*>(static_cast<void*>(storage.data)); }
{ return move_detail::launder_cast<T*>(&storage); }

static const std::size_t internal_capacity = N;

Expand Down
16 changes: 8 additions & 8 deletions include/boost/container/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//intrusive
#include <boost/intrusive/pointer_traits.hpp>
#include <boost/intrusive/detail/hash_combine.hpp>
#include <boost/move/detail/force_ptr.hpp>
#include <boost/move/detail/launder.hpp>
//move
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
Expand Down Expand Up @@ -226,7 +226,7 @@ class basic_string_base
{
inline void init()
{
short_t &s = *::new(this->m_repr.data) short_t;
short_t &s = *::new(&this->m_repr) short_t;
s.h.is_short = 1;
s.h.length = 0;
}
Expand All @@ -241,16 +241,16 @@ class basic_string_base
{ this->init(); }

inline const short_t *pshort_repr() const
{ return move_detail::force_ptr<const short_t*>(m_repr.data); }
{ return move_detail::launder_cast<const short_t*>(&m_repr); }

inline const long_t *plong_repr() const
{ return move_detail::force_ptr<const long_t*>(m_repr.data); }
{ return move_detail::launder_cast<const long_t*>(&m_repr); }

inline short_t *pshort_repr()
{ return move_detail::force_ptr<short_t*>(m_repr.data); }
{ return move_detail::launder_cast<short_t*>(&m_repr); }

inline long_t *plong_repr()
{ return move_detail::force_ptr<long_t*>(m_repr.data); }
{ return move_detail::launder_cast<long_t*>(&m_repr); }

repr_t m_repr;
} members_;
Expand Down Expand Up @@ -280,7 +280,7 @@ class basic_string_base

inline short_t *construct_short()
{
short_t *ps = ::new(this->members_.m_repr.data) short_t;
short_t *ps = ::new(&this->members_.m_repr) short_t;
ps->h.is_short = 1;
return ps;
}
Expand All @@ -302,7 +302,7 @@ class basic_string_base

inline long_t *construct_long()
{
long_t *pl = ::new(this->members_.m_repr.data) long_t;
long_t *pl = ::new(&this->members_.m_repr) long_t;
//is_short flag is written in the constructor
return pl;
}
Expand Down
Loading

0 comments on commit 978bbb1

Please sign in to comment.