]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/context/include/boost/context/pooled_fixedsize_stack.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / context / include / boost / context / pooled_fixedsize_stack.hpp
1
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)
6
7 #ifndef BOOST_CONTEXT_POOLED_pooled_fixedsize_H
8 #define BOOST_CONTEXT_POOLED_pooled_fixedsize_H
9
10 #include <atomic>
11 #include <cstddef>
12 #include <cstdlib>
13 #include <new>
14
15 #include <boost/assert.hpp>
16 #include <boost/config.hpp>
17 #include <boost/pool/pool.hpp>
18
19 #include <boost/context/detail/config.hpp>
20 #include <boost/context/stack_context.hpp>
21 #include <boost/context/stack_traits.hpp>
22
23 #if defined(BOOST_USE_VALGRIND)
24 #include <valgrind/valgrind.h>
25 #endif
26
27 #ifdef BOOST_HAS_ABI_HEADERS
28 # include BOOST_ABI_PREFIX
29 #endif
30
31 namespace boost {
32 namespace context {
33
34 template< typename traitsT >
35 class basic_pooled_fixedsize_stack {
36 private:
37 class storage {
38 private:
39 std::atomic< std::size_t > use_count_;
40 std::size_t stack_size_;
41 boost::pool< boost::default_user_allocator_malloc_free > storage_;
42
43 public:
44 storage( std::size_t stack_size, std::size_t next_size, std::size_t max_size) :
45 use_count_( 0),
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_) );
49 }
50
51 stack_context allocate() {
52 void * vp = storage_.malloc();
53 if ( ! vp) {
54 throw std::bad_alloc();
55 }
56 stack_context sctx;
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);
61 #endif
62 return sctx;
63 }
64
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) );
68
69 #if defined(BOOST_USE_VALGRIND)
70 VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
71 #endif
72 void * vp = static_cast< char * >( sctx.sp) - sctx.size;
73 storage_.free( vp);
74 }
75
76 friend void intrusive_ptr_add_ref( storage * s) noexcept {
77 ++s->use_count_;
78 }
79
80 friend void intrusive_ptr_release( storage * s) noexcept {
81 if ( 0 == --s->use_count_) {
82 delete s;
83 }
84 }
85 };
86
87 intrusive_ptr< storage > storage_;
88
89 public:
90 typedef traitsT traits_type;
91
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) ) {
96 }
97
98 stack_context allocate() {
99 return storage_->allocate();
100 }
101
102 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
103 storage_->deallocate( sctx);
104 }
105 };
106
107 typedef basic_pooled_fixedsize_stack< stack_traits > pooled_fixedsize_stack;
108
109 }}
110
111 #ifdef BOOST_HAS_ABI_HEADERS
112 # include BOOST_ABI_SUFFIX
113 #endif
114
115 #endif // BOOST_CONTEXT_POOLED_pooled_fixedsize_H