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/pool/pool.hpp>
19 #include <boost/context/detail/config.hpp>
20 #include <boost/context/stack_context.hpp>
21 #include <boost/context/stack_traits.hpp>
23 #if defined(BOOST_USE_VALGRIND)
24 #include <valgrind/valgrind.h>
27 #ifdef BOOST_HAS_ABI_HEADERS
28 # include BOOST_ABI_PREFIX
34 template< typename traitsT >
35 class basic_pooled_fixedsize_stack {
39 std::atomic< std::size_t > use_count_;
40 std::size_t stack_size_;
41 boost::pool< boost::default_user_allocator_malloc_free > storage_;
44 storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
46 stack_size_( stack_size),
47 storage_( stack_size, next_size, max_size) {
48 BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= stack_size_) );
51 stack_context allocate() {
52 void * vp = storage_.malloc();
54 throw std::bad_alloc();
57 sctx.size = stack_size_;
58 sctx.sp = static_cast< char * >( vp) + sctx.size;
59 #if defined(BOOST_USE_VALGRIND)
60 sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
65 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
66 BOOST_ASSERT( sctx.sp);
67 BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
69 #if defined(BOOST_USE_VALGRIND)
70 VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
72 void * vp = static_cast< char * >( sctx.sp) - sctx.size;
76 friend void intrusive_ptr_add_ref( storage * s) noexcept {
80 friend void intrusive_ptr_release( storage * s) noexcept {
81 if ( 0 == --s->use_count_) {
87 intrusive_ptr< storage > storage_;
90 typedef traitsT traits_type;
92 basic_pooled_fixedsize_stack( std::size_t stack_size = traits_type::default_size(),
93 std::size_t next_size = 32,
94 std::size_t max_size = 0) BOOST_NOEXCEPT_OR_NOTHROW :
95 storage_( new storage( stack_size, next_size, max_size) ) {
98 stack_context allocate() {
99 return storage_->allocate();
102 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
103 storage_->deallocate( sctx);
107 typedef basic_pooled_fixedsize_stack< stack_traits > pooled_fixedsize_stack;
111 #ifdef BOOST_HAS_ABI_HEADERS
112 # include BOOST_ABI_SUFFIX
115 #endif // BOOST_CONTEXT_POOLED_pooled_fixedsize_H