Clean force_ptr usage and use launder_cast

This commit is contained in:
Ion Gaztañaga
2024-04-15 12:51:15 +02:00
parent 56cc38f761
commit 978bbb113a
17 changed files with 98 additions and 75 deletions
+11 -5
View File
@@ -14,7 +14,6 @@
#include <boost/container/uses_allocator.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/move/core.hpp>
#include <boost/move/detail/force_ptr.hpp>
template<class T, unsigned int Id, bool HasTrueTypes = false>
class propagation_test_allocator
@@ -69,11 +68,18 @@ class propagation_test_allocator
std::size_t max_size() const
{ return std::size_t(-1); }
T* allocate(std::size_t n)
{ return boost::move_detail::force_ptr<T*>(::new char[n*sizeof(T)]); }
value_type* allocate(std::size_t count)
{ return static_cast<value_type*>(::operator new(count * sizeof(value_type))); }
void deallocate(T*p, std::size_t)
{ delete []static_cast<char*>(static_cast<void*>(p)); }
void deallocate(value_type *ptr, std::size_t n)
{
(void)n;
# if __cpp_sized_deallocation
::operator delete((void*)ptr, n * sizeof(value_type));
#else
::operator delete((void*)ptr);
# endif
}
bool m_move_contructed;
bool m_move_assigned;
+20 -7
View File
@@ -34,7 +34,6 @@
#include <boost/move/utility_core.hpp>
#include <boost/move/adl_move_swap.hpp>
#include <boost/move/detail/force_ptr.hpp>
#include <boost/assert.hpp>
#include <memory>
@@ -61,10 +60,17 @@ class simple_allocator
{}
T* allocate(std::size_t n)
{ return move_detail::force_ptr<T*>(::new char[sizeof(T)*n]); }
{ return (T*) ::operator new(sizeof(T) * n); }
void deallocate(T*p, std::size_t)
{ delete[] ((char*)p);}
void deallocate(T *ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
{
(void)n;
# if __cpp_sized_deallocation
::operator delete((void*)ptr, n * sizeof(T));
#else
::operator delete((void*)ptr);
# endif
}
friend bool operator==(const simple_allocator &, const simple_allocator &)
{ return true; }
@@ -176,10 +182,17 @@ class propagation_test_allocator
{ unique_id_ = id; }
T* allocate(std::size_t n)
{ return move_detail::force_ptr<T*>(::new char[sizeof(T)*n]); }
{ return static_cast<T*>(::operator new(n * sizeof(T))); }
void deallocate(T*p, std::size_t)
{ delete[] ((char*)p);}
void deallocate(T *ptr, std::size_t n) BOOST_NOEXCEPT_OR_NOTHROW
{
(void)n;
# if __cpp_sized_deallocation
::operator delete((void*)ptr, n * sizeof(T));
#else
::operator delete((void*)ptr);
# endif
}
friend bool operator==(const propagation_test_allocator &a, const propagation_test_allocator &b)
{ return EqualIfEqualIds ? a.id_ == b.id_ : true; }
+2 -2
View File
@@ -17,7 +17,7 @@
#include <boost/container/detail/mpl.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/move/detail/force_ptr.hpp> //adl_move_swap
#include <boost/move/detail/launder.hpp> //adl_move_swap
namespace boost{
namespace container {
@@ -152,7 +152,7 @@ static boost::container::dtl::aligned_storage<sizeof(EmplaceIntPair)*10>::type p
static EmplaceIntPair* initialize_emplace_int_pair()
{
EmplaceIntPair* ret = move_detail::force_ptr<EmplaceIntPair*>(&pair_storage);
EmplaceIntPair* ret = move_detail::launder_cast<EmplaceIntPair*>(&pair_storage);
for(unsigned int i = 0; i != 10; ++i){
new(&ret->first)EmplaceInt();
new(&ret->second)EmplaceInt();
+10 -4
View File
@@ -22,7 +22,6 @@ volatile ::boost::container::vector<empty> dummy;
#include <boost/container/allocator.hpp>
#include "movable_int.hpp"
#include "dummy_test_allocator.hpp"
#include <boost/move/detail/force_ptr.hpp>
class CustomAllocator
{
@@ -34,10 +33,17 @@ class CustomAllocator
typedef short difference_type;
pointer allocate(size_type count)
{ return boost::move_detail::force_ptr<pointer>(new char[sizeof(value_type)*count]); }
{ return static_cast<value_type*>(::operator new(count * sizeof(value_type))); }
void deallocate(pointer ptr, size_type )
{ delete [](char*)ptr; }
void deallocate(pointer ptr, size_type n)
{
(void)n;
# if __cpp_sized_deallocation
::operator delete((void*)ptr, n * sizeof(value_type));
#else
::operator delete((void*)ptr);
# endif
}
friend bool operator==(CustomAllocator const&, CustomAllocator const&) BOOST_NOEXCEPT
{ return true; }
+1 -1
View File
@@ -630,7 +630,7 @@ bool default_init_test()//Test for default initialization
typedef static_vector<unsigned char, Capacity> di_vector_t;
{
typename dtl::aligned_storage<sizeof(di_vector_t)>::type as;
dtl::aligned_storage<sizeof(di_vector_t)>::type as;
di_vector_t *pv = ::new(as.data)di_vector_t(Capacity);
//Use volatile pointer to make compiler's job harder, as we are riding on UB