4 inline std::size_t
GetOffset( std::size_t numBytes, std::size_t alignment ){
5 return ( numBytes + alignment-1 ) / alignment;
9 return doThrow ? ::operator
new( numBytes ) : ::
operator new( numBytes, std::nothrow_t() );
13 ::operator
delete( p );
17 std::size_t maxObjectSize, std::size_t objectAlignSize ) :
19 fMaxObjectSize( maxObjectSize ),
20 fAlignSize( objectAlignSize ){
22 assert( 0 != objectAlignSize );
23 const std::size_t allocCount =
GetOffset( maxObjectSize, objectAlignSize );
25 for ( std::size_t i = 0; i < allocCount; ++i )
26 fPool[ i ].Initialize( ( i+1 ) * objectAlignSize, pageSize );
35 const std::size_t allocCount =
GetOffset( GetMaxObjectSize(), GetAlignment() );
37 for ( ; i < allocCount; ++i )
38 found or_eq fPool[i].TrimEmptyChunk();
39 for ( i = 0; i < allocCount; ++i )
40 found or_eq fPool[i].TrimChunkList();
45 if ( numBytes > GetMaxObjectSize() )
48 assert( fPool != NULL );
49 if ( numBytes == 0 ) numBytes = 1;
50 const std::size_t index =
GetOffset( numBytes, GetAlignment() ) - 1;
51 const std::size_t allocCount =
GetOffset( GetMaxObjectSize(), GetAlignment() );
53 assert( allocCount > index );
56 assert( allocator.
BlockSize() >= numBytes );
57 assert( allocator.
BlockSize() < numBytes + GetAlignment() );
60 if ( ( place == NULL ) && TrimExcessMemory() )
63 if ( ( place == NULL ) && doThrow )
64 throw std::bad_alloc();
69 if ( p == NULL )
return;
70 if ( numBytes > GetMaxObjectSize() ){
75 assert( fPool != NULL );
76 if ( numBytes ==0 ) numBytes = 1;
77 const std::size_t index =
GetOffset( numBytes, GetAlignment() ) - 1;
78 const std::size_t allocCount =
GetOffset( GetMaxObjectSize(), GetAlignment() );
80 assert( index < allocCount );
82 assert( allocator.
BlockSize() >= numBytes );
83 assert( allocator.
BlockSize() < numBytes + GetAlignment() );
84 const bool found = allocator.
Deallocate( p, NULL );
90 if ( p == NULL )
return;
91 assert( fPool != NULL );
92 const std::size_t allocCount =
GetOffset( GetMaxObjectSize(), GetAlignment() );
96 for ( std::size_t ii = 0; ii < allocCount; ++ii ) {
99 pAllocator = &fPool[ ii ];
103 if ( pAllocator == NULL ){
108 assert( NULL != chunk );
109 const bool found = pAllocator->
Deallocate( p, chunk );
116 if ( fPool == NULL ){
120 if ( 0 == GetAlignment() ) {
124 if ( 0 == GetMaxObjectSize() ) {
128 const std::size_t allocCount =
GetOffset( GetMaxObjectSize(), GetAlignment() );
129 for ( std::size_t ii = 0; ii < allocCount; ++ii ){
130 if ( fPool[ ii ].IsCorrupt() )
void * Allocate(std::size_t size, bool doThrow)
Pool of allocation chunks.
bool IsCorrupt(void) const
std::size_t GetOffset(std::size_t numBytes, std::size_t alignment)
bool HasBlock(void *p, std::size_t chunkLength) const
Checks if this block belongs to this chunk.
void * DefaultAllocator(std::size_t numBytes, bool doThrow)
SmallObjectAllocatorImplementation(void)
Protium::Allocation::FixedAllocator * fPool
bool Deallocate(void *p, Chunk *hint)
bool TrimExcessMemory(void)
Represents a fixed number of blocks.
void Deallocate(void *p, std::size_t size)
void DefaultDeallocator(void *p)
~SmallObjectAllocatorImplementation(void)
std::size_t BlockSize() const
Returns block size with which the FixedAllocator was initialized.