Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / context / posix / protected_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_PROTECTED_FIXEDSIZE_H
8 #define BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
9
10 extern "C" {
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 }
16
17 #include <cmath>
18 #include <cstddef>
19 #include <new>
20
21 #include <boost/assert.hpp>
22 #include <boost/config.hpp>
23
24 #include <boost/context/detail/config.hpp>
25 #include <boost/context/stack_context.hpp>
26 #include <boost/context/stack_traits.hpp>
27
28 #if defined(BOOST_USE_VALGRIND)
29 #include <valgrind/valgrind.h>
30 #endif
31
32 #ifdef BOOST_HAS_ABI_HEADERS
33 #  include BOOST_ABI_PREFIX
34 #endif
35
36 namespace boost {
37 namespace context {
38
39 template< typename traitsT >
40 class basic_protected_fixedsize_stack {
41 private:
42     std::size_t     size_;
43
44 public:
45     typedef traitsT traits_type;
46
47     basic_protected_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
48         size_( size) {
49     }
50
51     stack_context allocate() {
52         // calculate how many pages are required
53         const std::size_t pages(        
54             static_cast< std::size_t >(
55                 std::ceil(
56                     static_cast< float >( size_) / traits_type::page_size() ) ) );
57         // add one page at bottom that will be used as guard-page
58         const std::size_t size__ = ( pages + 1) * traits_type::page_size();
59
60 #if defined(BOOST_CONTEXT_USE_MAP_STACK)
61         void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_STACK, -1, 0);
62 #elif defined(MAP_ANON)
63         void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
64 #else
65         void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
66 #endif
67         if ( MAP_FAILED == vp) throw std::bad_alloc();
68
69         // conforming to POSIX.1-2001
70 #if defined(BOOST_DISABLE_ASSERTS)
71         ::mprotect( vp, traits_type::page_size(), PROT_NONE);
72 #else
73         const int result( ::mprotect( vp, traits_type::page_size(), PROT_NONE) );
74         BOOST_ASSERT( 0 == result);
75 #endif
76
77         stack_context sctx;
78         sctx.size = size__;
79         sctx.sp = static_cast< char * >( vp) + sctx.size;
80 #if defined(BOOST_USE_VALGRIND)
81         sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
82 #endif
83         return sctx;
84     }
85
86     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
87         BOOST_ASSERT( sctx.sp);
88
89 #if defined(BOOST_USE_VALGRIND)
90         VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
91 #endif
92
93         void * vp = static_cast< char * >( sctx.sp) - sctx.size;
94         // conform to POSIX.4 (POSIX.1b-1993, _POSIX_C_SOURCE=199309L)
95         ::munmap( vp, sctx.size);
96     }
97 };
98
99 typedef basic_protected_fixedsize_stack< stack_traits > protected_fixedsize_stack;
100
101 }}
102
103 #ifdef BOOST_HAS_ABI_HEADERS
104 #  include BOOST_ABI_SUFFIX
105 #endif
106
107 #endif // BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H