FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fei_Pool.cpp
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2005 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8 
9 #include "fei_macros.hpp"
10 #include "fei_Pool.hpp"
11 
12 fei_Pool::fei_Pool(unsigned int sz)
13  : chunks(NULL),
14  esize(sz<sizeof(Link) ? sizeof(Link) : sz),
15  head(NULL)
16 {
17 }
18 
19 fei_Pool::~fei_Pool()
20 {
21  //free all chunks
22  Chunk* n = chunks;
23  while(n) {
24  Chunk* p = n;
25  n = n->next;
26  delete p;
27  }
28 }
29 
30 void
31 fei_Pool::grow()
32 {
33  //allocate new chunk, organize it as a linked list of elements of size 'esize'
34  Chunk* n = new Chunk;
35  n->next = chunks;
36  chunks = n;
37 
38  const int nelem = Chunk::size/esize;
39  char* start = n->mem;
40  char* last = &start[ (nelem-1)*esize ];
41  for(char* p=start; p<last; p+=esize) {
42  reinterpret_cast<Link*>(p)->next = reinterpret_cast<Link*>(p+esize);
43  }
44  reinterpret_cast<Link*>(last)->next = NULL;
45  head = reinterpret_cast<Link*>(start);
46 }
47