2
0
mirror of https://github.com/boostorg/fiber.git synced 2026-01-29 07:32:11 +00:00
Files
fiber/performance/preallocated_stack_allocator.hpp
2015-02-06 18:29:26 +01:00

62 lines
1.4 KiB
C++

// 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)
#ifndef BOOST_FIBER_PREALLOCATED_STACK_ALLOCATOR_H
#define BOOST_FIBER_PREALLOCATED_STACK_ALLOCATOR_H
#include <cstddef>
#include <cstdlib>
#include <stdexcept>
#include <vector>
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/context/stack_context.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_PREFIX
#endif
class preallocated_stack_allocator
{
private:
typedef std::vector< boost::context::stack_context > cont_t;
cont_t stacks_;
std::size_t idx_;
public:
preallocated_stack_allocator( std::size_t size = 1) :
stacks_(), idx_( 0)
{
boost::context::fixedsize_stack allocator;
for ( std::size_t i = 0; i < size; ++i)
{
stacks_.push_back( allocator.allocate() );
}
}
boost::context::stack_context allocate()
{
boost::context::stack_context ctx;
ctx.sp = stacks_[idx_].sp;
ctx.size = stacks_[idx_].size;
++idx_;
return ctx;
}
void deallocate( boost::context::stack_context & ctx)
{
}
};
#ifdef BOOST_HAS_ABI_HEADERS
# include BOOST_ABI_SUFFIX
#endif
#endif // BOOST_FIBER_PREALLOCATED_STACK_ALLOCATOR_H