1 2 // Copyright Oliver Kowalke 2009. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef BOOST_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H 8 #define BOOST_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H 9 10 #include <cstddef> 11 #include <new> 12 13 #include <boost/config.hpp> 14 15 #include <boost/coroutine/detail/config.hpp> 16 #include <boost/coroutine/stack_context.hpp> 17 #include <boost/coroutine/stack_traits.hpp> 18 19 #ifdef BOOST_HAS_ABI_HEADERS 20 # include BOOST_ABI_PREFIX 21 #endif 22 23 // forward declaration for splitstack-functions defined in libgcc 24 extern "C" { 25 void *__splitstack_makecontext( std::size_t, 26 void * [BOOST_CONTEXT_SEGMENTS], 27 std::size_t *); 28 29 void __splitstack_releasecontext( void * [BOOST_CONTEXT_SEGMENTS]); 30 31 void __splitstack_resetcontext( void * [BOOST_CONTEXT_SEGMENTS]); 32 33 void __splitstack_block_signals_context( void * [BOOST_CONTEXT_SEGMENTS], 34 int * new_value, int * old_value); 35 } 36 37 namespace boost { 38 namespace coroutines { 39 40 template< typename traitsT > 41 struct basic_segmented_stack_allocator 42 { 43 typedef traitsT traits_type; 44 allocateboost::coroutines::basic_segmented_stack_allocator45 void allocate( stack_context & ctx, std::size_t size = traits_type::minimum_size() ) 46 { 47 void * limit = __splitstack_makecontext( size, ctx.segments_ctx, & ctx.size); 48 if ( ! limit) throw std::bad_alloc(); 49 50 // ctx.size is already filled by __splitstack_makecontext 51 ctx.sp = static_cast< char * >( limit) + ctx.size; 52 53 int off = 0; 54 __splitstack_block_signals_context( ctx.segments_ctx, & off, 0); 55 } 56 deallocateboost::coroutines::basic_segmented_stack_allocator57 void deallocate( stack_context & ctx) 58 { __splitstack_releasecontext( ctx.segments_ctx); } 59 }; 60 61 typedef basic_segmented_stack_allocator< stack_traits > segmented_stack_allocator; 62 63 }} 64 65 #ifdef BOOST_HAS_ABI_HEADERS 66 # include BOOST_ABI_SUFFIX 67 #endif 68 69 #endif // BOOST_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H 70