|
||||
File indexing completed on 2025-01-18 09:57:53
0001 // 0002 // ******************************************************************** 0003 // * License and Disclaimer * 0004 // * * 0005 // * The Geant4 software is copyright of the Copyright Holders of * 0006 // * the Geant4 Collaboration. It is provided under the terms and * 0007 // * conditions of the Geant4 Software License, included in the file * 0008 // * LICENSE and available at http://cern.ch/geant4/license . These * 0009 // * include a list of copyright holders. * 0010 // * * 0011 // * Neither the authors of this software system, nor their employing * 0012 // * institutes,nor the agencies providing financial support for this * 0013 // * work make any representation or warranty, express or implied, * 0014 // * regarding this software system or assume any liability for its * 0015 // * use. Please see the license in the file LICENSE and URL above * 0016 // * for the full disclaimer and the limitation of liability. * 0017 // * * 0018 // * This code implementation is the result of the scientific and * 0019 // * technical work of the GEANT4 collaboration. * 0020 // * By using, copying, modifying or distributing the software (or * 0021 // * any work based on the software) you agree to acknowledge its * 0022 // * use in resulting scientific publications, and indicate your * 0023 // * acceptance of all terms of the Geant4 Software license. * 0024 // ******************************************************************** 0025 // 0026 // G4AllocatorPool 0027 // 0028 // Class description: 0029 // 0030 // Class implementing a memory pool for fast allocation and deallocation 0031 // of memory chunks. The size of the chunks for small allocated objects 0032 // is fixed to 1Kb and takes into account of memory alignment; for large 0033 // objects it is set to 10 times the object's size. 0034 // The implementation is derived from: B.Stroustrup, The C++ Programming 0035 // Language, Third Edition. 0036 0037 // -------------- G4AllocatorPool ---------------- 0038 // 0039 // Author: G.Cosmo (CERN), November 2000 0040 // -------------------------------------------------------------------- 0041 #ifndef G4AllocatorPool_hh 0042 #define G4AllocatorPool_hh 1 0043 0044 class G4AllocatorPool 0045 { 0046 public: 0047 explicit G4AllocatorPool(unsigned int n = 0); 0048 // Create a pool of elements of size n 0049 ~G4AllocatorPool(); 0050 // Destructor. Return storage to the free store 0051 0052 G4AllocatorPool(const G4AllocatorPool& right) = default; 0053 // Copy constructor 0054 G4AllocatorPool& operator=(const G4AllocatorPool& right); 0055 // Equality operator 0056 0057 inline void* Alloc(); 0058 // Allocate one element 0059 inline void Free(void* b); 0060 // Return an element back to the pool 0061 0062 inline unsigned int Size() const; 0063 // Return storage size 0064 void Reset(); 0065 // Return storage to the free store 0066 0067 inline int GetNoPages() const; 0068 // Return the total number of allocated pages 0069 inline unsigned int GetPageSize() const; 0070 // Accessor for default page size 0071 inline void GrowPageSize(unsigned int factor); 0072 // Increase default page size by a given factor 0073 0074 private: 0075 struct G4PoolLink 0076 { 0077 G4PoolLink* next; 0078 }; 0079 class G4PoolChunk 0080 { 0081 public: 0082 explicit G4PoolChunk(unsigned int sz) 0083 : size(sz) 0084 , mem(new char[size]) 0085 , next(nullptr) 0086 { 0087 ; 0088 } 0089 ~G4PoolChunk() { delete[] mem; } 0090 const unsigned int size; 0091 char* mem; 0092 G4PoolChunk* next; 0093 }; 0094 0095 void Grow(); 0096 // Make pool larger 0097 0098 private: 0099 const unsigned int esize; 0100 unsigned int csize; 0101 G4PoolChunk* chunks = nullptr; 0102 G4PoolLink* head = nullptr; 0103 int nchunks = 0; 0104 }; 0105 0106 // ------------------------------------------------------------ 0107 // Inline implementation 0108 // ------------------------------------------------------------ 0109 0110 // ************************************************************ 0111 // Alloc 0112 // ************************************************************ 0113 // 0114 inline void* G4AllocatorPool::Alloc() 0115 { 0116 if(head == nullptr) 0117 { 0118 Grow(); 0119 } 0120 G4PoolLink* p = head; // return first element 0121 head = p->next; 0122 return p; 0123 } 0124 0125 // ************************************************************ 0126 // Free 0127 // ************************************************************ 0128 // 0129 inline void G4AllocatorPool::Free(void* b) 0130 { 0131 auto* p = static_cast<G4PoolLink*>(b); 0132 p->next = head; // put b back as first element 0133 head = p; 0134 } 0135 0136 // ************************************************************ 0137 // Size 0138 // ************************************************************ 0139 // 0140 inline unsigned int G4AllocatorPool::Size() const { return nchunks * csize; } 0141 0142 // ************************************************************ 0143 // GetNoPages 0144 // ************************************************************ 0145 // 0146 inline int G4AllocatorPool::GetNoPages() const { return nchunks; } 0147 0148 // ************************************************************ 0149 // GetPageSize 0150 // ************************************************************ 0151 // 0152 inline unsigned int G4AllocatorPool::GetPageSize() const { return csize; } 0153 0154 // ************************************************************ 0155 // GrowPageSize 0156 // ************************************************************ 0157 // 0158 inline void G4AllocatorPool::GrowPageSize(unsigned int sz) 0159 { 0160 csize = (sz) != 0u ? sz * csize : csize; 0161 } 0162 0163 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |