Skip to content

Instantly share code, notes, and snippets.

@chfast
Created June 2, 2015 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chfast/bfc1e1af4176960b4722 to your computer and use it in GitHub Desktop.
Save chfast/bfc1e1af4176960b4722 to your computer and use it in GitHub Desktop.
boost context
// Copyright Oliver Kowalke 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/assert.hpp>
#include <boost/context/all.hpp>
#ifndef BOOST_CONTEXT_SIMPLE_STACK_ALLOCATOR_H
#define BOOST_CONTEXT_SIMPLE_STACK_ALLOCATOR_H
#include <cstddef>
#include <cstdlib>
#include <stdexcept>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/context/detail/config.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
namespace boost {
namespace context {
template< std::size_t Max, std::size_t Default, std::size_t Min >
class simple_stack_allocator
{
public:
static std::size_t maximum_stacksize()
{ return Max; }
static std::size_t default_stacksize()
{ return Default; }
static std::size_t minimum_stacksize()
{ return Min; }
void * allocate( std::size_t size) const
{
BOOST_ASSERT( minimum_stacksize() <= size);
BOOST_ASSERT( maximum_stacksize() >= size);
void * limit = std::malloc( size);
if ( ! limit) throw std::bad_alloc();
return static_cast< char * >( limit) + size;
}
void deallocate( void * vp, std::size_t size) const
{
BOOST_ASSERT( vp);
BOOST_ASSERT( minimum_stacksize() <= size);
BOOST_ASSERT( maximum_stacksize() >= size);
void * limit = static_cast< char * >( vp) - size;
std::free( limit);
}
};
}}
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#endif // BOOST_CONTEXT_SIMPLE_STACK_ALLOCATOR_H
// Copyright Oliver Kowalke 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
namespace ctx = boost::context;
typedef ctx::simple_stack_allocator<
8 * 1024 * 1024, // 8MB
64 * 1024, // 64kB
8 * 1024 // 8kB
> stack_allocator;
ctx::fcontext_t * fc1;
ctx::fcontext_t * fc2;
ctx::fcontext_t * fc3;
void f1( intptr_t)
{
std::cout << "f1: entered" << std::endl;
std::cout << "f1: call jump_fcontext( fc1, fc2, 0)" << std::endl;
ctx::jump_fcontext( fc1, fc2, 0);
std::cout << "f1: return" << std::endl;
}
void f2( intptr_t)
{
std::cout << "f2: entered" << std::endl;
std::cout << "f2: call jump_fcontext( fc2, fc1, 0)" << std::endl;
ctx::jump_fcontext( fc2, fc1, 0);
BOOST_ASSERT( false && ! "f2: never returns");
}
int main( int argc, char * argv[])
{
std::cout << "size: 0x" << std::hex << sizeof( ctx::fcontext_t) << std::endl;
ctx::fcontext_t fcm;
fc3 = &fcm;
stack_allocator alloc;
void * sp1 = alloc.allocate( stack_allocator::default_stacksize());
fc1 = ctx::make_fcontext( sp1, stack_allocator::default_stacksize(), f1);
void * sp2 = alloc.allocate( stack_allocator::default_stacksize());
fc2 = ctx::make_fcontext( sp2, stack_allocator::default_stacksize(), f2);
std::cout << "main: call start_fcontext( & fcm, fc1, 0)" << std::endl;
ctx::jump_fcontext( & fcm, fc1, 0);
std::cout << "main: done" << std::endl;
alloc.deallocate(sp2, stack_allocator::default_stacksize());
alloc.deallocate(sp1, stack_allocator::default_stacksize());
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment