standard_stack_allocator.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_STANDARD_STACK_ALLOCATOR_H
  6. #define BOOST_COROUTINES_STANDARD_STACK_ALLOCATOR_H
  7. #if defined(BOOST_USE_VALGRIND)
  8. #include <valgrind/valgrind.h>
  9. #endif
  10. #include <cstddef>
  11. #include <cstdlib>
  12. #include <new>
  13. #include <boost/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/coroutine/detail/config.hpp>
  16. #include <boost/coroutine/stack_context.hpp>
  17. #include <boost/coroutine/stack_traits.hpp>
  18. #ifdef BOOST_HAS_ABI_HEADERS
  19. # include BOOST_ABI_PREFIX
  20. #endif
  21. namespace boost {
  22. namespace coroutines {
  23. template< typename traitsT >
  24. struct basic_standard_stack_allocator
  25. {
  26. typedef traitsT traits_type;
  27. void allocate( stack_context & ctx, std::size_t size = traits_type::minimum_size() )
  28. {
  29. BOOST_ASSERT( traits_type::minimum_size() <= size);
  30. BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= size) );
  31. void * limit = std::malloc( size);
  32. if ( ! limit) throw std::bad_alloc();
  33. ctx.size = size;
  34. ctx.sp = static_cast< char * >( limit) + ctx.size;
  35. #if defined(BOOST_USE_VALGRIND)
  36. ctx.valgrind_stack_id = VALGRIND_STACK_REGISTER( ctx.sp, limit);
  37. #endif
  38. }
  39. void deallocate( stack_context & ctx)
  40. {
  41. BOOST_ASSERT( ctx.sp);
  42. BOOST_ASSERT( traits_type::minimum_size() <= ctx.size);
  43. BOOST_ASSERT( traits_type::is_unbounded() || ( traits_type::maximum_size() >= ctx.size) );
  44. #if defined(BOOST_USE_VALGRIND)
  45. VALGRIND_STACK_DEREGISTER( ctx.valgrind_stack_id);
  46. #endif
  47. void * limit = static_cast< char * >( ctx.sp) - ctx.size;
  48. std::free( limit);
  49. }
  50. };
  51. typedef basic_standard_stack_allocator< stack_traits > standard_stack_allocator;
  52. }}
  53. #ifdef BOOST_HAS_ABI_HEADERS
  54. # include BOOST_ABI_SUFFIX
  55. #endif
  56. #endif // BOOST_COROUTINES_STANDARD_STACK_ALLOCATOR_H