OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Pool.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 #include "Utility/Pool.h"
28 #include "Utility/PAssert.h"
29 
30 
31 #include <cstdlib>
32 
33 // Null ctor creates an invalid Pool.
35  : outstandingAllocs(0), // Number of blocks given to user
36  bsize(0), // The size of each block
37  nblock(0), // Number of blocks
38  head(0) // the first one.
39 {
40 }
41 
42 // Make a new Pool for objects of a given size.
43 Pool::Pool(size_t sz)
44  : outstandingAllocs(0), // Number of blocks given to user
45  bsize(round_to_align(sz)), // The size of each block
46  nblock(blocks_in_page(bsize)), // Number of blocks
47  head(0) // the first one.
48 {
49 }
50 
51 // Destroy the Pool.
53 {
54  PInsist(outstandingAllocs==0,"Not all of the pooled memory was freed!");
55 
56  // Loop over the allocated chunks and free them.
57  std::vector<char*>::iterator p, pend = chunks.end();
58  for (p = chunks.begin(); p != pend; ++p)
59  delete [] *p;
60 }
61 
62 // Grow a Pool.
63 void Pool::grow()
64 {
65 
66  size_t alloc_this;
67  if ( bsize>page() )
68  alloc_this = bsize;
69  else
70  alloc_this = page();
71 
72  char *start = new char[alloc_this]; // Allocate aligned space.
73  chunks.push_back(start); // Put it in list of chunks.
74  char *last = start + (nblock-1)*bsize; // Get a pointer to the last one.
75 
76  for (char *p=start; p!=last; p+=bsize) // For all but the last one
77  ((Link*)p)->next = (Link*)(p+bsize); // point to the next.
78  ((Link*)last)->next = head; // The last points to the head
79  head = (Link*)start; // Reset the head to the first.
80 }
81 
82 /***************************************************************************
83  * $RCSfile: Pool.cpp,v $ $Author: adelmann $
84  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
85  * IPPL_VERSION_ID: $Id: Pool.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
86  ***************************************************************************/
Link * head
Definition: Pool.h:84
size_t bsize
Definition: Pool.h:82
void grow()
Definition: Pool.cpp:63
std::vector< char * > chunks
Definition: Pool.h:85
int outstandingAllocs
Definition: Pool.h:81
~Pool()
Definition: Pool.cpp:52
Pool()
Definition: Pool.cpp:34
std::string::iterator iterator
Definition: MSLang.h:16
#define PInsist(c, m)
Definition: PAssert.h:135
static size_t page()
Definition: Pool.h:22
size_t nblock
Definition: Pool.h:83