segmented_stack_allocator.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright Oliver Kowalke 2009.
  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_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H
  6. #define BOOST_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H
  7. #include <cstddef>
  8. #include <new>
  9. #include <boost/config.hpp>
  10. #include <boost/coroutine/detail/config.hpp>
  11. #include <boost/coroutine/stack_context.hpp>
  12. #include <boost/coroutine/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 coroutines {
  28. template< typename traitsT >
  29. struct basic_segmented_stack_allocator
  30. {
  31. typedef traitsT traits_type;
  32. void allocate( stack_context & ctx, std::size_t size = traits_type::minimum_size() )
  33. {
  34. void * limit = __splitstack_makecontext( size, ctx.segments_ctx, & ctx.size);
  35. if ( ! limit) throw std::bad_alloc();
  36. // ctx.size is already filled by __splitstack_makecontext
  37. ctx.sp = static_cast< char * >( limit) + ctx.size;
  38. int off = 0;
  39. __splitstack_block_signals_context( ctx.segments_ctx, & off, 0);
  40. }
  41. void deallocate( stack_context & ctx)
  42. { __splitstack_releasecontext( ctx.segments_ctx); }
  43. };
  44. typedef basic_segmented_stack_allocator< stack_traits > segmented_stack_allocator;
  45. }}
  46. #ifdef BOOST_HAS_ABI_HEADERS
  47. # include BOOST_ABI_SUFFIX
  48. #endif
  49. #endif // BOOST_COROUTINES_SEGMENTED_STACK_ALLOCATOR_H