Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / context / windows / 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_FIXEDSIZE_H
8 #define BOOST_CONTEXT_FIXEDSIZE_H
9
10 extern "C" {
11 #include <windows.h>
12 }
13
14 #include <cmath>
15 #include <cstddef>
16 #include <new>
17
18 #include <boost/config.hpp>
19
20 #include <boost/context/detail/config.hpp>
21 #include <boost/context/stack_context.hpp>
22 #include <boost/context/stack_traits.hpp>
23
24 #ifdef BOOST_HAS_ABI_HEADERS
25 #  include BOOST_ABI_PREFIX
26 #endif
27
28 namespace boost {
29 namespace context {
30
31 template< typename traitsT >
32 class basic_fixedsize_stack {
33 private:
34     std::size_t     size_;
35
36 public:
37     typedef traitsT traits_type;
38
39     basic_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
40         size_( size) {
41     }
42
43     stack_context allocate() {
44         // page at bottom will be used as guard-page
45         const std::size_t pages(
46             static_cast< std::size_t >( 
47                 std::floor(
48                     static_cast< float >( size_) / traits_type::page_size() ) ) );
49         BOOST_ASSERT_MSG( 1 <= pages, "at least one page must fit into stack");
50         const std::size_t size__( pages * traits_type::page_size() );
51         BOOST_ASSERT( 0 != size_ && 0 != size__);
52         BOOST_ASSERT( size__ <= size_);
53
54         void * vp = ::VirtualAlloc( 0, size__, MEM_COMMIT, PAGE_READWRITE);
55         if ( ! vp) throw std::bad_alloc();
56
57         stack_context sctx;
58         sctx.size = size__;
59         sctx.sp = static_cast< char * >( vp) + sctx.size;
60         return sctx;
61     }
62
63     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
64         BOOST_ASSERT( sctx.sp);
65
66         void * vp = static_cast< char * >( sctx.sp) - sctx.size;
67         ::VirtualFree( vp, 0, MEM_RELEASE);
68     }
69 };
70
71 typedef basic_fixedsize_stack< stack_traits > fixedsize_stack;
72 typedef fixedsize_stack default_stack;
73
74 }}
75
76 #ifdef BOOST_HAS_ABI_HEADERS
77 #  include BOOST_ABI_SUFFIX
78 #endif
79
80 #endif // BOOST_CONTEXT_FIXEDSIZE_H