StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMemoryPool.cc
1 /***************************************************************************
2  *
3  * $Id: StMemoryPool.cc,v 1.1 1999/11/09 19:29:11 ullrich Exp $
4  *
5  * Author: Thomas Ullrich, Nov 1999
6  ***************************************************************************
7  *
8  * Description: (see header file)
9  *
10  ***************************************************************************
11  *
12  * $Log: StMemoryPool.cc,v $
13  * Revision 1.1 1999/11/09 19:29:11 ullrich
14  * Initial Revision
15  *
16  **************************************************************************/
17 #include "StMemoryPool.hh"
18 
19 
20 StMemoryPool::StMemoryPool(unsigned int sz)
21  : esize(sz<sizeof(Link*) ? sizeof(Link*) : sz)
22 {
23  head = 0;
24  chunks = 0;
25 }
26 
27 StMemoryPool::~StMemoryPool()
28 {
29  Chunk* n = chunks;
30  while (n) {
31  Chunk* p = n;
32  n = n->next;
33  delete p;
34  }
35 }
36 
37 void
38 StMemoryPool::grow()
39 {
40  Chunk* n = new Chunk;
41  n->next = chunks;
42  chunks = n;
43 
44  const int nelem = Chunk::size/esize;
45  char* start = n->mem;
46  char* last = &start[(nelem-1)*esize];
47 
48  for (char* p = start; p<last; p+=esize)
49  reinterpret_cast<Link*>(p)->next = reinterpret_cast<Link*>(p+esize);
50  reinterpret_cast<Link*>(last)->next = 0;
51  head = reinterpret_cast<Link*>(start);
52 }