OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Pool.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef POOL_H
12 #define POOL_H
13 
14 // include files
15 #include <cstddef>
16 #include <vector>
17 #include <memory.h>
18 class Pool
19 {
20 private:
21  struct Link { Link *next; }; // Point to the next pointer.
22  static inline size_t page() { return 4096-8; }
23  static inline int blocks_in_page(size_t sz)
24  {
25  return (page()>sz)?(page()/sz):1;
26  }
27 
28 public:
29 
30  static inline size_t log2_align() { return 3; }
31  static inline size_t align() { return (1<<log2_align()); }
32  static inline size_t align_mask() { return align()-1; }
33 
34  static inline size_t round_to_align(size_t s)
35  {
36  if (s)
37  s = (s & ~align_mask()) + ((s&align_mask())?align():0);
38  else
39  s = align();
40  return s;
41  }
42 
43 
44  // Null ctor creates an invalid Pool.
45  Pool();
46 
47  // Make a new Pool for objects of a given size.
48  Pool(size_t);
49 
50  // Destroy the Pool.
51  ~Pool();
52 
53  // Allocate a block from the pool.
54  inline void* alloc()
55  {
56  ++outstandingAllocs; // Record an alloc.
57  if ( head==0 ) grow(); // If we're out of space, get more.
58  Link *p = head; // get the top of the stack.
59 
60  // Make the next one the new head of the list.
61  // We can't do head = p->next since p will soon be treated
62  // as something other than a Link. By doing this assignment
63  // with memcpy, we ensure that p->next will be read before
64  // it is clobbered.
65  memcpy(&head, &p->next, sizeof(head));
66 
67  // Return the requested block.
68  return p;
69  }
70 
71  // Release a block to the pool.
72  inline void free(void *b)
73  {
74  --outstandingAllocs; // Record a free.
75  Link *p = (Link*)b; // Cast this to a Link
76  p->next = head; // Makew it point to top of stack.
77  head = p; // Make it the new top.
78  }
79 
80 private:
81  int outstandingAllocs; // Number of blocks given to user.
82  size_t bsize; // How big is each block.
83  size_t nblock; // How many to allocate at once.
84  Link *head; // The first one.
85  std::vector<char*> chunks; // Currently allocated chunks
86  void grow(); // Allocate another chunk and put
87  // its blocks in the free list
88 };
89 
91 
92 #endif // POOL_H
93 
94 /***************************************************************************
95  * $RCSfile: Pool.h,v $ $Author: adelmann $
96  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
97  * IPPL_VERSION_ID: $Id: Pool.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
98  ***************************************************************************/
static int blocks_in_page(size_t sz)
Definition: Pool.h:23
Definition: Pool.h:18
Link * head
Definition: Pool.h:84
size_t bsize
Definition: Pool.h:82
static size_t align_mask()
Definition: Pool.h:32
void grow()
Definition: Pool.cpp:63
std::vector< char * > chunks
Definition: Pool.h:85
int outstandingAllocs
Definition: Pool.h:81
Link * next
Definition: Pool.h:21
static size_t round_to_align(size_t s)
Definition: Pool.h:34
static size_t align()
Definition: Pool.h:31
void free(void *b)
Definition: Pool.h:72
~Pool()
Definition: Pool.cpp:52
void * alloc()
Definition: Pool.h:54
Pool()
Definition: Pool.cpp:34
static size_t page()
Definition: Pool.h:22
static size_t log2_align()
Definition: Pool.h:30
size_t nblock
Definition: Pool.h:83