mirror of
https://github.com/boostorg/coroutine.git
synced 2026-02-16 01:12:26 +00:00
fix performance tests
This commit is contained in:
@@ -14,89 +14,46 @@
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/coroutine/protected_stack_allocator.hpp>
|
||||
#include <boost/coroutine/stack_context.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/utility.hpp>
|
||||
|
||||
#include <boost/fiber/detail/config.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
# include BOOST_ABI_PREFIX
|
||||
#endif
|
||||
|
||||
template< std::size_t Default >
|
||||
class preallocated_stack_allocator
|
||||
{
|
||||
private:
|
||||
class impl : private boost::noncopyable
|
||||
{
|
||||
private:
|
||||
std::size_t pos_;
|
||||
std::vector< void * > stacks_;
|
||||
|
||||
public:
|
||||
impl( std::size_t count) :
|
||||
pos_( 0),
|
||||
stacks_()
|
||||
{
|
||||
for ( int i = 0; i < count; ++i)
|
||||
{
|
||||
void * limit = std::calloc( preallocated_stack_allocator::default_stacksize(), sizeof( char) );
|
||||
if ( ! limit) throw std::bad_alloc();
|
||||
limit = static_cast< char * >( limit) + preallocated_stack_allocator::default_stacksize();
|
||||
stacks_.push_back( limit);
|
||||
}
|
||||
}
|
||||
|
||||
~impl()
|
||||
{
|
||||
for ( int i = 0; i < stacks_.size(); ++i)
|
||||
{
|
||||
void * limit = static_cast< char * >( stacks_[i]);
|
||||
limit = static_cast< char * >( limit) - preallocated_stack_allocator::default_stacksize();
|
||||
std::free( limit);
|
||||
}
|
||||
}
|
||||
|
||||
void allocate( boost::coroutines::stack_context & ctx, std::size_t size)
|
||||
{
|
||||
BOOST_ASSERT( stacks_.size() > pos_);
|
||||
BOOST_ASSERT( preallocated_stack_allocator::minimum_stacksize() <= size);
|
||||
BOOST_ASSERT( preallocated_stack_allocator::maximum_stacksize() >= size);
|
||||
|
||||
ctx.size = preallocated_stack_allocator::default_stacksize();
|
||||
ctx.sp = stacks_[pos_++];
|
||||
}
|
||||
|
||||
void deallocate( boost::coroutines::stack_context & ctx)
|
||||
{
|
||||
BOOST_ASSERT( ctx.sp);
|
||||
BOOST_ASSERT( preallocated_stack_allocator::minimum_stacksize() <= ctx.size);
|
||||
BOOST_ASSERT( preallocated_stack_allocator::maximum_stacksize() >= ctx.size);
|
||||
}
|
||||
};
|
||||
|
||||
boost::shared_ptr< impl > p_;
|
||||
boost::coroutines::stack_context stack_ctx_;
|
||||
|
||||
public:
|
||||
static std::size_t maximum_stacksize()
|
||||
{ return Default; }
|
||||
|
||||
static std::size_t default_stacksize()
|
||||
{ return Default; }
|
||||
|
||||
static std::size_t minimum_stacksize()
|
||||
{ return Default; }
|
||||
|
||||
preallocated_stack_allocator( std::size_t count = 1) :
|
||||
p_( new impl( count) )
|
||||
{}
|
||||
preallocated_stack_allocator() :
|
||||
stack_ctx_()
|
||||
{
|
||||
boost::coroutines::protected_stack_allocator allocator;
|
||||
allocator.allocate( stack_ctx_, default_stacksize() );
|
||||
}
|
||||
|
||||
void allocate( boost::coroutines::stack_context & ctx, std::size_t size)
|
||||
{ p_->allocate( ctx, size); }
|
||||
{
|
||||
ctx.sp = stack_ctx_.sp;
|
||||
ctx.size = stack_ctx_.size;
|
||||
}
|
||||
|
||||
void deallocate( boost::coroutines::stack_context & ctx)
|
||||
{ p_->deallocate( ctx); }
|
||||
{
|
||||
}
|
||||
|
||||
static std::size_t maximum_stacksize()
|
||||
{ return boost::coroutines::protected_stack_allocator::maximum_stacksize(); }
|
||||
|
||||
static std::size_t default_stacksize()
|
||||
{ return boost::coroutines::protected_stack_allocator::default_stacksize(); }
|
||||
|
||||
static std::size_t minimum_stacksize()
|
||||
{ return boost::coroutines::protected_stack_allocator::minimum_stacksize(); }
|
||||
};
|
||||
|
||||
#ifdef BOOST_HAS_ABI_HEADERS
|
||||
|
||||
Reference in New Issue
Block a user