The Gaudi Framework  v29r0 (ff2e7097)
AllocatorPool.cpp
Go to the documentation of this file.
1 // ============================================================================
7 // ============================================================================
8 //
9 // ********************************************************************
10 // * DISCLAIMER *
11 // * *
12 // * The following disclaimer summarizes all the specific disclaimers *
13 // * of contributors to this software. The specific disclaimers,which *
14 // * govern, are listed with their locations in: *
15 // * http://cern.ch/geant4/license *
16 // * *
17 // * Neither the authors of this software system, nor their employing *
18 // * institutes,nor the agencies providing financial support for this *
19 // * work make any representation or warranty, express or implied, *
20 // * regarding this software system or assume any liability for its *
21 // * use. *
22 // * *
23 // * This code implementation is the intellectual property of the *
24 // * GEANT4 collaboration. *
25 // * By copying, distributing or modifying the Program (or any work *
26 // * based on the Program) you indicate your acceptance of this *
27 // * statement, and all its terms. *
28 // ********************************************************************
29 //
30 // ----------------------------------------------------------------------
31 // G4AllocatorPool
32 //
33 // Implementation file
34 //
35 // Author: G.Cosmo, November 2000
36 //
37 // ============================================================================
38 // Include files
39 // ============================================================================
40 // GaudiKernel
41 // ============================================================================
43 // ============================================================================
44 
45 // ************************************************************
46 // G4AllocatorPool constructor
47 // ************************************************************
48 //
50  : esize( sz < sizeof( PoolLink ) ? sizeof( PoolLink ) : sz ), csize( sz < 1024 / 2 - 16 ? 1024 - 16 : sz * 10 - 16 )
51 {
52 }
53 
54 // ************************************************************
55 // G4AllocatorPool copy constructor
56 // ************************************************************
57 //
59 {
60  *this = right;
61 }
62 
63 // ************************************************************
64 // G4AllocatorPool operator=
65 // ************************************************************
66 //
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 }
77 
78 // ************************************************************
79 // G4AllocatorPool destructor
80 // ************************************************************
81 //
83 
84 // ************************************************************
85 // Reset
86 // ************************************************************
87 //
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 }
103 
104 // ************************************************************
105 // Grow
106 // ************************************************************
107 //
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 }
127 
128 // ============================================================================
129 // The END
130 // ============================================================================
void Grow()
Make pool larger.
AllocatorPool & operator=(const AllocatorPool &right)
Private equality operator.
~AllocatorPool()
Destructor. Return storage to the free store.
const unsigned int esize
void Reset()
Return storage to the free store.
start
Definition: IOTest.py:99
Allocator pool.
const unsigned int csize
AllocatorPool(unsigned int n=0)
Create a pool of elements of size n.