BVB Source Codes

mars Show protected_fixedsize_stack.hpp Source code

Return Download mars: download protected_fixedsize_stack.hpp Source code - Download mars Source code - Type:.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 <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 mars_boost {} namespace boost = mars_boost; namespace mars_boost {
  29. namespace context {
  30.  
  31. template< typename traitsT >
  32. class basic_protected_fixedsize_stack {
  33. private:
  34.     std::size_t     size_;
  35.  
  36. public:
  37.     typedef traitsT traits_type;
  38.  
  39.     basic_protected_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
  40.         size_( size) {
  41.         BOOST_ASSERT( traits_type::minimum_size() <= size_);
  42.         BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= size_) );
  43.     }
  44.  
  45.     stack_context allocate() {
  46.         // page at bottom will be used as guard-page
  47.         const std::size_t pages(
  48.             static_cast< std::size_t >(
  49.                 std::floor(
  50.                     static_cast< float >( size_) / traits_type::page_size() ) ) );
  51.         BOOST_ASSERT_MSG( 2 <= pages, "at least two pages must fit into stack (one page is guard-page)");
  52.         const std::size_t size__( pages * traits_type::page_size() );
  53.         BOOST_ASSERT( 0 != size_ && 0 != size__);
  54.         BOOST_ASSERT( size__ <= size_);
  55.  
  56.         void * vp = ::VirtualAlloc( 0, size__, MEM_COMMIT, PAGE_READWRITE);
  57.         if ( ! vp) throw std::bad_alloc();
  58.  
  59.         DWORD old_options;
  60. #if defined(BOOST_DISABLE_ASSERTS)
  61.         ::VirtualProtect(
  62.             vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
  63. #else
  64.         const BOOL result = ::VirtualProtect(
  65.             vp, traits_type::page_size(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options);
  66.         BOOST_ASSERT( FALSE != result);
  67. #endif
  68.  
  69.         stack_context sctx;
  70.         sctx.size = size__;
  71.         sctx.sp = static_cast< char * >( vp) + sctx.size;
  72.         return sctx;
  73.     }
  74.  
  75.     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  76.         BOOST_ASSERT( sctx.sp);
  77.         BOOST_ASSERT( traits_type::minimum_size() <= sctx.size);
  78.         BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
  79.  
  80.         void * vp = static_cast< char * >( sctx.sp) - sctx.size;
  81.         ::VirtualFree( vp, 0, MEM_RELEASE);
  82.     }
  83. };
  84.  
  85. typedef basic_protected_fixedsize_stack< stack_traits > protected_fixedsize_stack;
  86.  
  87. }}
  88.  
  89. #ifdef BOOST_HAS_ABI_HEADERS
  90. #  include BOOST_ABI_SUFFIX
  91. #endif
  92.  
  93. #endif // BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
  94.  
downloadprotected_fixedsize_stack.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top