![]() |
|
|
Generated: 24 Nov 2008 |
00001 // $Id: AllocatorPool.cpp,v 1.3 2007/05/24 14:39:11 hmd Exp $ 00002 // ============================================================================ 00003 // CVS tag $Name: v26r0 $, version $Revision: 1.3 $ 00004 // ============================================================================ 00010 // ============================================================================ 00011 // 00012 // ******************************************************************** 00013 // * DISCLAIMER * 00014 // * * 00015 // * The following disclaimer summarizes all the specific disclaimers * 00016 // * of contributors to this software. The specific disclaimers,which * 00017 // * govern, are listed with their locations in: * 00018 // * http://cern.ch/geant4/license * 00019 // * * 00020 // * Neither the authors of this software system, nor their employing * 00021 // * institutes,nor the agencies providing financial support for this * 00022 // * work make any representation or warranty, express or implied, * 00023 // * regarding this software system or assume any liability for its * 00024 // * use. * 00025 // * * 00026 // * This code implementation is the intellectual property of the * 00027 // * GEANT4 collaboration. * 00028 // * By copying, distributing or modifying the Program (or any work * 00029 // * based on the Program) you indicate your acceptance of this * 00030 // * statement, and all its terms. * 00031 // ******************************************************************** 00032 // 00033 // ---------------------------------------------------------------------- 00034 // G4AllocatorPool 00035 // 00036 // Implementation file 00037 // 00038 // Author: G.Cosmo, November 2000 00039 // 00040 // ============================================================================ 00041 // Include files 00042 // ============================================================================ 00043 // GaudiKernel 00044 // ============================================================================ 00045 #include "GaudiKernel/AllocatorPool.h" 00046 // ============================================================================ 00047 00048 // ************************************************************ 00049 // G4AllocatorPool constructor 00050 // ************************************************************ 00051 // 00052 GaudiUtils::AllocatorPool::AllocatorPool 00053 ( unsigned int sz ) 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 {} 00058 00059 // ************************************************************ 00060 // G4AllocatorPool copy constructor 00061 // ************************************************************ 00062 // 00063 GaudiUtils::AllocatorPool::AllocatorPool(const AllocatorPool& right) 00064 : esize(right.esize), csize(right.csize) 00065 { 00066 *this = right; 00067 } 00068 00069 // ************************************************************ 00070 // G4AllocatorPool operator= 00071 // ************************************************************ 00072 // 00073 GaudiUtils::AllocatorPool& 00074 GaudiUtils::AllocatorPool::operator= 00075 (const GaudiUtils::AllocatorPool& right) 00076 { 00077 if (&right == this) { return *this; } 00078 chunks = right.chunks; 00079 head = right.head; 00080 nchunks = right.nchunks; 00081 return *this; 00082 } 00083 00084 // ************************************************************ 00085 // G4AllocatorPool destructor 00086 // ************************************************************ 00087 // 00088 GaudiUtils::AllocatorPool::~AllocatorPool() 00089 { 00090 Reset(); 00091 } 00092 00093 // ************************************************************ 00094 // Reset 00095 // ************************************************************ 00096 // 00097 void GaudiUtils::AllocatorPool::Reset() 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 } 00113 00114 // ************************************************************ 00115 // Grow 00116 // ************************************************************ 00117 // 00118 void GaudiUtils::AllocatorPool::Grow() 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 } 00139 00140 00141 // ============================================================================ 00142 // The END 00143 // ============================================================================