1 
2 //          Copyright Oliver Kowalke 2009.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <cstdlib>
8 #include <cstring>
9 #include <iostream>
10 
11 #include <boost/assert.hpp>
12 #include <boost/context/all.hpp>
13 
14 #include "simple_stack_allocator.hpp"
15 
16 namespace ctx = boost::context;
17 
18 typedef ctx::simple_stack_allocator<
19     8 * 1024 * 1024, // 8MB
20     64 * 1024, // 64kB
21     8 * 1024 // 8kB
22 >       stack_allocator;
23 
24 ctx::fcontext_t fcm = 0;
25 ctx::fcontext_t fc1 = 0;
26 ctx::fcontext_t fc2 = 0;
27 
f2(intptr_t)28 void f2( intptr_t)
29 {
30     std::cout << "f2: entered" << std::endl;
31     ctx::jump_fcontext( & fc2, fc1, 0);
32 }
33 
f1(intptr_t)34 void f1( intptr_t)
35 {
36     std::cout << "f1: entered" << std::endl;
37 
38     stack_allocator alloc;
39     void * sp = alloc.allocate( stack_allocator::default_stacksize());
40     fc2 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f2);
41     ctx::jump_fcontext( & fc1, fc2, 0);
42 
43     ctx::jump_fcontext( & fc1, fcm, 0);
44 }
45 
main(int argc,char * argv[])46 int main( int argc, char * argv[])
47 {
48     stack_allocator alloc;
49     void * sp = alloc.allocate( stack_allocator::default_stacksize());
50     fc1 = ctx::make_fcontext( sp, stack_allocator::default_stacksize(), f1);
51 
52     std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl;
53     ctx::jump_fcontext( & fcm, fc1, 0);
54 
55     std::cout << "main: done" << std::endl;
56 
57     return EXIT_SUCCESS;
58 }
59