G4EnhancedVecAllocator< _Tp > Class Template Reference

#include <G4EnhancedVecAllocator.hh>


Public Member Functions

 G4EnhancedVecAllocator ()
 G4EnhancedVecAllocator (const G4EnhancedVecAllocator< _Tp > &)
template<typename _Tp1>
 G4EnhancedVecAllocator (const G4EnhancedVecAllocator< _Tp1 > &)
 ~G4EnhancedVecAllocator ()
void deallocate (_Tp *_Ptr, size_t _Count)
_Tp * allocate (size_t _Count)

Data Structures

struct  rebind


Detailed Description

template<typename _Tp>
class G4EnhancedVecAllocator< _Tp >

Definition at line 80 of file G4EnhancedVecAllocator.hh.


Constructor & Destructor Documentation

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator (  )  [inline]

Definition at line 87 of file G4EnhancedVecAllocator.hh.

00087 {;}

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp > &   )  [inline]

Definition at line 89 of file G4EnhancedVecAllocator.hh.

00090       : std::allocator<_Tp>() {;}

template<typename _Tp>
template<typename _Tp1>
G4EnhancedVecAllocator< _Tp >::G4EnhancedVecAllocator ( const G4EnhancedVecAllocator< _Tp1 > &   )  [inline]

Definition at line 93 of file G4EnhancedVecAllocator.hh.

00094       : std::allocator<_Tp>() {;}

template<typename _Tp>
G4EnhancedVecAllocator< _Tp >::~G4EnhancedVecAllocator (  )  [inline]

Definition at line 96 of file G4EnhancedVecAllocator.hh.

00096 {;}


Member Function Documentation

template<typename _Tp>
_Tp * G4EnhancedVecAllocator< _Tp >::allocate ( size_t  _Count  ) 

Definition at line 152 of file G4EnhancedVecAllocator.hh.

References G4AllocStats::allocStat, G4ChunkType::isAllocated, G4AllocStats::numCat, G4ChunkIndexType::preAllocated, G4ChunkIndexType::size, G4ChunkIndexType::totalspace, and G4AllocStats::totSpace.

00154 {
00155   size_t totalsize = _Count * sizeof(_Tp);
00156 
00157   G4int found = -1;
00158   for (register int j = 0 ; j < G4AllocStats::numCat ; j++)
00159   {
00160     if ( (G4AllocStats::allocStat != 0)
00161       && (G4AllocStats::allocStat[j].size == totalsize) )
00162     {
00163       found = j;
00164       break;
00165     } 
00166   }   
00167 
00168   if (found == -1)  // Find the new size
00169   {
00170     G4AllocStats::numCat++;
00171     if (G4AllocStats::numCat > G4AllocStats::totSpace)
00172     {
00173       G4AllocStats::totSpace = G4AllocStats::totSpace + 128;
00174         // heuristic parameter for different sizes
00175 
00176       G4AllocStats::allocStat =
00177            (G4ChunkIndexType *) realloc(G4AllocStats::allocStat,
00178            sizeof(G4ChunkIndexType) * G4AllocStats::totSpace);
00179         // This value must be different than zero; otherwise means
00180         // failure in allocating extra space !
00181       // assert(G4AllocStats::allocStat != 0);
00182     }
00183 
00184     G4AllocStats::allocStat[G4AllocStats::numCat-1].size = totalsize;
00185     G4AllocStats::allocStat[G4AllocStats::numCat-1].totalspace = 0;
00186     G4AllocStats::allocStat[G4AllocStats::numCat-1].preAllocated = 0;
00187 
00188     found = G4AllocStats::numCat - 1;
00189 
00190     G4AllocStats::allocStat[found].totalspace = 512;
00191       // heuristic for the number of STL vector instances
00192 
00193     G4AllocStats::allocStat[found].preAllocated =
00194         (G4ChunkType *) realloc(G4AllocStats::allocStat[found].preAllocated,
00195           sizeof(G4ChunkType) * G4AllocStats::allocStat[found].totalspace);
00196       // This value must be different than zero; otherwise means
00197       // failure in allocating extra space for pointers !
00198     // assert(G4AllocStats::allocStat[found].preAllocated != 0);
00199 
00200     char *newSpace1 = (char *) malloc(totalsize * 512);
00201       // This pointer must be different than zero; otherwise means
00202       // failure in allocating extra space for instances !
00203     // assert(newSpace1 != 0);
00204 
00205     for (register int k = 0; k < 512 ; k++)
00206     {
00207       ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 0;
00208       ((G4AllocStats::allocStat[found]).preAllocated[k]).address =
00209                                                     newSpace1+totalsize*k;
00210     }
00211 
00212     ((G4AllocStats::allocStat[found]).preAllocated[0]).isAllocated = 1;
00213     return (_Tp*)(((G4AllocStats::allocStat[found]).preAllocated[0]).address);
00214   }
00215 
00216   // assert(G4AllocStats::allocStat[found].size == totalsize);
00217 
00218   for (register int k = 0; k < G4AllocStats::allocStat[found].totalspace; k++)
00219   {
00220     if (((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated == 0)
00221     { 
00222       ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 1;
00223       return (_Tp*)(((G4AllocStats::allocStat[found]).preAllocated[k]).address);
00224     }
00225   }
00226 
00227   G4int originalchunknumber = G4AllocStats::allocStat[found].totalspace;
00228       
00229   G4AllocStats::allocStat[found].totalspace =      // heuristic for the number
00230     G4AllocStats::allocStat[found].totalspace+512; // of STL vector instances
00231 
00232   G4AllocStats::allocStat[found].preAllocated =
00233     (G4ChunkType *) realloc(G4AllocStats::allocStat[found].preAllocated,
00234       sizeof(G4ChunkType) * G4AllocStats::allocStat[found].totalspace);
00235     // This value must be different than zero; otherwise means
00236     // failure in allocating extra space for pointers !
00237   // assert(G4AllocStats::allocStat[found].preAllocated != 0);
00238 
00239   char *newSpace = (char *) malloc(totalsize * 512);
00240     // This pointer must be different than zero; otherwise means
00241     // failure in allocating extra space for instances !
00242   // assert(newSpace != 0);
00243 
00244   for (register int k = 0; k < 512 ; k++)
00245   {
00246     ((G4AllocStats::allocStat[found]).
00247       preAllocated[originalchunknumber + k]).isAllocated= 0;
00248     ((G4AllocStats::allocStat[found]).
00249       preAllocated[originalchunknumber + k]).address= newSpace+totalsize*k;
00250   }
00251 
00252   ((G4AllocStats::allocStat[found]).preAllocated[originalchunknumber])
00253                                    .isAllocated = 1;
00254 
00255   return (_Tp*)(((G4AllocStats::allocStat[found]).
00256                   preAllocated[originalchunknumber]).address);
00257 }

template<typename _Tp>
void G4EnhancedVecAllocator< _Tp >::deallocate ( _Tp *  _Ptr,
size_t  _Count 
)

Definition at line 117 of file G4EnhancedVecAllocator.hh.

References G4AllocStats::allocStat, G4AllocStats::numCat, and G4ChunkIndexType::totalspace.

00118 {
00119   G4int found = -1;
00120   for (register int j = 0 ; j < G4AllocStats::numCat ; j++)
00121   {
00122     if ( (G4AllocStats::allocStat != 0)
00123       && (G4AllocStats::allocStat[j].size == (_Count * sizeof(_Tp))))
00124     {
00125       found = j;
00126       break;
00127     }
00128   }
00129   // assert(found != -1);
00130 
00131   for (register int k = 0; k < G4AllocStats::allocStat[found].totalspace; k++)
00132   {
00133     if ( ((G4AllocStats::allocStat[found]).preAllocated[k]).address
00134       == ((char *) _Ptr))
00135     {
00136    // assert(((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated==1);
00137       ((G4AllocStats::allocStat[found]).preAllocated[k]).isAllocated = 0;
00138       return;
00139     }
00140   }
00141 }


The documentation for this class was generated from the following file:
Generated on Mon May 27 17:51:54 2013 for Geant4 by  doxygen 1.4.7