segmented_stack.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright Oliver Kowalke 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_CONTEXT_SEGMENTED_H
  6. #define BOOST_CONTEXT_SEGMENTED_H
  7. #include <cstddef>
  8. #include <new>
  9. #include <boost/config.hpp>
  10. #include <boost/context/detail/config.hpp>
  11. #include <boost/context/stack_context.hpp>
  12. #include <boost/context/stack_traits.hpp>
  13. #ifdef BOOST_HAS_ABI_HEADERS
  14. # include BOOST_ABI_PREFIX
  15. #endif
  16. // forward declaration for splitstack-functions defined in libgcc
  17. extern "C" {
  18. void *__splitstack_makecontext( std::size_t,
  19. void * [BOOST_CONTEXT_SEGMENTS],
  20. std::size_t *);
  21. void __splitstack_releasecontext( void * [BOOST_CONTEXT_SEGMENTS]);
  22. void __splitstack_resetcontext( void * [BOOST_CONTEXT_SEGMENTS]);
  23. void __splitstack_block_signals_context( void * [BOOST_CONTEXT_SEGMENTS],
  24. int * new_value, int * old_value);
  25. }
  26. namespace boost {
  27. namespace context {
  28. template< typename traitsT >
  29. class basic_segmented_stack {
  30. private:
  31. std::size_t size_;
  32. public:
  33. typedef traitsT traits_type;
  34. basic_segmented_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
  35. size_( size) {
  36. }
  37. stack_context allocate() {
  38. stack_context sctx;
  39. void * vp = __splitstack_makecontext( size_, sctx.segments_ctx, & sctx.size);
  40. if ( ! vp) throw std::bad_alloc();
  41. // sctx.size is already filled by __splitstack_makecontext
  42. sctx.sp = static_cast< char * >( vp) + sctx.size;
  43. int off = 0;
  44. __splitstack_block_signals_context( sctx.segments_ctx, & off, 0);
  45. return sctx;
  46. }
  47. void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  48. __splitstack_releasecontext( sctx.segments_ctx);
  49. }
  50. };
  51. typedef basic_segmented_stack< stack_traits > segmented_stack;
  52. # if defined(BOOST_USE_SEGMENTED_STACKS)
  53. typedef segmented_stack default_stack;
  54. # endif
  55. }}
  56. #ifdef BOOST_HAS_ABI_HEADERS
  57. # include BOOST_ABI_SUFFIX
  58. #endif
  59. #endif // BOOST_CONTEXT_SEGMENTED_H