Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:28

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 // ============================================================================
0012 /** @file
0013  *  Allocator pool.
0014  *  Class is imported from Geant4 project
0015  *  @date 2006-02-14
0016  */
0017 // ============================================================================
0018 
0019 //
0020 // ********************************************************************
0021 // * DISCLAIMER                                                       *
0022 // *                                                                  *
0023 // * The following disclaimer summarizes all the specific disclaimers *
0024 // * of contributors to this software. The specific disclaimers,which *
0025 // * govern, are listed with their locations in:                      *
0026 // *   http://cern.ch/geant4/license                                  *
0027 // *                                                                  *
0028 // * Neither the authors of this software system, nor their employing *
0029 // * institutes,nor the agencies providing financial support for this *
0030 // * work  make  any representation or  warranty, express or implied, *
0031 // * regarding  this  software system or assume any liability for its *
0032 // * use.                                                             *
0033 // *                                                                  *
0034 // * This  code  implementation is the  intellectual property  of the *
0035 // * GEANT4 collaboration.                                            *
0036 // * By copying,  distributing  or modifying the Program (or any work *
0037 // * based  on  the Program)  you indicate  your  acceptance of  this *
0038 // * statement, and all its terms.                                    *
0039 // ********************************************************************
0040 //
0041 // -------------------------------------------------------------------
0042 //      GEANT 4 class header file
0043 //
0044 // Class description:
0045 //
0046 // Class implementing a memory pool for fast allocation and deallocation
0047 // of memory chunks.  The size of the chunks for small allocated objects
0048 // is fixed to 1Kb and takes into account of memory alignment; for large
0049 // objects it is set to 10 times the object's size.
0050 // The implementation is derived from: B.Stroustrup, The C++ Programming
0051 // Language, Third Edition.
0052 
0053 //           -------------- G4AllocatorPool ----------------
0054 //
0055 // Author: G.Cosmo (CERN), November 2000
0056 // -------------------------------------------------------------------
0057 
0058 #ifndef GAUDIKERNEL_AllocatorPool_h
0059 #define GAUDIKERNEL_AllocatorPool_h 1
0060 
0061 #include "GaudiKernel/Kernel.h"
0062 #include <memory>
0063 
0064 namespace GaudiUtils {
0065   /** @class AllocatorPool AllocatorPool.h GaudiKernel/AllocatorPool.h
0066    *
0067    *  Allocator pool.
0068    *  Class is imported from Geant4 project
0069    *
0070    *  @date 2006-02-14
0071    */
0072   class GAUDI_API AllocatorPool final {
0073   public:
0074     /// Create a pool of elements of size n
0075     explicit AllocatorPool( unsigned int n = 0 );
0076     /// Destructor. Return storage to the free store
0077     ~AllocatorPool();
0078     /// Copy constructor
0079     AllocatorPool( const AllocatorPool& right );
0080     /// Allocate one element
0081     inline void* Alloc();
0082     /// Return an element back to the pool
0083     inline void Free( void* b );
0084     /// Return storage size
0085     inline unsigned int Size() const;
0086     /// Return storage to the free store
0087     void Reset();
0088 
0089   private:
0090     /// Private equality operator
0091     AllocatorPool& operator=( const AllocatorPool& right );
0092 
0093   private:
0094     struct PoolLink final {
0095       PoolLink* next = nullptr;
0096     };
0097     class PoolChunk final {
0098     public:
0099       explicit PoolChunk( unsigned int sz ) : size( sz ), mem{ new char[size] } {}
0100       const unsigned int      size;
0101       std::unique_ptr<char[]> mem;
0102       PoolChunk*              next = nullptr;
0103     };
0104 
0105     /// Make pool larger
0106     void Grow();
0107 
0108   private:
0109     const unsigned int esize;
0110     const unsigned int csize;
0111     PoolChunk*         chunks  = nullptr;
0112     PoolLink*          head    = nullptr;
0113     int                nchunks = 0;
0114   };
0115 
0116 } // end of namespace GaudiUtils
0117 
0118 // ************************************************************
0119 // Alloc
0120 // ************************************************************
0121 //
0122 inline void* GaudiUtils::AllocatorPool::Alloc() {
0123   if ( head == 0 ) { Grow(); }
0124   PoolLink* p = head; // return first element
0125   head        = p->next;
0126   return p;
0127 }
0128 
0129 // ************************************************************
0130 // Free
0131 // ************************************************************
0132 //
0133 inline void GaudiUtils::AllocatorPool::Free( void* b ) {
0134   PoolLink* p = static_cast<PoolLink*>( b );
0135   p->next     = head; // put b back as first element
0136   head        = p;
0137 }
0138 
0139 // ************************************************************
0140 // Size
0141 // ************************************************************
0142 //
0143 inline unsigned int GaudiUtils::AllocatorPool::Size() const { return nchunks * csize; }
0144 
0145 // ============================================================================
0146 // The END
0147 // ============================================================================
0148 #endif