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_PROTECTED_FIXEDSIZE_H
8 #define BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
18 #include <boost/config.hpp>
20 #include <boost/context/detail/config.hpp>
21 #include <boost/context/stack_context.hpp>
22 #include <boost/context/stack_traits.hpp>
24 #ifdef BOOST_HAS_ABI_HEADERS
25 # include BOOST_ABI_PREFIX
31 template< typename traitsT >
32 class basic_protected_fixedsize_stack {
37 typedef traitsT traits_type;
39 basic_protected_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
43 stack_context allocate() {
44 // calculate how many pages are required
45 const std::size_t pages(
46 static_cast< std::size_t >(
48 static_cast< float >( size_) / traits_type::page_size() ) ) );
49 // add one page at bottom that will be used as guard-page
50 const std::size_t size__ = ( pages + 1) * traits_type::page_size();
52 void * vp = ::VirtualAlloc( 0, size__, MEM_COMMIT, PAGE_READWRITE);
53 if ( ! vp) throw std::bad_alloc();
56 #if defined(BOOST_DISABLE_ASSERTS)
58 vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
60 const BOOL result = ::VirtualProtect(
61 vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
62 BOOST_ASSERT( FALSE != result);
67 sctx.sp = static_cast< char * >( vp) + sctx.size;
71 void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
72 BOOST_ASSERT( sctx.sp);
74 void * vp = static_cast< char * >( sctx.sp) - sctx.size;
75 ::VirtualFree( vp, 0, MEM_RELEASE);
79 typedef basic_protected_fixedsize_stack< stack_traits > protected_fixedsize_stack;
83 #ifdef BOOST_HAS_ABI_HEADERS
84 # include BOOST_ABI_SUFFIX
87 #endif // BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H