Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:09

0001 
0002 //          Copyright Oliver Kowalke 2014.
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //    (See accompanying file LICENSE_1_0.txt or copy at
0005 //          http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_CONTEXT_POOLED_pooled_fixedsize_H
0008 #define BOOST_CONTEXT_POOLED_pooled_fixedsize_H
0009 
0010 #include <atomic>
0011 #include <cstddef>
0012 #include <cstdlib>
0013 #include <new>
0014 
0015 #include <boost/assert.hpp>
0016 #include <boost/config.hpp>
0017 #include <boost/intrusive_ptr.hpp>
0018 #include <boost/pool/pool.hpp>
0019 
0020 #include <boost/context/detail/config.hpp>
0021 #include <boost/context/stack_context.hpp>
0022 #include <boost/context/stack_traits.hpp>
0023 
0024 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
0025 extern "C" {
0026 #include <sys/mman.h>
0027 #include <stdlib.h>
0028 }
0029 #endif
0030 
0031 #if defined(BOOST_USE_VALGRIND)
0032 #include <valgrind/valgrind.h>
0033 #endif
0034 
0035 #ifdef BOOST_HAS_ABI_HEADERS
0036 #  include BOOST_ABI_PREFIX
0037 #endif
0038 
0039 namespace boost {
0040 namespace context {
0041 
0042 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
0043 namespace detail {
0044 template< typename traitsT >
0045 struct map_stack_allocator {
0046     typedef std::size_t size_type;
0047     typedef std::ptrdiff_t difference_type;
0048 
0049     static char * malloc( const size_type bytes) {
0050         void * block;
0051         if ( ::posix_memalign( &block, traitsT::page_size(), bytes) != 0) {
0052             return 0;
0053         }
0054         if ( mmap( block, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_STACK, -1, 0) == MAP_FAILED) {
0055             std::free( block);
0056             return 0;
0057         }
0058         return reinterpret_cast< char * >( block);
0059     }
0060     static void free( char * const block) {
0061         std::free( block);
0062     }
0063 };
0064 }
0065 #endif
0066 
0067 template< typename traitsT >
0068 class basic_pooled_fixedsize_stack {
0069 private:
0070     class storage {
0071     private:
0072         std::atomic< std::size_t >                                  use_count_;
0073         std::size_t                                                 stack_size_;
0074 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
0075         boost::pool< detail::map_stack_allocator< traitsT > >       storage_;
0076 #else
0077         boost::pool< boost::default_user_allocator_malloc_free >    storage_;
0078 #endif
0079 
0080     public:
0081         storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
0082                 use_count_( 0),
0083                 stack_size_( stack_size),
0084                 storage_( stack_size, next_size, max_size) {
0085             BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size_) );
0086         }
0087 
0088         stack_context allocate() {
0089             void * vp = storage_.malloc();
0090             if ( ! vp) {
0091                 throw std::bad_alloc();
0092             }
0093             stack_context sctx;
0094             sctx.size = stack_size_;
0095             sctx.sp = static_cast< char * >( vp) + sctx.size;
0096 #if defined(BOOST_USE_VALGRIND)
0097             sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
0098 #endif
0099             return sctx;
0100         }
0101 
0102         void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
0103             BOOST_ASSERT( sctx.sp);
0104             BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
0105 
0106 #if defined(BOOST_USE_VALGRIND)
0107             VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
0108 #endif
0109             void * vp = static_cast< char * >( sctx.sp) - sctx.size;
0110             storage_.free( vp);
0111         }
0112 
0113         friend void intrusive_ptr_add_ref( storage * s) noexcept {
0114             ++s->use_count_;
0115         }
0116 
0117         friend void intrusive_ptr_release( storage * s) noexcept {
0118             if ( 0 == --s->use_count_) {
0119                 delete s;
0120             }
0121         }
0122     };
0123 
0124     intrusive_ptr< storage >    storage_;
0125 
0126 public:
0127     typedef traitsT traits_type;
0128 
0129     basic_pooled_fixedsize_stack( std::size_t stack_size = traits_type::default_size(),
0130                            std::size_t next_size = 32,
0131                            std::size_t max_size = 0) BOOST_NOEXCEPT_OR_NOTHROW :
0132         storage_( new storage( stack_size, next_size, max_size) ) {
0133     }
0134 
0135     stack_context allocate() {
0136         return storage_->allocate();
0137     }
0138 
0139     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
0140         storage_->deallocate( sctx);
0141     }
0142 };
0143 
0144 typedef basic_pooled_fixedsize_stack< stack_traits >  pooled_fixedsize_stack;
0145 
0146 }}
0147 
0148 #ifdef BOOST_HAS_ABI_HEADERS
0149 #  include BOOST_ABI_SUFFIX
0150 #endif
0151 
0152 #endif // BOOST_CONTEXT_POOLED_pooled_fixedsize_H