Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_MemPoolImp.hpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 #include <new>
33 
34 inline
35 Sacado::Fad::MemPool::MemPool(unsigned int elem_size, unsigned int n_elem,
36  unsigned int pre_alloc) :
37  esize(elem_size < sizeof(Link) ? sizeof(Link) : elem_size),
38  n(n_elem),
39  csize(esize*n+sizeof(Chunk)),
40  chunks(NULL),
41  head(NULL),
42  num_chunks(0)
43 {
44  // Pre allocate chunks if required
45  if (pre_alloc)
46  for (unsigned int i=0; i<pre_alloc; i++)
47  grow();
48 }
49 
50 inline
52 {
53  Chunk * nc = chunks;
54  while (nc != NULL) {
55  Chunk * p = nc;
56  nc = nc->next;
57  operator delete((void*) p);
58  }
59 }
60 
61 inline void*
63 {
64  if (head == NULL)
65  grow();
66  Link *p = head;
67  head = p->next;
68 
69  return p;
70 }
71 
72 inline void
74 {
75  if (b == NULL)
76  return;
77  Link *p = static_cast<Link*>(b);
78  p->next = head;
79  head = p;
80 }
81 
82 inline void
84 {
85  // Create a new chunk
86  void *p = operator new(csize);
87  Chunk *c = static_cast<Chunk*>(p);
88  c->mem = static_cast<char*>(p)+sizeof(Chunk);
89  c->next = chunks;
90  chunks = c;
91  ++num_chunks;
92 
93  // Initialize each element in a chunk
94  char *start = c->mem;
95  char *last = &start[(n-1)*esize];
96 
97  for (char *q = start; q<last; q += esize)
98  reinterpret_cast<Link*>(q)->next = reinterpret_cast<Link*>(q+esize);
99  reinterpret_cast<Link*>(last)->next = NULL;
100  head = reinterpret_cast<Link*>(start);
101 }
void free(void *b)
Free an element.
void grow()
Allocate a new chunk.
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
MemPool(unsigned int elem_size, unsigned int n_elem, unsigned int pre_alloc=0)
Constructor. elem_size is the size of elements, n_elem is the number of elements per chunk...
Represents a memory chunk.
void * alloc()
Allocate a new element.
int n