Reimplemented SmallObject Allocator to provide all 3 forms of new and

delete operators with appropriate exception safety level for each, and
greater efficiency for all.


git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@146 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
rich_sposato 2005-04-07 05:36:44 +00:00
parent 7d790e2232
commit fbb100b84c
2 changed files with 466 additions and 313 deletions

View file

@ -8,33 +8,127 @@
// purpose is hereby granted without fee, provided that the above copyright // purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this // notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation. // permission notice appear in supporting documentation.
// The author or Addison-Welsey Longman make no representations about the // The author or Addison-Wesley Longman make no representations about the
// suitability of this software for any purpose. It is provided "as is" // suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty. // without express or implied warranty.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Last update: March 20, 2001 // Last update: Nov 26, 2004
#include "SmallObj.h" #include "SmallObj.h"
#include <cassert> #include <cassert>
#include <vector>
#include <algorithm> #include <algorithm>
#include <functional>
using namespace Loki; using namespace Loki;
namespace Loki
{
////////////////////////////////////////////////////////////////////////////////
// class FixedAllocator
// Offers services for allocating fixed-sized objects
////////////////////////////////////////////////////////////////////////////////
class FixedAllocator
{
private:
struct Chunk
{
bool Init( std::size_t blockSize, unsigned char blocks );
void* Allocate(std::size_t blockSize);
void Deallocate(void* p, std::size_t blockSize);
void Reset(std::size_t blockSize, unsigned char blocks);
void Release();
inline bool HasBlock( unsigned char * p, std::size_t chunkLength ) const
{ return ( pData_ <= p ) && ( p < pData_ + chunkLength ); }
inline bool HasAvailable( unsigned char numBlocks ) const
{ return ( blocksAvailable_ == numBlocks ); }
inline bool IsFilled( void ) const
{ return ( 0 == blocksAvailable_ ); }
unsigned char* pData_;
unsigned char
firstAvailableBlock_,
blocksAvailable_;
};
// Internal functions
void DoDeallocate(void* p);
bool MakeNewChunk( void );
Chunk * VicinityFind( void * p );
/// Not implemented.
FixedAllocator(const FixedAllocator&);
/// Not implemented.
FixedAllocator& operator=(const FixedAllocator&);
// Data
std::size_t blockSize_;
unsigned char numBlocks_;
typedef std::vector<Chunk> Chunks;
typedef Chunks::iterator ChunkIter;
typedef Chunks::const_iterator ChunkCIter;
Chunks chunks_;
Chunk* allocChunk_;
Chunk* deallocChunk_;
Chunk * emptyChunk_;
public:
// Create a FixedAllocator able to manage blocks of 'blockSize' size
FixedAllocator();
~FixedAllocator();
void Initialize( std::size_t blockSize, std::size_t pageSize );
// Allocate a memory block
void * Allocate( void );
// Deallocate a memory block previously allocated with Allocate()
// (if that's not the case, the behavior is undefined)
bool Deallocate( void * p, bool doChecks );
// Returns the block size with which the FixedAllocator was initialized
inline std::size_t BlockSize() const
{ return blockSize_; }
};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// FixedAllocator::Chunk::Init // FixedAllocator::Chunk::Init
// Initializes a chunk object // Initializes a chunk object
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void FixedAllocator::Chunk::Init(std::size_t blockSize, unsigned char blocks) bool FixedAllocator::Chunk::Init( std::size_t blockSize, unsigned char blocks )
{ {
assert(blockSize > 0); assert(blockSize > 0);
assert(blocks > 0); assert(blocks > 0);
// Overflow check // Overflow check
assert((blockSize * blocks) / blockSize == blocks); const std::size_t allocSize = blockSize * blocks;
assert( allocSize / blockSize == blocks);
pData_ = new unsigned char[blockSize * blocks];
Reset(blockSize, blocks); #ifdef USE_NEW_TO_ALLOCATE
// If this new operator fails, it will throw, and the exception will get
// caught one layer up.
pData_ = new unsigned char[ allocSize ];
#else
// malloc can't throw, so its only way to indicate an error is to return
// a NULL pointer, so we have to check for that.
pData_ = static_cast< unsigned char * >( ::malloc( allocSize ) );
if ( NULL == pData_ ) return false;
#endif
Reset( blockSize, blocks );
return true;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -67,7 +161,12 @@ void FixedAllocator::Chunk::Reset(std::size_t blockSize, unsigned char blocks)
void FixedAllocator::Chunk::Release() void FixedAllocator::Chunk::Release()
{ {
delete[] pData_; assert( NULL != pData_ );
#ifdef USE_NEW_TO_ALLOCATE
delete [] pData_;
#else
::free( static_cast< void * >( pData_ ) );
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -77,16 +176,14 @@ void FixedAllocator::Chunk::Release()
void* FixedAllocator::Chunk::Allocate(std::size_t blockSize) void* FixedAllocator::Chunk::Allocate(std::size_t blockSize)
{ {
if (!blocksAvailable_) return 0; if ( IsFilled() ) return NULL;
assert((firstAvailableBlock_ * blockSize) / blockSize == assert((firstAvailableBlock_ * blockSize) / blockSize ==
firstAvailableBlock_); firstAvailableBlock_);
unsigned char * pResult = pData_ + (firstAvailableBlock_ * blockSize);
unsigned char* pResult =
pData_ + (firstAvailableBlock_ * blockSize);
firstAvailableBlock_ = *pResult; firstAvailableBlock_ = *pResult;
--blocksAvailable_; --blocksAvailable_;
return pResult; return pResult;
} }
@ -117,52 +214,12 @@ void FixedAllocator::Chunk::Deallocate(void* p, std::size_t blockSize)
// Creates a FixedAllocator object of a fixed block size // Creates a FixedAllocator object of a fixed block size
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
FixedAllocator::FixedAllocator(std::size_t blockSize) FixedAllocator::FixedAllocator()
: blockSize_(blockSize) : blockSize_( 0 )
, allocChunk_(0) , allocChunk_( NULL )
, deallocChunk_(0) , deallocChunk_( NULL )
, emptyChunk_( NULL )
{ {
assert(blockSize_ > 0);
prev_ = next_ = this;
std::size_t numBlocks = DEFAULT_CHUNK_SIZE / blockSize;
if (numBlocks > UCHAR_MAX) numBlocks = UCHAR_MAX;
else if (numBlocks == 0) numBlocks = 8 * blockSize;
numBlocks_ = static_cast<unsigned char>(numBlocks);
assert(numBlocks_ == numBlocks);
}
////////////////////////////////////////////////////////////////////////////////
// FixedAllocator::FixedAllocator(const FixedAllocator&)
// Creates a FixedAllocator object of a fixed block size
////////////////////////////////////////////////////////////////////////////////
FixedAllocator::FixedAllocator(const FixedAllocator& rhs)
: blockSize_(rhs.blockSize_)
, numBlocks_(rhs.numBlocks_)
, chunks_(rhs.chunks_)
{
prev_ = &rhs;
next_ = rhs.next_;
rhs.next_->prev_ = this;
rhs.next_ = this;
allocChunk_ = rhs.allocChunk_
? &chunks_.front() + (rhs.allocChunk_ - &rhs.chunks_.front())
: 0;
deallocChunk_ = rhs.deallocChunk_
? &chunks_.front() + (rhs.deallocChunk_ - &rhs.chunks_.front())
: 0;
}
FixedAllocator& FixedAllocator::operator=(const FixedAllocator& rhs)
{
FixedAllocator copy(rhs);
copy.Swap(*this);
return *this;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -171,35 +228,57 @@ FixedAllocator& FixedAllocator::operator=(const FixedAllocator& rhs)
FixedAllocator::~FixedAllocator() FixedAllocator::~FixedAllocator()
{ {
if (prev_ != this) for ( ChunkIter i( chunks_.begin() ); i != chunks_.end(); ++i )
{
prev_->next_ = next_;
next_->prev_ = prev_;
return;
}
assert(prev_ == next_);
Chunks::iterator i = chunks_.begin();
for (; i != chunks_.end(); ++i)
{
assert(i->blocksAvailable_ == numBlocks_);
i->Release(); i->Release();
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// FixedAllocator::Swap // FixedAllocator::Initialize
// Initializes the operational constraints for the FixedAllocator
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void FixedAllocator::Swap(FixedAllocator& rhs) void FixedAllocator::Initialize( std::size_t blockSize, std::size_t pageSize )
{ {
using namespace std; assert( blockSize > 0 );
assert( pageSize >= blockSize );
swap(blockSize_, rhs.blockSize_); blockSize_ = blockSize;
swap(numBlocks_, rhs.numBlocks_);
chunks_.swap(rhs.chunks_); std::size_t numBlocks = pageSize / blockSize;
swap(allocChunk_, rhs.allocChunk_); if (numBlocks > UCHAR_MAX) numBlocks = UCHAR_MAX;
swap(deallocChunk_, rhs.deallocChunk_); else if (numBlocks == 0) numBlocks = 8 * blockSize;
numBlocks_ = static_cast<unsigned char>(numBlocks);
assert(numBlocks_ == numBlocks);
}
////////////////////////////////////////////////////////////////////////////////
// FixedAllocator::MakeNewChunk
// Allocates a new Chunk for a FixedAllocator.
////////////////////////////////////////////////////////////////////////////////
bool FixedAllocator::MakeNewChunk( void )
{
bool allocated = false;
try
{
// Calling chunks_.reserve *before* creating and initializing the new
// Chunk means that nothing is leaked by this function in case an
// exception is thrown from reserve.
chunks_.reserve( chunks_.size() + 1 );
Chunk newChunk;
allocated = newChunk.Init( blockSize_, numBlocks_ );
if ( allocated )
chunks_.push_back( newChunk );
}
catch ( ... )
{
allocated = false;
}
if ( !allocated ) return false;
allocChunk_ = &chunks_.back();
deallocChunk_ = &chunks_.front();
return true;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -207,34 +286,42 @@ void FixedAllocator::Swap(FixedAllocator& rhs)
// Allocates a block of fixed size // Allocates a block of fixed size
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void* FixedAllocator::Allocate() void * FixedAllocator::Allocate( void )
{ {
if (allocChunk_ == 0 || allocChunk_->blocksAvailable_ == 0) // prove either emptyChunk_ points nowhere, or points to a truly empty Chunk.
assert( ( NULL == emptyChunk_ ) || ( emptyChunk_->HasAvailable( numBlocks_ ) ) );
if ( ( NULL == allocChunk_ ) || allocChunk_->IsFilled() )
{ {
Chunks::iterator i = chunks_.begin(); if ( NULL != emptyChunk_ )
for (;; ++i)
{ {
if (i == chunks_.end()) allocChunk_ = emptyChunk_;
emptyChunk_ = NULL;
}
else
{
for ( ChunkIter i( chunks_.begin() ); ; ++i )
{ {
// Initialize if ( chunks_.end() == i )
chunks_.reserve(chunks_.size() + 1); {
Chunk newChunk; if ( !MakeNewChunk() )
newChunk.Init(blockSize_, numBlocks_); return NULL;
chunks_.push_back(newChunk); break;
allocChunk_ = &chunks_.back(); }
deallocChunk_ = &chunks_.front(); if ( !i->IsFilled() )
break; {
} allocChunk_ = &*i;
if (i->blocksAvailable_ > 0) break;
{ }
allocChunk_ = &*i;
break;
} }
} }
} }
assert(allocChunk_ != 0);
assert(allocChunk_->blocksAvailable_ > 0); assert( allocChunk_ != NULL );
assert( !allocChunk_->IsFilled() );
// prove either emptyChunk_ points nowhere, or points to a truly empty Chunk.
assert( ( NULL == emptyChunk_ ) || ( emptyChunk_->HasAvailable( numBlocks_ ) ) );
return allocChunk_->Allocate(blockSize_); return allocChunk_->Allocate(blockSize_);
} }
@ -244,16 +331,28 @@ void* FixedAllocator::Allocate()
// (undefined behavior if called with the wrong pointer) // (undefined behavior if called with the wrong pointer)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void FixedAllocator::Deallocate(void* p) bool FixedAllocator::Deallocate( void * p, bool doChecks )
{ {
assert(!chunks_.empty()); if ( doChecks )
assert(&chunks_.front() <= deallocChunk_); {
assert(&chunks_.back() >= deallocChunk_); assert(!chunks_.empty());
assert(&chunks_.front() <= deallocChunk_);
deallocChunk_ = VicinityFind(p); assert(&chunks_.back() >= deallocChunk_);
assert(deallocChunk_); assert( &chunks_.front() <= allocChunk_ );
assert( &chunks_.back() >= allocChunk_ );
}
Chunk * foundChunk = VicinityFind( p );
if ( doChecks )
{
assert( NULL != foundChunk );
}
else if ( NULL == foundChunk )
return false;
deallocChunk_ = foundChunk;
DoDeallocate(p); DoDeallocate(p);
return true;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -261,45 +360,47 @@ void FixedAllocator::Deallocate(void* p)
// Finds the chunk corresponding to a pointer, using an efficient search // Finds the chunk corresponding to a pointer, using an efficient search
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
FixedAllocator::Chunk* FixedAllocator::VicinityFind(void* p) FixedAllocator::Chunk * FixedAllocator::VicinityFind( void * p )
{ {
assert(!chunks_.empty()); if ( chunks_.empty() ) return NULL;
assert(deallocChunk_); assert(deallocChunk_);
unsigned char * pc = static_cast< unsigned char * >( p );
const std::size_t chunkLength = numBlocks_ * blockSize_; const std::size_t chunkLength = numBlocks_ * blockSize_;
Chunk* lo = deallocChunk_; Chunk* lo = deallocChunk_;
Chunk* hi = deallocChunk_ + 1; Chunk* hi = deallocChunk_ + 1;
Chunk* loBound = &chunks_.front(); Chunk* loBound = &chunks_.front();
Chunk* hiBound = &chunks_.back() + 1; Chunk* hiBound = &chunks_.back() + 1;
// Special case: deallocChunk_ is the last in the array // Special case: deallocChunk_ is the last in the array
if (hi == hiBound) hi = 0; if (hi == hiBound) hi = NULL;
for (;;) for (;;)
{ {
if (lo) if (lo)
{ {
if (p >= lo->pData_ && p < lo->pData_ + chunkLength) if ( lo->HasBlock( pc, chunkLength ) ) return lo;
{ if ( lo == loBound )
return lo; {
} lo = NULL;
if (lo == loBound) lo = 0; if ( NULL == hi ) break;
}
else --lo; else --lo;
} }
if (hi) if (hi)
{ {
if (p >= hi->pData_ && p < hi->pData_ + chunkLength) if ( hi->HasBlock( pc, chunkLength ) ) return hi;
{ if ( ++hi == hiBound )
return hi; {
} hi = NULL;
if (++hi == hiBound) hi = 0; if ( NULL == lo ) break;
}
} }
} }
// assert(false); return NULL;
// return 0;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -309,107 +410,178 @@ FixedAllocator::Chunk* FixedAllocator::VicinityFind(void* p)
void FixedAllocator::DoDeallocate(void* p) void FixedAllocator::DoDeallocate(void* p)
{ {
assert(deallocChunk_->pData_ <= p); assert( deallocChunk_->HasBlock( static_cast< unsigned char * >( p ),
assert(deallocChunk_->pData_ + numBlocks_ * blockSize_ > p); numBlocks_ * blockSize_ ) );
// prove either emptyChunk_ points nowhere, or points to a truly empty Chunk.
assert( ( NULL == emptyChunk_ ) || ( emptyChunk_->HasAvailable( numBlocks_ ) ) );
// call into the chunk, will adjust the inner list but won't release memory // call into the chunk, will adjust the inner list but won't release memory
deallocChunk_->Deallocate(p, blockSize_); deallocChunk_->Deallocate(p, blockSize_);
if (deallocChunk_->blocksAvailable_ == numBlocks_) if ( deallocChunk_->HasAvailable( numBlocks_ ) )
{ {
// deallocChunk_ is completely free, should we release it? assert( emptyChunk_ != deallocChunk_ );
// deallocChunk_ is empty, but a Chunk is only released if there are 2
Chunk& lastChunk = chunks_.back(); // empty chunks. Since emptyChunk_ may only point to a previously
// cleared Chunk, if it points to something else besides deallocChunk_,
if (&lastChunk == deallocChunk_) // then FixedAllocator currently has 2 empty Chunks.
if ( NULL != emptyChunk_ )
{ {
// check if we have two last chunks empty // If last Chunk is empty, just change what deallocChunk_
// points to, and release the last. Otherwise, swap an empty
if (chunks_.size() > 1 && // Chunk with the last, and then release it.
deallocChunk_[-1].blocksAvailable_ == numBlocks_) Chunk * lastChunk = &chunks_.back();
{ if ( lastChunk == deallocChunk_ )
// Two free chunks, discard the last one deallocChunk_ = emptyChunk_;
lastChunk.Release(); else if ( lastChunk != emptyChunk_ )
chunks_.pop_back(); std::swap( *emptyChunk_, *lastChunk );
allocChunk_ = deallocChunk_ = &chunks_.front(); assert( lastChunk->HasAvailable( numBlocks_ ) );
} lastChunk->Release();
return;
}
if (lastChunk.blocksAvailable_ == numBlocks_)
{
// Two free blocks, discard one
lastChunk.Release();
chunks_.pop_back(); chunks_.pop_back();
allocChunk_ = deallocChunk_; allocChunk_ = deallocChunk_;
} }
else emptyChunk_ = deallocChunk_;
{
// move the empty chunk to the end
std::swap(*deallocChunk_, lastChunk);
allocChunk_ = &chunks_.back();
}
} }
// prove either emptyChunk_ points nowhere, or points to a truly empty Chunk.
assert( ( NULL == emptyChunk_ ) || ( emptyChunk_->HasAvailable( numBlocks_ ) ) );
}
////////////////////////////////////////////////////////////////////////////////
// GetOffset
// Calculates index into array where a FixedAllocator of numBytes is located.
////////////////////////////////////////////////////////////////////////////////
inline std::size_t GetOffset( std::size_t numBytes, std::size_t alignment )
{
const std::size_t alignExtra = alignment-1;
return ( numBytes + alignExtra ) / alignment;
}
////////////////////////////////////////////////////////////////////////////////
// DefaultAllocator
// Call to default allocator when SmallObjAllocator decides not to handle request.
////////////////////////////////////////////////////////////////////////////////
void * DefaultAllocator( std::size_t numBytes, bool doThrow )
{
#ifdef USE_NEW_TO_ALLOCATE
return doThrow ? ::operator new( numBytes ) :
::operator new( numBytes, std::nothrow_t() );
#else
void * p = ::malloc( numBytes );
if ( doThrow && ( NULL == p ) )
throw std::bad_alloc();
return p;
#endif
}
////////////////////////////////////////////////////////////////////////////////
// DefaultDeallocator
// Call to default deallocator when SmallObjAllocator decides not to handle request.
////////////////////////////////////////////////////////////////////////////////
void DefaultDeallocator( void * p )
{
#ifdef USE_NEW_TO_ALLOCATE
::operator delete( p );
#else
::free( p );
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// SmallObjAllocator::SmallObjAllocator // SmallObjAllocator::SmallObjAllocator
// Creates an allocator for small objects given chunk size and maximum 'small' // Creates a SmallObjAllocator, and all the FixedAllocators within it. Each
// object size // FixedAllocator is then initialized to use the correct Chunk size.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
SmallObjAllocator::SmallObjAllocator( SmallObjAllocator::SmallObjAllocator( std::size_t pageSize,
std::size_t chunkSize, std::size_t maxObjectSize, std::size_t objectAlignSize ) :
std::size_t maxObjectSize) pool_( NULL ),
: pLastAlloc_(0), pLastDealloc_(0) maxSmallObjectSize_( maxObjectSize ),
, chunkSize_(chunkSize), maxObjectSize_(maxObjectSize) objectAlignSize_( objectAlignSize )
{ {
assert( 0 != objectAlignSize );
const std::size_t allocCount = GetOffset( maxObjectSize, objectAlignSize );
pool_ = new FixedAllocator[ allocCount ];
for ( std::size_t i = 0; i < allocCount; ++i )
pool_[ i ].Initialize( ( i+1 ) * objectAlignSize, pageSize );
}
////////////////////////////////////////////////////////////////////////////////
// SmallObjAllocator::~SmallObjAllocator
// Deletes all memory consumed by SmallObjAllocator.
// This deletes all the FixedAllocator's in the pool.
////////////////////////////////////////////////////////////////////////////////
SmallObjAllocator::~SmallObjAllocator( void )
{
delete [] pool_;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// SmallObjAllocator::Allocate // SmallObjAllocator::Allocate
// Allocates 'numBytes' memory // Handles request to allocate numBytes for 1 object.
// Uses an internal pool of FixedAllocator objects for small objects // This acts in constant-time - except for the calls to DefaultAllocator
// and sometimes FixedAllocator::Allocate. It throws bad_alloc only if the
// doThrow parameter is true and can't allocate another block. Otherwise, it
// provides the no-throw exception safety level.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void* SmallObjAllocator::Allocate(std::size_t numBytes) void * SmallObjAllocator::Allocate( std::size_t numBytes, bool doThrow )
{ {
if (numBytes > maxObjectSize_) return operator new(numBytes); if ( numBytes > GetMaxObjectSize() )
return DefaultAllocator( numBytes, doThrow );
if (pLastAlloc_ && pLastAlloc_->BlockSize() == numBytes)
assert( NULL != pool_ );
if ( 0 == numBytes ) numBytes = 1;
const std::size_t index = GetOffset( numBytes, GetAlignment() ) - 1;
const std::size_t allocCount = GetOffset( GetMaxObjectSize(), GetAlignment() );
assert( index < allocCount );
FixedAllocator & allocator = pool_[ index ];
assert( allocator.BlockSize() >= numBytes );
assert( allocator.BlockSize() < numBytes + GetAlignment() );
void * place = allocator.Allocate();
if ( ( NULL == place ) && doThrow )
{ {
return pLastAlloc_->Allocate(); #if _MSC_VER
} throw std::bad_alloc( "could not allocate small object" );
Pool::iterator i = std::lower_bound(pool_.begin(), pool_.end(), numBytes); #else
if (i == pool_.end() || i->BlockSize() != numBytes) // GCC did not like a literal string passed to std::bad_alloc.
{ // so just throw the default-constructed exception.
i = pool_.insert(i, FixedAllocator(numBytes)); throw std::bad_alloc();
pLastDealloc_ = &*pool_.begin(); #endif
} }
pLastAlloc_ = &*i; return place;
return pLastAlloc_->Allocate();
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// SmallObjAllocator::Deallocate // SmallObjAllocator::Deallocate
// Deallocates memory previously allocated with Allocate // Handles request to deallocate numBytes for 1 object.
// (undefined behavior if you pass any other pointer) // This will act in constant-time - except for the calls to DefaultDeallocator
// and sometimes FixedAllocator::Deallocate. It will never throw.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void SmallObjAllocator::Deallocate(void* p, std::size_t numBytes) void SmallObjAllocator::Deallocate( void * p, std::size_t numBytes )
{ {
if (numBytes > maxObjectSize_) return operator delete(p); if ( NULL == p ) return;
if ( numBytes > GetMaxObjectSize() )
if (pLastDealloc_ && pLastDealloc_->BlockSize() == numBytes)
{ {
pLastDealloc_->Deallocate(p); DefaultDeallocator( p );
return; return;
} }
Pool::iterator i = std::lower_bound(pool_.begin(), pool_.end(), numBytes); assert( NULL != pool_ );
assert(i != pool_.end()); if ( 0 == numBytes ) numBytes = 1;
assert(i->BlockSize() == numBytes); const std::size_t index = GetOffset( numBytes, GetAlignment() ) - 1;
pLastDealloc_ = &*i; const std::size_t allocCount = GetOffset( GetMaxObjectSize(), GetAlignment() );
pLastDealloc_->Deallocate(p); assert( index < allocCount );
FixedAllocator & allocator = pool_[ index ];
assert( allocator.BlockSize() >= numBytes );
assert( allocator.BlockSize() < numBytes + GetAlignment() );
const bool found = allocator.Deallocate( p, true );
assert( found );
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -417,5 +589,9 @@ void SmallObjAllocator::Deallocate(void* p, std::size_t numBytes)
// March 20: fix exception safety issue in FixedAllocator::Allocate // March 20: fix exception safety issue in FixedAllocator::Allocate
// (thanks to Chris Udazvinis for pointing that out) // (thanks to Chris Udazvinis for pointing that out)
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
// May 10, 2002: ported by Rani Sharoni to VC7 (RTM - 9466) // Aug 02, 2002: Fix in VicinityFind sent by Pavel Vozenilek
// Nov 26, 2004: Re-implemented by Rich Sposato.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
}; // end namespace Loki

View file

@ -8,12 +8,12 @@
// purpose is hereby granted without fee, provided that the above copyright // purpose is hereby granted without fee, provided that the above copyright
// notice appear in all copies and that both that copyright notice and this // notice appear in all copies and that both that copyright notice and this
// permission notice appear in supporting documentation. // permission notice appear in supporting documentation.
// The author or Addison-Welsey Longman make no representations about the // The author or Addison-Wesley Longman make no representations about the
// suitability of this software for any purpose. It is provided "as is" // suitability of this software for any purpose. It is provided "as is"
// without express or implied warranty. // without express or implied warranty.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Last update: May 19, 2002 // Last update: Nov 26, 2004
#ifndef SMALLOBJ_INC_ #ifndef SMALLOBJ_INC_
#define SMALLOBJ_INC_ #define SMALLOBJ_INC_
@ -21,168 +21,145 @@
#include "Threads.h" #include "Threads.h"
#include "Singleton.h" #include "Singleton.h"
#include <cstddef> #include <cstddef>
#include <vector> #include <new> // needed for std::nothrow_t parameter.
#ifndef DEFAULT_CHUNK_SIZE #ifndef DEFAULT_CHUNK_SIZE
#define DEFAULT_CHUNK_SIZE 4096 #define DEFAULT_CHUNK_SIZE 4096
#endif #endif
#ifndef MAX_SMALL_OBJECT_SIZE #ifndef MAX_SMALL_OBJECT_SIZE
#define MAX_SMALL_OBJECT_SIZE 64 #define MAX_SMALL_OBJECT_SIZE 256
#endif #endif
#ifndef LOKI_DEFAULT_OBJECT_ALIGNMENT
#define LOKI_DEFAULT_OBJECT_ALIGNMENT 4
#endif
namespace Loki namespace Loki
{ {
//////////////////////////////////////////////////////////////////////////////// class FixedAllocator;
// class FixedAllocator
// Offers services for allocating fixed-sized objects
////////////////////////////////////////////////////////////////////////////////
class FixedAllocator
{
public: // VC7 access control BUG
class Chunk
{
friend FixedAllocator;
void Init(std::size_t blockSize, unsigned char blocks);
void* Allocate(std::size_t blockSize);
void Deallocate(void* p, std::size_t blockSize);
void Reset(std::size_t blockSize, unsigned char blocks);
void Release();
unsigned char* pData_;
unsigned char
firstAvailableBlock_,
blocksAvailable_;
};
private:
// Internal functions
void DoDeallocate(void* p);
Chunk* VicinityFind(void* p);
// Data
std::size_t blockSize_;
unsigned char numBlocks_;
typedef std::vector<Chunk> Chunks;
Chunks chunks_;
Chunk* allocChunk_;
Chunk* deallocChunk_;
// For ensuring proper copy semantics
mutable const FixedAllocator* prev_;
mutable const FixedAllocator* next_;
public:
// Create a FixedAllocator able to manage blocks of 'blockSize' size
explicit FixedAllocator(std::size_t blockSize = 0);
FixedAllocator(const FixedAllocator&);
FixedAllocator& operator=(const FixedAllocator&);
~FixedAllocator();
void Swap(FixedAllocator& rhs);
// Allocate a memory block
void* Allocate();
// Deallocate a memory block previously allocated with Allocate()
// (if that's not the case, the behavior is undefined)
void Deallocate(void* p);
// Returns the block size with which the FixedAllocator was initialized
std::size_t BlockSize() const
{ return blockSize_; }
// Comparison operator for sorting
bool operator<(std::size_t rhs) const
{ return BlockSize() < rhs; }
};
////////////////////////////////////////////////////////////////////////////////
// class SmallObjAllocator
// Offers services for allocating small-sized objects
////////////////////////////////////////////////////////////////////////////////
class SmallObjAllocator class SmallObjAllocator
{ {
public: public:
SmallObjAllocator( SmallObjAllocator( std::size_t pageSize, std::size_t maxObjectSize,
std::size_t chunkSize, std::size_t objectAlignSize );
std::size_t maxObjectSize);
~SmallObjAllocator( void );
void* Allocate(std::size_t numBytes);
void Deallocate(void* p, std::size_t size); void * Allocate( std::size_t size, bool doThrow );
void Deallocate( void * p, std::size_t size );
inline std::size_t GetMaxObjectSize() const { return maxSmallObjectSize_; }
inline std::size_t GetAlignment() const { return objectAlignSize_; }
private: private:
SmallObjAllocator(const SmallObjAllocator&); Loki::FixedAllocator * pool_;
SmallObjAllocator& operator=(const SmallObjAllocator&);
std::size_t maxSmallObjectSize_;
typedef std::vector<FixedAllocator> Pool;
Pool pool_; std::size_t objectAlignSize_;
FixedAllocator* pLastAlloc_;
FixedAllocator* pLastDealloc_;
std::size_t chunkSize_;
std::size_t maxObjectSize_;
}; };
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// class SmallObject // class SmallObject
// Base class for polymorphic small objects, offers fast // Base class for polymorphic small objects, offers fast allocations &
// allocations/deallocations // deallocations. Destructor is virtual and public.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template template
< <
template <class> class ThreadingModel = DEFAULT_THREADING, template <class> class ThreadingModel = DEFAULT_THREADING,
std::size_t chunkSize = DEFAULT_CHUNK_SIZE, std::size_t chunkSize = DEFAULT_CHUNK_SIZE,
std::size_t maxSmallObjectSize = MAX_SMALL_OBJECT_SIZE std::size_t maxSmallObjectSize = MAX_SMALL_OBJECT_SIZE,
std::size_t objectAlignSize = LOKI_DEFAULT_OBJECT_ALIGNMENT
> >
class SmallObject : public ThreadingModel< class SmallObject : public ThreadingModel<
SmallObject<ThreadingModel, chunkSize, maxSmallObjectSize> > SmallObject< ThreadingModel, chunkSize, maxSmallObjectSize, objectAlignSize > >
{ {
typedef ThreadingModel< SmallObject<ThreadingModel,
chunkSize, maxSmallObjectSize> > MyThreadingModel; typedef ThreadingModel< SmallObject<ThreadingModel,
chunkSize, maxSmallObjectSize, objectAlignSize > > MyThreadingModel;
struct MySmallObjAllocator : public SmallObjAllocator struct MySmallObjAllocator : public SmallObjAllocator
{ {
MySmallObjAllocator() MySmallObjAllocator()
: SmallObjAllocator(chunkSize, maxSmallObjectSize) : SmallObjAllocator( chunkSize, maxSmallObjectSize, objectAlignSize )
{} {}
}; };
// The typedef below would make things much simpler, // The typedef below would make things much simpler,
// but MWCW won't like it // but MWCW won't like it
// typedef SingletonHolder<MySmallObjAllocator/*, CreateStatic, // typedef SingletonHolder<MySmallObjAllocator/*, CreateStatic,
// DefaultLifetime, ThreadingModel*/> MyAllocator; // NoDestroy, ThreadingModel*/> MyAllocator;
public: public:
static void* operator new(std::size_t size)
#if (MAX_SMALL_OBJECT_SIZE != 0) && (DEFAULT_CHUNK_SIZE != 0) && (LOKI_DEFAULT_OBJECT_ALIGNMENT != 0)
/// Throwing single-object new.
static void * operator new ( std::size_t size ) throw ( std::bad_alloc )
{ {
#if (MAX_SMALL_OBJECT_SIZE != 0) && (DEFAULT_CHUNK_SIZE != 0)
typename MyThreadingModel::Lock lock; typename MyThreadingModel::Lock lock;
(void)lock; // get rid of warning (void)lock; // get rid of warning
return SingletonHolder< MySmallObjAllocator, CreateStatic,
return SingletonHolder<MySmallObjAllocator, CreateStatic, NoDestroy >::Instance().Allocate( size, true );
PhoenixSingleton>::Instance().Allocate(size);
#else
return ::operator new(size);
#endif
} }
static void operator delete(void* p, std::size_t size)
/// Non-throwing single-object new.
static void * operator new ( std::size_t size, const std::nothrow_t & ) throw ()
{ {
#if (MAX_SMALL_OBJECT_SIZE != 0) && (DEFAULT_CHUNK_SIZE != 0)
typename MyThreadingModel::Lock lock; typename MyThreadingModel::Lock lock;
(void)lock; // get rid of warning (void)lock; // get rid of warning
return SingletonHolder< MySmallObjAllocator, CreateStatic,
SingletonHolder<MySmallObjAllocator, CreateStatic, NoDestroy >::Instance().Allocate( size, false );
PhoenixSingleton>::Instance().Deallocate(p, size);
#else
::operator delete(p, size);
#endif
} }
/// Placement single-object new.
static void * operator new ( std::size_t size, void * place )
{
return ::operator new( size, place );
}
/// Single-object delete.
static void operator delete ( void * p, std::size_t size ) throw ()
{
typename MyThreadingModel::Lock lock;
(void)lock; // get rid of warning
SingletonHolder< MySmallObjAllocator, CreateStatic,
NoDestroy >::Instance().Deallocate( p, size );
}
/// Non-throwing single-object delete.
static void operator delete ( void * p, std::size_t size,
const std::nothrow_t & ) throw()
{
typename MyThreadingModel::Lock lock;
(void)lock; // get rid of warning
SingletonHolder< MySmallObjAllocator, CreateStatic,
NoDestroy >::Instance().Deallocate( p, size );
}
/// Placement single-object delete.
static void operator delete ( void * p, void * place )
{
::operator delete ( p, place );
}
#endif // #if default template parameters are not zero
virtual ~SmallObject() {} virtual ~SmallObject() {}
}; }; // end class SmallObject
} // namespace Loki } // namespace Loki
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Change log: // Change log:
// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
// May 10, 2002: ported by Rani Sharoni to VC7 (RTM - 9466) // Nov. 26, 2004: re-implemented by Rich Sposato.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#endif // SMALLOBJ_INC_ #endif // SMALLOBJ_INC_