Back to index

CMemoryPool.C

 
//----------------------------------------------------------------------------- 
//  $Header: /tmp_mnt/asis/offline/ceres/cool/project/RCS/CMemoryPool.C,v 2.1 1996/10/04 08:46:03 voigt Exp $ 
// 
//  COOL Program Library   
//  Copyright (C) CERES collaboration, 1996 
// 
//  Implementation of CMemoryPool class. 
// 
//----------------------------------------------------------------------------- 
#include <iostream.h> 
#include <stdlib.h> 
#include "CMemoryPool.h" 
 
// 
//  The following member are private and will never be called 
// 
CMemoryPool::CMemoryPool() : esize(sizeof(Link*)) {} 
CMemoryPool::CMemoryPool(CMemoryPool&) : esize(sizeof(Link*)) {} 
void CMemoryPool::operator= (CMemoryPool&) {} 
 
// 
//  Default constructor 
// 
CMemoryPool::CMemoryPool(unsigned sz)  
   : esize(sz<sizeof(Link*) ? sizeof(Link*) : sz) 
{ 
   head = 0; 
} 
    
// 
//  Empty desctructor 
// 
CMemoryPool::~CMemoryPool() {} 
 
 
// 
//  Member to allocate a new chunk of memory to  
//  hold several objects of size esize. 
// 
void CMemoryPool::grow() 
{ 
   const int overhead = 12; 
   const int chunk_size = 12*1024-overhead; 
   const int nelem = chunk_size/esize; 
    
   char* start = new char[chunk_size]; 
   if (start == 0) { 
      cerr << "CMemoryPool::grow():\n"; 
      cerr << "\tFATAL ERROR\n"; 
      cerr << "\tMemory exhausted. Cannot allocate new chunk of " << chunk_size << " bytes.\n"; 
      cerr << "\tThis is assumed to be fatal. Aborting program.\n"; 
      abort(); 
   } 
   char* last  = &start[(nelem-1)*esize]; 
    
   for (char* p = start; p<last; p+=esize) 
      ((Link*)p)->next = (Link*)(p+esize); 
   ((Link*)last)->next = 0; 
   head = (Link*)start; 
} 

Back to index