[833] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
[1340] | 27 | // $Id: G4AllocatorPool.cc,v 1.6 2010/07/14 10:45:46 gcosmo Exp $ |
---|
| 28 | // GEANT4 tag $Name: global-V09-03-22 $ |
---|
[833] | 29 | // |
---|
| 30 | // |
---|
| 31 | // ---------------------------------------------------------------------- |
---|
| 32 | // G4AllocatorPool |
---|
| 33 | // |
---|
| 34 | // Implementation file |
---|
| 35 | // |
---|
| 36 | // Author: G.Cosmo, November 2000 |
---|
| 37 | // |
---|
| 38 | |
---|
| 39 | #include "G4AllocatorPool.hh" |
---|
| 40 | |
---|
| 41 | // ************************************************************ |
---|
| 42 | // G4AllocatorPool constructor |
---|
| 43 | // ************************************************************ |
---|
| 44 | // |
---|
| 45 | G4AllocatorPool::G4AllocatorPool( unsigned int sz ) |
---|
| 46 | : esize(sz<sizeof(G4PoolLink) ? sizeof(G4PoolLink) : sz), |
---|
| 47 | csize(sz<1024/2-16 ? 1024-16 : sz*10-16), |
---|
| 48 | chunks(0), head(0), nchunks(0) |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | // ************************************************************ |
---|
| 53 | // G4AllocatorPool copy constructor |
---|
| 54 | // ************************************************************ |
---|
| 55 | // |
---|
| 56 | G4AllocatorPool::G4AllocatorPool(const G4AllocatorPool& right) |
---|
[1340] | 57 | : esize(right.esize), csize(right.csize), |
---|
| 58 | chunks(right.chunks), head(right.head), nchunks(right.nchunks) |
---|
[833] | 59 | { |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | // ************************************************************ |
---|
| 63 | // G4AllocatorPool operator= |
---|
| 64 | // ************************************************************ |
---|
| 65 | // |
---|
| 66 | G4AllocatorPool& |
---|
| 67 | G4AllocatorPool::operator= (const G4AllocatorPool& right) |
---|
| 68 | { |
---|
| 69 | if (&right == this) { return *this; } |
---|
| 70 | chunks = right.chunks; |
---|
| 71 | head = right.head; |
---|
| 72 | nchunks = right.nchunks; |
---|
| 73 | return *this; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // ************************************************************ |
---|
| 77 | // G4AllocatorPool destructor |
---|
| 78 | // ************************************************************ |
---|
| 79 | // |
---|
| 80 | G4AllocatorPool::~G4AllocatorPool() |
---|
| 81 | { |
---|
| 82 | Reset(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // ************************************************************ |
---|
| 86 | // Reset |
---|
| 87 | // ************************************************************ |
---|
| 88 | // |
---|
| 89 | void G4AllocatorPool::Reset() |
---|
| 90 | { |
---|
| 91 | // Free all chunks |
---|
| 92 | // |
---|
| 93 | G4PoolChunk* n = chunks; |
---|
| 94 | G4PoolChunk* p = 0; |
---|
| 95 | while (n) |
---|
| 96 | { |
---|
| 97 | p = n; |
---|
| 98 | n = n->next; |
---|
| 99 | delete p; |
---|
| 100 | } |
---|
| 101 | head = 0; |
---|
| 102 | chunks = 0; |
---|
| 103 | nchunks = 0; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // ************************************************************ |
---|
| 107 | // Grow |
---|
| 108 | // ************************************************************ |
---|
| 109 | // |
---|
| 110 | void G4AllocatorPool::Grow() |
---|
| 111 | { |
---|
| 112 | // Allocate new chunk, organize it as a linked list of |
---|
| 113 | // elements of size 'esize' |
---|
| 114 | // |
---|
| 115 | G4PoolChunk* n = new G4PoolChunk(csize); |
---|
| 116 | n->next = chunks; |
---|
| 117 | chunks = n; |
---|
| 118 | nchunks++; |
---|
| 119 | |
---|
| 120 | const int nelem = csize/esize; |
---|
| 121 | char* start = n->mem; |
---|
| 122 | char* last = &start[(nelem-1)*esize]; |
---|
| 123 | for (char* p=start; p<last; p+=esize) |
---|
| 124 | { |
---|
| 125 | reinterpret_cast<G4PoolLink*>(p)->next |
---|
| 126 | = reinterpret_cast<G4PoolLink*>(p+esize); |
---|
| 127 | } |
---|
| 128 | reinterpret_cast<G4PoolLink*>(last)->next = 0; |
---|
| 129 | head = reinterpret_cast<G4PoolLink*>(start); |
---|
| 130 | } |
---|