fix performance tests

This commit is contained in:
Oliver Kowalke
2014-01-27 19:52:57 +01:00
parent e74929f66a
commit 98dae1bc98
5 changed files with 67 additions and 99 deletions

View File

@@ -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