Sierra Toolkit  Version of the Day
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 <stk_util/util/Pool.hpp>
10 
11 namespace stk_classic {
12 namespace util {
13 
14 Pool::Pool(unsigned int sz)
15  : chunks(NULL),
16  esize(sz<sizeof(Link) ? sizeof(Link) : sz),
17  head(NULL)
18 {
19 }
20 
21 Pool::~Pool()
22 {
23  //free all chunks
24  Chunk* n = chunks;
25  while(n) {
26  Chunk* p = n;
27  n = n->next;
28  delete p;
29  }
30 }
31 
32 void
33 Pool::grow()
34 {
35  //allocate new chunk, organize it as a linked list of elements of size 'esize'
36  Chunk* n = new Chunk;
37  n->next = chunks;
38  chunks = n;
39 
40  const int nelem = Chunk::size/esize;
41  char* start = n->mem;
42  char* last = &start[ (nelem-1)*esize ];
43  for(char* p=start; p<last; p+=esize) {
44  reinterpret_cast<Link*>(p)->next = reinterpret_cast<Link*>(p+esize);
45  }
46  reinterpret_cast<Link*>(last)->next = NULL;
47  head = reinterpret_cast<Link*>(start);
48 }
49 
50 }//namespace util
51 }//namespace stk_classic
52 
Sierra Toolkit.