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 <iostream>
9
10 #include <boost/assert.hpp>
11 #include <boost/context/all.hpp>
12
13 #include "simple_stack_allocator.hpp"
14
15 namespace ctx = boost::context;
16
17 typedef ctx::simple_stack_allocator<
18 8 * 1024 * 1024, // 8MB
19 64 * 1024, // 64kB
20 8 * 1024 // 8kB
21 > stack_allocator;
22
23 ctx::fcontext_t fcm = 0;
24 ctx::fcontext_t fc1 = 0;
25 ctx::fcontext_t fc2 = 0;
26
f1(intptr_t)27 void f1( intptr_t)
28 {
29 std::cout << "f1: entered" << std::endl;
30 std::cout << "f1: call jump_fcontext( & fc1, fc2, 0)" << std::endl;
31 ctx::jump_fcontext( & fc1, fc2, 0);
32 std::cout << "f1: return" << std::endl;
33 }
34
f2(intptr_t)35 void f2( intptr_t)
36 {
37 std::cout << "f2: entered" << std::endl;
38 std::cout << "f2: call jump_fcontext( & fc2, fc1, 0)" << std::endl;
39 ctx::jump_fcontext( & fc2, fc1, 0);
40 BOOST_ASSERT( false && ! "f2: never returns");
41 }
42
main(int argc,char * argv[])43 int main( int argc, char * argv[])
44 {
45 std::cout << "size: 0x" << std::hex << sizeof( ctx::fcontext_t) << std::endl;
46
47 stack_allocator alloc;
48
49 void * sp1 = alloc.allocate( stack_allocator::default_stacksize());
50 fc1 = ctx::make_fcontext( sp1, stack_allocator::default_stacksize(), f1);
51
52 void * sp2 = alloc.allocate( stack_allocator::default_stacksize());
53 fc2 = ctx::make_fcontext( sp2, stack_allocator::default_stacksize(), f2);
54
55 std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl;
56 ctx::jump_fcontext( & fcm, fc1, 0);
57
58 std::cout << "main: done" << std::endl;
59 BOOST_ASSERT( false && ! "main: never returns");
60
61 return EXIT_SUCCESS;
62 }
63