Skip to content

Commit

Permalink
Rework conversion to std::error_category to not allocate (closes #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Apr 21, 2022
1 parent 28a1357 commit 986efb1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
29 changes: 25 additions & 4 deletions include/boost/system/detail/error_category.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class std_category;
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#endif

#if defined(BOOST_MSVC)
#pragma warning(push)
#pragma warning(disable: 4351) // new behavior: elements of array will be default initialized
#endif

class BOOST_SYMBOL_VISIBLE error_category
{
private:
Expand Down Expand Up @@ -76,13 +81,21 @@ class BOOST_SYMBOL_VISIBLE error_category

boost::ulong_long_type id_;

static std::size_t const stdcat_size_ = 4 * sizeof( void const* );

union
{
mutable unsigned char stdcat_[ stdcat_size_ ];
void const* stdcat_align_;
};

#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)

mutable std::atomic< boost::system::detail::std_category* > ps_;
mutable std::atomic< unsigned > sc_init_;

#else

boost::system::detail::std_category* ps_;
unsigned sc_init_;

#endif

Expand All @@ -103,11 +116,11 @@ class BOOST_SYMBOL_VISIBLE error_category

#endif

BOOST_SYSTEM_CONSTEXPR error_category() BOOST_NOEXCEPT: id_( 0 ), ps_()
BOOST_SYSTEM_CONSTEXPR error_category() BOOST_NOEXCEPT: id_( 0 ), stdcat_(), sc_init_()
{
}

explicit BOOST_SYSTEM_CONSTEXPR error_category( boost::ulong_long_type id ) BOOST_NOEXCEPT: id_( id ), ps_()
explicit BOOST_SYSTEM_CONSTEXPR error_category( boost::ulong_long_type id ) BOOST_NOEXCEPT: id_( id ), stdcat_(), sc_init_()
{
}

Expand Down Expand Up @@ -158,14 +171,22 @@ class BOOST_SYMBOL_VISIBLE error_category
}

#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)

void init_stdcat() const;

# if defined(__SUNPRO_CC) // trailing __global is not supported
operator std::error_category const & () const;
# else
operator std::error_category const & () const BOOST_SYMBOL_VISIBLE;
# endif

#endif
};

#if defined(BOOST_MSVC)
#pragma warning(pop)
#endif

#if ( defined( BOOST_GCC ) && BOOST_GCC >= 40600 ) || defined( BOOST_CLANG )
#pragma GCC diagnostic pop
#endif
Expand Down
78 changes: 44 additions & 34 deletions include/boost/system/detail/error_category_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,60 @@ inline char const * error_category::message( int ev, char * buffer, std::size_t
#if defined(BOOST_SYSTEM_HAS_SYSTEM_ERROR)

#include <boost/system/detail/std_category_impl.hpp>
#include <mutex>
#include <new>

namespace boost
{
namespace system
{

namespace detail
{

template<class = void> struct stdcat_mx_holder
{
static std::mutex mx_;
};

template<class T> std::mutex stdcat_mx_holder<T>::mx_;

} // namespace detail

inline BOOST_NOINLINE void error_category::init_stdcat() const
{
static_assert( sizeof( stdcat_ ) >= sizeof( boost::system::detail::std_category ), "sizeof(stdcat_) is not enough for std_category" );

#if defined(BOOST_MSVC) && BOOST_MSVC < 1900
// no alignof
#else

static_assert( alignof( decltype(stdcat_align_) ) >= alignof( boost::system::detail::std_category ), "alignof(stdcat_) is not enough for std_category" );

#endif

std::lock_guard<std::mutex> lk( boost::system::detail::stdcat_mx_holder<>::mx_ );

if( sc_init_.load( std::memory_order_acquire ) == 0 )
{
::new( static_cast<void*>( stdcat_ ) ) boost::system::detail::std_category( this, 0 );
sc_init_.store( 1, std::memory_order_release );
}
}

inline error_category::operator std::error_category const & () const
{
if( id_ == detail::generic_category_id )
{
// This condition must be the same as the one in error_condition.hpp
#if defined(BOOST_SYSTEM_AVOID_STD_GENERIC_CATEGORY)

static const boost::system::detail::std_category generic_instance( this, 0x1F4D3 );
return generic_instance;
static const boost::system::detail::std_category generic_instance( this, 0x1F4D3 );
return generic_instance;

#else

return std::generic_category();
return std::generic_category();

#endif
}
Expand All @@ -126,47 +161,22 @@ inline error_category::operator std::error_category const & () const
// This condition must be the same as the one in error_code.hpp
#if defined(BOOST_SYSTEM_AVOID_STD_SYSTEM_CATEGORY)

static const boost::system::detail::std_category system_instance( this, 0x1F4D7 );
return system_instance;
static const boost::system::detail::std_category system_instance( this, 0x1F4D7 );
return system_instance;

#else

return std::system_category();
return std::system_category();

#endif
}

detail::std_category* p = ps_.load( std::memory_order_acquire );

if( p != 0 )
if( sc_init_.load( std::memory_order_acquire ) == 0 )
{
return *p;
init_stdcat();
}

// One `std_category` object is allocated for every
// user-defined `error_category` that is converted to
// `std::error_category`.
//
// This one-time allocation will show up on leak checkers.
// That's unavoidable. There is no way to deallocate the
// `std_category` object because first, `error_category`
// is a literal type (so it can't have a destructor) and
// second, `error_category` needs to be usable during program
// shutdown.
//
// https://github.com/boostorg/system/issues/78

detail::std_category* q = new detail::std_category( this, 0 );

if( ps_.compare_exchange_strong( p, q, std::memory_order_release, std::memory_order_acquire ) )
{
return *q;
}
else
{
delete q;
return *p;
}
return *reinterpret_cast<boost::system::detail::std_category const*>( stdcat_ );
}

} // namespace system
Expand Down

0 comments on commit 986efb1

Please sign in to comment.