MFEM  v3.3.2
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
mem_alloc.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_MEM_ALLOC
13 #define MFEM_MEM_ALLOC
14 
15 #include "../config/config.hpp"
16 #include <cstddef>
17 
18 namespace mfem
19 {
20 
21 template <class Elem, int Num>
22 class StackPart
23 {
24 public:
26  Elem Elements[Num];
27 };
28 
29 template <class Elem, int Num>
30 class Stack
31 {
32 private:
33  StackPart <Elem, Num> *TopPart, *TopFreePart;
34  int UsedInTop, SSize;
35 public:
36  Stack() { TopPart = TopFreePart = NULL; UsedInTop = Num; SSize = 0; }
37  int Size() const { return SSize; }
38  void Push (Elem E);
39  Elem Pop();
40  void Clear();
41  size_t MemoryUsage() const;
42  ~Stack() { Clear(); }
43 };
44 
45 template <class Elem, int Num>
47 {
49  if (UsedInTop == Num)
50  {
51  if (TopFreePart == NULL)
52  {
53  aux = new StackPart <Elem, Num>;
54  }
55  else
56  {
57  TopFreePart = (aux = TopFreePart)->Prev;
58  }
59  aux->Prev = TopPart;
60  TopPart = aux;
61  UsedInTop = 0;
62  }
63  TopPart->Elements[UsedInTop++] = E;
64  SSize++;
65 }
66 
67 template <class Elem, int Num>
69 {
71  if (UsedInTop == 0)
72  {
73  TopPart = (aux = TopPart)->Prev;
74  aux->Prev = TopFreePart;
75  TopFreePart = aux;
76  UsedInTop = Num;
77  }
78  SSize--;
79  return TopPart->Elements[--UsedInTop];
80 }
81 
82 template <class Elem, int Num>
84 {
86  while (TopPart != NULL)
87  {
88  TopPart = (aux = TopPart)->Prev;
89  delete aux;
90  }
91  while (TopFreePart != NULL)
92  {
93  TopFreePart = (aux = TopFreePart)->Prev;
94  delete aux;
95  }
96  UsedInTop = Num;
97  SSize = 0;
98 }
99 
100 template <class Elem, int Num>
102 {
103  size_t used_mem = 0;
104  StackPart <Elem, Num> *aux = TopPart;
105  while (aux != NULL)
106  {
107  used_mem += sizeof(StackPart <Elem, Num>);
108  aux = aux->Prev;
109  }
110  aux = TopFreePart;
111  while (aux != NULL)
112  {
113  used_mem += sizeof(StackPart <Elem, Num>);
114  aux = aux->Prev;
115  }
116  // Not counting sizeof(Stack <Elem, Num>)
117  return used_mem;
118 }
119 
120 
121 template <class Elem, int Num>
123 {
124 public:
126  Elem Elements[Num];
127 };
128 
129 template <class Elem, int Num>
130 class MemAlloc
131 {
132 private:
134  int AllocatedInLast;
135  Stack <Elem *, Num> UsedMem;
136 public:
137  MemAlloc() { Last = NULL; AllocatedInLast = Num; }
138  Elem *Alloc();
139  void Free (Elem *);
140  void Clear();
141  size_t MemoryUsage() const;
142  ~MemAlloc() { Clear(); }
143 };
144 
145 template <class Elem, int Num>
147 {
149  if (UsedMem.Size() > 0)
150  {
151  return UsedMem.Pop();
152  }
153  if (AllocatedInLast == Num)
154  {
155  aux = Last;
156  Last = new MemAllocNode <Elem, Num>;
157  Last->Prev = aux;
158  AllocatedInLast = 0;
159  }
160  return &(Last->Elements[AllocatedInLast++]);
161 }
162 
163 template <class Elem, int Num>
165 {
166  UsedMem.Push (E);
167 }
168 
169 template <class Elem, int Num>
171 {
173  while (Last != NULL)
174  {
175  aux = Last->Prev;
176  delete Last;
177  Last = aux;
178  }
179  AllocatedInLast = Num;
180  UsedMem.Clear();
181 }
182 
183 template <class Elem, int Num>
185 {
186  size_t used_mem = UsedMem.MemoryUsage();
187  MemAllocNode <Elem, Num> *aux = Last;
188  while (aux != NULL)
189  {
190  used_mem += sizeof(MemAllocNode <Elem, Num>);
191  aux = aux->Prev;
192  }
193  // Not counting sizeof(MemAlloc <Elem, Num>)
194  return used_mem;
195 }
196 
197 }
198 
199 #endif
Elem * Alloc()
Definition: mem_alloc.hpp:146
Elem Elements[Num]
Definition: mem_alloc.hpp:26
void Free(Elem *)
Definition: mem_alloc.hpp:164
size_t MemoryUsage() const
Definition: mem_alloc.hpp:184
Elem Elements[Num]
Definition: mem_alloc.hpp:126
size_t MemoryUsage() const
Definition: mem_alloc.hpp:101
StackPart< Elem, Num > * Prev
Definition: mem_alloc.hpp:25
int Size() const
Definition: mem_alloc.hpp:37
void Push(Elem E)
Definition: mem_alloc.hpp:46
Elem Pop()
Definition: mem_alloc.hpp:68
void Clear()
Definition: mem_alloc.hpp:83
MemAllocNode< Elem, Num > * Prev
Definition: mem_alloc.hpp:125