[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 | // |
---|
| 27 | // $Id: G4AllocatorPool.hh,v 1.5 2006/06/29 19:01:18 gunter Exp $ |
---|
[1228] | 28 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
[833] | 29 | // |
---|
| 30 | // |
---|
| 31 | // ------------------------------------------------------------------- |
---|
| 32 | // GEANT 4 class header file |
---|
| 33 | // |
---|
| 34 | // Class description: |
---|
| 35 | // |
---|
| 36 | // Class implementing a memory pool for fast allocation and deallocation |
---|
| 37 | // of memory chunks. The size of the chunks for small allocated objects |
---|
| 38 | // is fixed to 1Kb and takes into account of memory alignment; for large |
---|
| 39 | // objects it is set to 10 times the object's size. |
---|
| 40 | // The implementation is derived from: B.Stroustrup, The C++ Programming |
---|
| 41 | // Language, Third Edition. |
---|
| 42 | |
---|
| 43 | // -------------- G4AllocatorPool ---------------- |
---|
| 44 | // |
---|
| 45 | // Author: G.Cosmo (CERN), November 2000 |
---|
| 46 | // ------------------------------------------------------------------- |
---|
| 47 | |
---|
| 48 | #ifndef G4AllocatorPool_h |
---|
| 49 | #define G4AllocatorPool_h 1 |
---|
| 50 | |
---|
| 51 | class G4AllocatorPool |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | |
---|
| 55 | explicit G4AllocatorPool( unsigned int n=0 ); |
---|
| 56 | // Create a pool of elements of size n |
---|
| 57 | ~G4AllocatorPool(); |
---|
| 58 | // Destructor. Return storage to the free store |
---|
| 59 | |
---|
| 60 | G4AllocatorPool(const G4AllocatorPool& right); |
---|
| 61 | // Copy constructor |
---|
| 62 | |
---|
| 63 | inline void* Alloc(); |
---|
| 64 | // Allocate one element |
---|
| 65 | inline void Free( void* b ); |
---|
| 66 | // Return an element back to the pool |
---|
| 67 | |
---|
| 68 | inline unsigned int Size() const; |
---|
| 69 | // Return storage size |
---|
| 70 | void Reset(); |
---|
| 71 | // Return storage to the free store |
---|
| 72 | |
---|
| 73 | private: |
---|
| 74 | |
---|
| 75 | G4AllocatorPool& operator= (const G4AllocatorPool& right); |
---|
| 76 | // Private equality operator |
---|
| 77 | |
---|
| 78 | struct G4PoolLink |
---|
| 79 | { |
---|
| 80 | G4PoolLink* next; |
---|
| 81 | }; |
---|
| 82 | class G4PoolChunk |
---|
| 83 | { |
---|
| 84 | public: |
---|
| 85 | explicit G4PoolChunk(unsigned int sz) |
---|
| 86 | : size(sz), mem(new char[size]), next(0) {;} |
---|
| 87 | ~G4PoolChunk() { delete [] mem; } |
---|
| 88 | const unsigned int size; |
---|
| 89 | char* mem; |
---|
| 90 | G4PoolChunk* next; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | void Grow(); |
---|
| 94 | // Make pool larger |
---|
| 95 | |
---|
| 96 | private: |
---|
| 97 | |
---|
| 98 | const unsigned int esize; |
---|
| 99 | const unsigned int csize; |
---|
| 100 | G4PoolChunk* chunks; |
---|
| 101 | G4PoolLink* head; |
---|
| 102 | int nchunks; |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | // ------------------------------------------------------------ |
---|
| 106 | // Inline implementation |
---|
| 107 | // ------------------------------------------------------------ |
---|
| 108 | |
---|
| 109 | // ************************************************************ |
---|
| 110 | // Alloc |
---|
| 111 | // ************************************************************ |
---|
| 112 | // |
---|
| 113 | inline void* |
---|
| 114 | G4AllocatorPool::Alloc() |
---|
| 115 | { |
---|
| 116 | if (head==0) { Grow(); } |
---|
| 117 | G4PoolLink* p = head; // return first element |
---|
| 118 | head = p->next; |
---|
| 119 | return p; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | // ************************************************************ |
---|
| 123 | // Free |
---|
| 124 | // ************************************************************ |
---|
| 125 | // |
---|
| 126 | inline void |
---|
| 127 | G4AllocatorPool::Free( void* b ) |
---|
| 128 | { |
---|
| 129 | G4PoolLink* p = static_cast<G4PoolLink*>(b); |
---|
| 130 | p->next = head; // put b back as first element |
---|
| 131 | head = p; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | // ************************************************************ |
---|
| 135 | // Size |
---|
| 136 | // ************************************************************ |
---|
| 137 | // |
---|
| 138 | inline unsigned int |
---|
| 139 | G4AllocatorPool::Size() const |
---|
| 140 | { |
---|
| 141 | return nchunks*csize; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #endif |
---|