fixedsize_stack.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_FIXEDSIZE_H
  6. #define BOOST_CONTEXT_FIXEDSIZE_H
  7. #include <cstddef>
  8. #include <cstdlib>
  9. #include <new>
  10. #include <boost/assert.hpp>
  11. #include <boost/config.hpp>
  12. #include <boost/context/detail/config.hpp>
  13. #include <boost/context/stack_context.hpp>
  14. #include <boost/context/stack_traits.hpp>
  15. #if defined(BOOST_USE_VALGRIND)
  16. #include <valgrind/valgrind.h>
  17. #endif
  18. #ifdef BOOST_HAS_ABI_HEADERS
  19. # include BOOST_ABI_PREFIX
  20. #endif
  21. namespace boost {
  22. namespace context {
  23. template< typename traitsT >
  24. class basic_fixedsize_stack {
  25. private:
  26. std::size_t size_;
  27. public:
  28. typedef traitsT traits_type;
  29. basic_fixedsize_stack( std::size_t size = traits_type::default_size() ) BOOST_NOEXCEPT_OR_NOTHROW :
  30. size_( size) {
  31. }
  32. stack_context allocate() {
  33. void * vp = std::malloc( size_);
  34. if ( ! vp) {
  35. throw std::bad_alloc();
  36. }
  37. stack_context sctx;
  38. sctx.size = size_;
  39. sctx.sp = static_cast< char * >( vp) + sctx.size;
  40. #if defined(BOOST_USE_VALGRIND)
  41. sctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( sctx.sp, vp);
  42. #endif
  43. return sctx;
  44. }
  45. void deallocate( stack_context & sctx) BOOST_NOEXCEPT_OR_NOTHROW {
  46. BOOST_ASSERT( sctx.sp);
  47. #if defined(BOOST_USE_VALGRIND)
  48. VALGRIND_STACK_DEREGISTER( sctx.valgrind_stack_id);
  49. #endif
  50. void * vp = static_cast< char * >( sctx.sp) - sctx.size;
  51. std::free( vp);
  52. }
  53. };
  54. typedef basic_fixedsize_stack< stack_traits > fixedsize_stack;
  55. # if ! defined(BOOST_USE_SEGMENTED_STACKS)
  56. typedef fixedsize_stack default_stack;
  57. # endif
  58. }}
  59. #ifdef BOOST_HAS_ABI_HEADERS
  60. # include BOOST_ABI_SUFFIX
  61. #endif
  62. #endif // BOOST_CONTEXT_FIXEDSIZE_H