2 // Copyright Oliver Kowalke 2014.
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)
7 #ifndef BOOST_CONTEXT_POOLED_pooled_fixedsize_H
8 #define BOOST_CONTEXT_POOLED_pooled_fixedsize_H
15 #include <boost/assert.hpp>
16 #include <boost/config.hpp>
17 #include <boost/intrusive_ptr.hpp>
18 #include <boost/pool/pool.hpp>
20 #include <boost/context/detail/config.hpp>
21 #include <boost/context/stack_context.hpp>
22 #include <boost/context/stack_traits.hpp>
24 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
31 #if defined(BOOST_USE_VALGRIND)
32 #include <valgrind/valgrind.h>
35 #ifdef BOOST_HAS_ABI_HEADERS
36 # include BOOST_ABI_PREFIX
42 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
44 template< typename traitsT >
45 struct map_stack_allocator {
46 typedef std::size_t size_type;
47 typedef std::ptrdiff_t difference_type;
49 static char * malloc( const size_type bytes) {
51 if ( ::posix_memalign( &block, traitsT::page_size(), bytes) != 0) {
54 if ( mmap( block, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_STACK, -1, 0) == MAP_FAILED) {
58 return reinterpret_cast< char * >( block);
60 static void free( char * const block) {
67 template< typename traitsT >
68 class basic_pooled_fixedsize_stack {
72 std::atomic< std::size_t > use_count_;
73 std::size_t stack_size_;
74 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
75 boost::pool< detail::map_stack_allocator< traitsT > > storage_;
77 boost::pool< boost::default_user_allocator_malloc_free > storage_;
81 storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
83 stack_size_( stack_size),
84 storage_( stack_size, next_size, max_size) {
85 BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size_) );
88 stack_context allocate() {
89 void * vp = storage_.malloc();
91 throw std::bad_alloc();
94 sctx.size = stack_size_;
95 sctx.sp = static_cast< char * >( vp) + sctx.size;
96 #if defined(BOOST_USE_VALGRIND)
97 sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
102 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
103 BOOST_ASSERT( sctx.sp);
104 BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
106 #if defined(BOOST_USE_VALGRIND)
107 VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
109 void * vp = static_cast< char * >( sctx.sp) - sctx.size;
113 friend void intrusive_ptr_add_ref( storage * s) noexcept {
117 friend void intrusive_ptr_release( storage * s) noexcept {
118 if ( 0 == --s->use_count_) {
124 intrusive_ptr< storage > storage_;
127 typedef traitsT traits_type;
129 basic_pooled_fixedsize_stack( std::size_t stack_size = traits_type::default_size(),
130 std::size_t next_size = 32,
131 std::size_t max_size = 0) BOOST_NOEXCEPT_OR_NOTHROW :
132 storage_( new storage( stack_size, next_size, max_size) ) {
135 stack_context allocate() {
136 return storage_->allocate();
139 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
140 storage_->deallocate( sctx);
144 typedef basic_pooled_fixedsize_stack< stack_traits > pooled_fixedsize_stack;
148 #ifdef BOOST_HAS_ABI_HEADERS
149 # include BOOST_ABI_SUFFIX
152 #endif // BOOST_CONTEXT_POOLED_pooled_fixedsize_H