G4AllocatorPool.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // ----------------------------------------------------------------------
00031 // G4AllocatorPool
00032 //
00033 // Implementation file
00034 //
00035 // Author: G.Cosmo, November 2000
00036 //
00037 
00038 #include "G4AllocatorPool.hh"
00039 
00040 // ************************************************************
00041 // G4AllocatorPool constructor
00042 // ************************************************************
00043 //
00044 G4AllocatorPool::G4AllocatorPool( unsigned int sz )
00045   : esize(sz<sizeof(G4PoolLink) ? sizeof(G4PoolLink) : sz),
00046     csize(sz<1024/2-16 ? 1024-16 : sz*10-16),
00047     chunks(0), head(0), nchunks(0)
00048 {
00049 }
00050 
00051 // ************************************************************
00052 // G4AllocatorPool copy constructor
00053 // ************************************************************
00054 //
00055 G4AllocatorPool::G4AllocatorPool(const G4AllocatorPool& right)
00056   : esize(right.esize), csize(right.csize),
00057     chunks(right.chunks), head(right.head), nchunks(right.nchunks)
00058 {
00059 }
00060 
00061 // ************************************************************
00062 // G4AllocatorPool operator=
00063 // ************************************************************
00064 //
00065 G4AllocatorPool&
00066 G4AllocatorPool::operator= (const G4AllocatorPool& right)
00067 {
00068   if (&right == this) { return *this; }
00069   chunks  = right.chunks;
00070   head    = right.head;
00071   nchunks = right.nchunks;
00072   return *this;
00073 }
00074 
00075 // ************************************************************
00076 // G4AllocatorPool destructor
00077 // ************************************************************
00078 //
00079 G4AllocatorPool::~G4AllocatorPool()
00080 {
00081   Reset();
00082 }
00083 
00084 // ************************************************************
00085 // Reset
00086 // ************************************************************
00087 //
00088 void G4AllocatorPool::Reset()
00089 {
00090   // Free all chunks
00091   //
00092   G4PoolChunk* n = chunks;
00093   G4PoolChunk* p = 0;
00094   while (n)
00095   {
00096     p = n;
00097     n = n->next;
00098     delete p;
00099   }
00100   head = 0;
00101   chunks = 0;
00102   nchunks = 0;
00103 }
00104 
00105 // ************************************************************
00106 // Grow
00107 // ************************************************************
00108 //
00109 void G4AllocatorPool::Grow()
00110 {
00111   // Allocate new chunk, organize it as a linked list of
00112   // elements of size 'esize'
00113   //
00114   G4PoolChunk* n = new G4PoolChunk(csize);
00115   n->next = chunks;
00116   chunks = n;
00117   nchunks++;
00118 
00119   const int nelem = csize/esize;
00120   char* start = n->mem;
00121   char* last = &start[(nelem-1)*esize];
00122   for (char* p=start; p<last; p+=esize)
00123   {
00124     reinterpret_cast<G4PoolLink*>(p)->next
00125       = reinterpret_cast<G4PoolLink*>(p+esize);
00126   }
00127   reinterpret_cast<G4PoolLink*>(last)->next = 0;
00128   head = reinterpret_cast<G4PoolLink*>(start);
00129 }

Generated on Mon May 27 17:47:38 2013 for Geant4 by  doxygen 1.4.7