The Gaudi Framework  v29r0 (ff2e7097)
GaudiUtils::AllocatorPool Class Referencefinal

Allocator pool. More...

#include <GaudiKernel/AllocatorPool.h>

Collaboration diagram for GaudiUtils::AllocatorPool:

Classes

class  PoolChunk
 
struct  PoolLink
 

Public Member Functions

 AllocatorPool (unsigned int n=0)
 Create a pool of elements of size n. More...
 
 ~AllocatorPool ()
 Destructor. Return storage to the free store. More...
 
 AllocatorPool (const AllocatorPool &right)
 Copy constructor. More...
 
void * Alloc ()
 Allocate one element. More...
 
void Free (void *b)
 Return an element back to the pool. More...
 
unsigned int Size () const
 Return storage size. More...
 
void Reset ()
 Return storage to the free store. More...
 

Private Member Functions

AllocatorPooloperator= (const AllocatorPool &right)
 Private equality operator. More...
 
void Grow ()
 Make pool larger. More...
 

Private Attributes

const unsigned int esize
 
const unsigned int csize
 
PoolChunkchunks = nullptr
 
PoolLinkhead = nullptr
 
int nchunks = 0
 

Detailed Description

Allocator pool.

Class is imported from Geant4 project

Date
2006-02-14

Definition at line 63 of file AllocatorPool.h.

Constructor & Destructor Documentation

GaudiUtils::AllocatorPool::AllocatorPool ( unsigned int  n = 0)
explicit

Create a pool of elements of size n.

Definition at line 49 of file AllocatorPool.cpp.

50  : esize( sz < sizeof( PoolLink ) ? sizeof( PoolLink ) : sz ), csize( sz < 1024 / 2 - 16 ? 1024 - 16 : sz * 10 - 16 )
51 {
52 }
const unsigned int esize
const unsigned int csize
GaudiUtils::AllocatorPool::~AllocatorPool ( )

Destructor. Return storage to the free store.

Definition at line 82 of file AllocatorPool.cpp.

82 { Reset(); }
void Reset()
Return storage to the free store.
GaudiUtils::AllocatorPool::AllocatorPool ( const AllocatorPool right)

Copy constructor.

Definition at line 58 of file AllocatorPool.cpp.

58  : esize( right.esize ), csize( right.csize )
59 {
60  *this = right;
61 }
T right(T...args)
const unsigned int esize
const unsigned int csize

Member Function Documentation

void * GaudiUtils::AllocatorPool::Alloc ( )
inline

Allocate one element.

Definition at line 115 of file AllocatorPool.h.

116 {
117  if ( head == 0 ) {
118  Grow();
119  }
120  PoolLink* p = head; // return first element
121  head = p->next;
122  return p;
123 }
void Grow()
Make pool larger.
void GaudiUtils::AllocatorPool::Free ( void *  b)
inline

Return an element back to the pool.

Definition at line 129 of file AllocatorPool.h.

130 {
131  PoolLink* p = static_cast<PoolLink*>( b );
132  p->next = head; // put b back as first element
133  head = p;
134 }
void GaudiUtils::AllocatorPool::Grow ( )
private

Make pool larger.

Definition at line 108 of file AllocatorPool.cpp.

109 {
110  // Allocate new chunk, organize it as a linked list of
111  // elements of size 'esize'
112  //
113  auto n = new PoolChunk( csize );
114  n->next = chunks;
115  chunks = n;
116  ++nchunks;
117 
118  const int nelem = csize / esize;
119  char* start = n->mem.get();
120  char* last = &start[( nelem - 1 ) * esize];
121  for ( char* p = start; p < last; p += esize ) {
122  reinterpret_cast<PoolLink*>( p )->next = reinterpret_cast<PoolLink*>( p + esize );
123  }
124  reinterpret_cast<PoolLink*>( last )->next = nullptr;
125  head = reinterpret_cast<PoolLink*>( start );
126 }
const unsigned int esize
start
Definition: IOTest.py:99
const unsigned int csize
GaudiUtils::AllocatorPool & GaudiUtils::AllocatorPool::operator= ( const AllocatorPool right)
private

Private equality operator.

Definition at line 67 of file AllocatorPool.cpp.

68 {
69  if ( &right == this ) {
70  return *this;
71  }
72  chunks = right.chunks;
73  head = right.head;
74  nchunks = right.nchunks;
75  return *this;
76 }
T right(T...args)
void GaudiUtils::AllocatorPool::Reset ( )

Return storage to the free store.

Definition at line 88 of file AllocatorPool.cpp.

89 {
90  // Free all chunks
91  //
92  PoolChunk* n = chunks;
93  PoolChunk* p = nullptr;
94  while ( n ) {
95  p = n;
96  n = n->next;
97  delete p;
98  }
99  head = nullptr;
100  chunks = nullptr;
101  nchunks = 0;
102 }
unsigned int GaudiUtils::AllocatorPool::Size ( ) const
inline

Return storage size.

Definition at line 140 of file AllocatorPool.h.

140 { return nchunks * csize; }
const unsigned int csize

Member Data Documentation

PoolChunk* GaudiUtils::AllocatorPool::chunks = nullptr
private

Definition at line 104 of file AllocatorPool.h.

const unsigned int GaudiUtils::AllocatorPool::csize
private

Definition at line 103 of file AllocatorPool.h.

const unsigned int GaudiUtils::AllocatorPool::esize
private

Definition at line 102 of file AllocatorPool.h.

PoolLink* GaudiUtils::AllocatorPool::head = nullptr
private

Definition at line 105 of file AllocatorPool.h.

int GaudiUtils::AllocatorPool::nchunks = 0
private

Definition at line 106 of file AllocatorPool.h.


The documentation for this class was generated from the following files: