segmented_stack.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include <boost/coroutine/all.hpp>
  6. #include <iostream>
  7. #include <boost/assert.hpp>
  8. #include <boost/config.hpp>
  9. int count = 384;
  10. #ifdef BOOST_MSVC //MS VisualStudio
  11. __declspec(noinline) void access( char *buf);
  12. #else // GCC
  13. void access( char *buf) __attribute__ ((noinline));
  14. #endif
  15. void access( char *buf)
  16. {
  17. buf[0] = '\0';
  18. }
  19. void bar( int i)
  20. {
  21. char buf[4 * 1024];
  22. if ( i > 0)
  23. {
  24. access( buf);
  25. std::cout << i << ". iteration" << std::endl;
  26. bar( i - 1);
  27. }
  28. }
  29. void foo( boost::coroutines::asymmetric_coroutine< void >::pull_type & source)
  30. {
  31. bar( count);
  32. source();
  33. }
  34. int main( int argc, char * argv[])
  35. {
  36. #if defined(BOOST_USE_SEGMENTED_STACKS)
  37. std::cout << "using segmented stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  38. std::cout << "initial stack size = " << boost::coroutines::stack_allocator::traits_type::default_size() / 1024 << "kB" << std::endl;
  39. std::cout << "application should not fail" << std::endl;
  40. #else
  41. std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  42. std::cout << "initial stack size = " << boost::coroutines::stack_allocator::traits_type::default_size() / 1024 << "kB" << std::endl;
  43. std::cout << "application might fail" << std::endl;
  44. #endif
  45. boost::coroutines::asymmetric_coroutine< void >::push_type sink( foo);
  46. sink();
  47. std::cout << "Done" << std::endl;
  48. return 0;
  49. }