fei_Pool.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include "fei_macros.hpp"
00010 #include "fei_Pool.hpp"
00011 
00012 fei_Pool::fei_Pool(unsigned int sz)
00013  : chunks(NULL),
00014    esize(sz<sizeof(Link) ? sizeof(Link) : sz),
00015    head(NULL)
00016 {
00017 }
00018 
00019 fei_Pool::~fei_Pool()
00020 {
00021   //free all chunks
00022   Chunk* n = chunks;
00023   while(n) {
00024     Chunk* p = n;
00025     n = n->next;
00026     delete p;
00027   }
00028 }
00029 
00030 void
00031 fei_Pool::grow()
00032 {
00033   //allocate new chunk, organize it as a linked list of elements of size 'esize'
00034   Chunk* n = new Chunk;
00035   n->next = chunks;
00036   chunks = n;
00037 
00038   const int nelem = Chunk::size/esize;
00039   char* start = n->mem;
00040   char* last = &start[ (nelem-1)*esize ];
00041   for(char* p=start; p<last; p+=esize) {
00042     reinterpret_cast<Link*>(p)->next = reinterpret_cast<Link*>(p+esize);
00043   }
00044   reinterpret_cast<Link*>(last)->next = NULL;
00045   head = reinterpret_cast<Link*>(start);
00046 }
00047 

Generated on Wed May 12 21:30:41 2010 for FEI by  doxygen 1.4.7