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 <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 mars_boost {} namespace boost = mars_boost; namespace mars_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.         BOOST_ASSERT( traits_type::minimum_size() <= size_);
  50.         BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= size_) );
  51.     }
  52.  
  53.     stack_context allocate() {
  54.         // page at bottom will be used as guard-page
  55.         const std::size_t pages(
  56.             static_cast< std::size_t >(
  57.                 std::floor(
  58.                     static_cast< float >( size_) / traits_type::page_size() ) ) );
  59.         BOOST_ASSERT_MSG( 2 <= pages, "at least two pages must fit into stack (one page is guard-page)");
  60.         const std::size_t size__( pages * traits_type::page_size() );
  61.         BOOST_ASSERT( 0 != size_ && 0 != size__);
  62.         BOOST_ASSERT( size__ <= size_);
  63.  
  64.         // conform to POSIX.4 (POSIX.1b-1993, _POSIX_C_SOURCE=199309L)
  65. #if defined(MAP_ANON)
  66.         void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  67. #else
  68.         void * vp = ::mmap( 0, size__, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  69. #endif
  70.         if ( MAP_FAILED == vp) throw std::bad_alloc();
  71.  
  72.         // conforming to POSIX.1-2001
  73. #if defined(BOOST_DISABLE_ASSERTS)
  74.         ::mprotect( vp, traits_type::page_size(), PROT_NONE);
  75. #else
  76.         const int result( ::mprotect( vp, traits_type::page_size(), PROT_NONE) );
  77.         BOOST_ASSERT( 0 == result);
  78. #endif
  79.  
  80.         stack_context sctx;
  81.         sctx.size = size__;
  82.         sctx.sp = static_cast< char * >( vp) + sctx.size;
  83. #if defined(BOOST_USE_VALGRIND)
  84.         sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
  85. #endif
  86.         return sctx;
  87.     }
  88.  
  89.     void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  90.         BOOST_ASSERT( sctx.sp);
  91.         BOOST_ASSERT( traits_type::minimum_size() <= sctx.size);
  92.         BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= sctx.size) );
  93.  
  94. #if defined(BOOST_USE_VALGRIND)
  95.         VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
  96. #endif
  97.  
  98.         void * vp = static_cast< char * >( sctx.sp) - sctx.size;
  99.         // conform to POSIX.4 (POSIX.1b-1993, _POSIX_C_SOURCE=199309L)
  100.         ::munmap( vp, sctx.size);
  101.     }
  102. };
  103.  
  104. typedef basic_protected_fixedsize_stack< stack_traits > protected_fixedsize_stack;
  105.  
  106. }}
  107.  
  108. #ifdef BOOST_HAS_ABI_HEADERS
  109. #  include BOOST_ABI_SUFFIX
  110. #endif
  111.  
  112. #endif // BOOST_CONTEXT_PROTECTED_FIXEDSIZE_H
  113.  
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