Gaudi Framework, version v20r2

Generated: 18 Jul 2008

GaudiUtils::AllocatorPool Class Reference

#include <GaudiKernel/AllocatorPool.h>

Collaboration diagram for GaudiUtils::AllocatorPool:

Collaboration graph
[legend]
List of all members.

Detailed Description

Allocator pool.

Class is imported from Geant4 project

Date:
2006-02-14

Definition at line 63 of file AllocatorPool.h.

Public Member Functions

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

Private Member Functions

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

Private Attributes

const unsigned int esize
const unsigned int csize
PoolChunkchunks
PoolLinkhead
int nchunks

Classes

class  PoolChunk
struct  PoolLink


Constructor & Destructor Documentation

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

Create a pool of elements of size n.

Definition at line 53 of file AllocatorPool.cpp.

00054   : esize(sz<sizeof(PoolLink) ? sizeof(PoolLink) : sz),
00055     csize(sz<1024/2-16 ? 1024-16 : sz*10-16),
00056     chunks(0), head(0), nchunks(0)
00057 {}

GaudiUtils::AllocatorPool::~AllocatorPool (  ) 

Destructor. Return storage to the free store.

Definition at line 88 of file AllocatorPool.cpp.

References Reset().

00089 {
00090   Reset();
00091 }

GaudiUtils::AllocatorPool::AllocatorPool ( const AllocatorPool right  ) 

Copy constructor.

Definition at line 63 of file AllocatorPool.cpp.

00064   : esize(right.esize), csize(right.csize)
00065 {
00066   *this = right;
00067 } 


Member Function Documentation

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

Allocate one element.

Definition at line 125 of file AllocatorPool.h.

References Grow(), head, and GaudiUtils::AllocatorPool::PoolLink::next.

Referenced by GaudiUtils::Allocator< Type >::MallocSingle().

00126 {
00127   if ( head==0 ) { Grow(); }
00128   PoolLink* p = head;  // return first element
00129   head = p->next;
00130   return p;
00131 }

void GaudiUtils::AllocatorPool::Free ( void *  b  )  [inline]

Return an element back to the pool.

Definition at line 138 of file AllocatorPool.h.

References head, and GaudiUtils::AllocatorPool::PoolLink::next.

Referenced by GaudiUtils::Allocator< Type >::FreeSingle().

00139 {
00140   PoolLink* p = static_cast<PoolLink*>(b);
00141   p->next = head;        // put b back as first element
00142   head = p;
00143 }

unsigned int GaudiUtils::AllocatorPool::Size (  )  const [inline]

Return storage size.

Definition at line 150 of file AllocatorPool.h.

References csize, and nchunks.

Referenced by GaudiUtils::Allocator< Type >::GetAllocatedSize().

00151 {
00152   return nchunks*csize;
00153 }

void GaudiUtils::AllocatorPool::Reset (  ) 

Return storage to the free store.

Definition at line 97 of file AllocatorPool.cpp.

References chunks, head, nchunks, and GaudiUtils::AllocatorPool::PoolChunk::next.

Referenced by GaudiUtils::Allocator< Type >::ResetStorage(), and ~AllocatorPool().

00098 {
00099   // Free all chunks
00100   //
00101   PoolChunk* n = chunks;
00102   PoolChunk* p = 0;
00103   while (n)
00104   {
00105     p = n;
00106     n = n->next;
00107     delete p;
00108   }
00109   head = 0;
00110   chunks = 0;
00111   nchunks = 0;
00112 }

GaudiUtils::AllocatorPool & GaudiUtils::AllocatorPool::operator= ( const AllocatorPool right  )  [private]

Private equality operator.

Definition at line 75 of file AllocatorPool.cpp.

References right().

00076 {
00077   if (&right == this) { return *this; }
00078   chunks  = right.chunks;
00079   head    = right.head;
00080   nchunks = right.nchunks;
00081   return *this;
00082 }

void GaudiUtils::AllocatorPool::Grow (  )  [private]

Make pool larger.

Definition at line 118 of file AllocatorPool.cpp.

References chunks, csize, esize, head, GaudiUtils::AllocatorPool::PoolChunk::mem, nchunks, and GaudiUtils::AllocatorPool::PoolChunk::next.

Referenced by Alloc().

00119 {
00120   // Allocate new chunk, organize it as a linked list of
00121   // elements of size 'esize'
00122   //
00123   PoolChunk* n = new PoolChunk(csize);
00124   n->next = chunks;
00125   chunks = n;
00126   nchunks++;
00127   
00128   const int nelem = csize/esize;
00129   char* start = n->mem;
00130   char* last = &start[(nelem-1)*esize];
00131   for (char* p=start; p<last; p+=esize)
00132   {
00133     reinterpret_cast<PoolLink*>(p)->next
00134       = reinterpret_cast<PoolLink*>(p+esize);
00135   }
00136   reinterpret_cast<PoolLink*>(last)->next = 0;
00137   head = reinterpret_cast<PoolLink*>(start);
00138 }


Member Data Documentation

const unsigned int GaudiUtils::AllocatorPool::esize [private]

Definition at line 109 of file AllocatorPool.h.

Referenced by Grow().

const unsigned int GaudiUtils::AllocatorPool::csize [private]

Definition at line 110 of file AllocatorPool.h.

Referenced by Grow(), and Size().

PoolChunk* GaudiUtils::AllocatorPool::chunks [private]

Definition at line 111 of file AllocatorPool.h.

Referenced by Grow(), and Reset().

PoolLink* GaudiUtils::AllocatorPool::head [private]

Definition at line 112 of file AllocatorPool.h.

Referenced by Alloc(), Free(), Grow(), and Reset().

int GaudiUtils::AllocatorPool::nchunks [private]

Definition at line 113 of file AllocatorPool.h.

Referenced by Grow(), Reset(), and Size().


The documentation for this class was generated from the following files:
Generated at Fri Jul 18 12:10:35 2008 for Gaudi Framework, version v20r2 by Doxygen version 1.5.1 written by Dimitri van Heesch, © 1997-2004