File indexing completed on 2025-01-18 09:30:24
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
0008 #define BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
0009
0010 extern "C" {
0011 #include <fcntl.h>
0012 #include <sys/mman.h>
0013 #include <sys/stat.h>
0014 #include <unistd.h>
0015 }
0016
0017 #include <cmath>
0018 #include <cstddef>
0019 #include <new>
0020
0021 #include <boost/assert.hpp>
0022 #include <boost/config.hpp>
0023 #include <boost/core/ignore_unused.hpp>
0024
0025 #include <boost/context/detail/config.hpp>
0026 #include <boost/context/stack_context.hpp>
0027 #include <boost/context/stack_traits.hpp>
0028
0029 #if defined(BOOST_USE_VALGRIND)
0030 #include <valgrind/valgrind.h>
0031 #endif
0032
0033 #ifdef BOOST_HAS_ABI_HEADERS
0034 # include BOOST_ABI_PREFIX
0035 #endif
0036
0037 namespace boost {
0038 namespace context {
0039
0040 template< typename traitsT >
0041 class basic_protected_fixedsize_stack {
0042 private:
0043 std::size_t size_;
0044
0045 public:
0046 typedef traitsT traits_type;
0047
0048 basic_protected_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
0049 size_( size) {
0050 }
0051
0052 stack_context allocate() {
0053
0054 const std::size_t pages = (size_ + traits_type::page_size() - 1) / traits_type::page_size();
0055
0056 const std::size_t size__ = ( pages + 1) * traits_type::page_size();
0057
0058 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
0059 void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
0060 #elif defined(MAP_ANON)
0061 void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
0062 #else
0063 void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0064 #endif
0065 if ( MAP_FAILED == vp) throw std::bad_alloc();
0066
0067
0068 const int result( ::mprotect( vp, traits_type::page_size(), PROT_NONE) );
0069 boost::ignore_unused(result);
0070 BOOST_ASSERT( 0 == result);
0071
0072 stack_context sctx;
0073 sctx.size = size__;
0074 sctx.sp = static_cast< char * >( vp) + sctx.size;
0075 #if defined(BOOST_USE_VALGRIND)
0076 sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
0077 #endif
0078 return sctx;
0079 }
0080
0081 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
0082 BOOST_ASSERT( sctx.sp);
0083
0084 #if defined(BOOST_USE_VALGRIND)
0085 VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
0086 #endif
0087
0088 void * vp = static_cast< char * >( sctx.sp) - sctx.size;
0089
0090 ::munmap( vp, sctx.size);
0091 }
0092 };
0093
0094 typedef basic_protected_fixedsize_stack< stack_traits > protected_fixedsize_stack;
0095
0096 }}
0097
0098 #ifdef BOOST_HAS_ABI_HEADERS
0099 # include BOOST_ABI_SUFFIX
0100 #endif
0101
0102 #endif