segmented.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. #include <cstdlib>
  6. #include <iostream>
  7. #include <memory>
  8. #include <boost/context/continuation.hpp>
  9. namespace ctx = boost::context;
  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. buf[0] = '\0';
  17. }
  18. void bar( int i) {
  19. char buf[4 * 1024];
  20. if ( i > 0) {
  21. access( buf);
  22. std::cout << i << ". iteration" << std::endl;
  23. bar( i - 1);
  24. }
  25. }
  26. int main() {
  27. int count = 100*1024;
  28. #if defined(BOOST_USE_SEGMENTED_STACKS)
  29. std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  30. std::cout << "initial stack size = " << ctx::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
  31. std::cout << "application should not fail" << std::endl;
  32. #else
  33. std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  34. std::cout << "initial stack size = " << ctx::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
  35. std::cout << "application might fail" << std::endl;
  36. #endif
  37. ctx::continuation c = ctx::callcc(
  38. [count](ctx::continuation && c){
  39. bar( count);
  40. return std::move( c);
  41. });
  42. std::cout << "main: done" << std::endl;
  43. return EXIT_SUCCESS;
  44. }