MFEM  v3.1
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() { return SSize; }
38  void Push (Elem E);
39  Elem Pop();
40  void Clear();
41  ~Stack() { Clear(); }
42 };
43 
44 template <class Elem, int Num>
46 {
48  if (UsedInTop == Num)
49  {
50  if (TopFreePart == NULL)
51  {
52  aux = new StackPart <Elem, Num>;
53  }
54  else
55  {
56  TopFreePart = (aux = TopFreePart)->Prev;
57  }
58  aux->Prev = TopPart;
59  TopPart = aux;
60  UsedInTop = 0;
61  }
62  TopPart->Elements[UsedInTop++] = E;
63  SSize++;
64 }
65 
66 template <class Elem, int Num>
68 {
70  if (UsedInTop == 0)
71  {
72  TopPart = (aux = TopPart)->Prev;
73  aux->Prev = TopFreePart;
74  TopFreePart = aux;
75  UsedInTop = Num;
76  }
77  SSize--;
78  return TopPart->Elements[--UsedInTop];
79 }
80 
81 template <class Elem, int Num>
83 {
85  while (TopPart != NULL)
86  {
87  TopPart = (aux = TopPart)->Prev;
88  delete aux;
89  }
90  while (TopFreePart != NULL)
91  {
92  TopFreePart = (aux = TopFreePart)->Prev;
93  delete aux;
94  }
95  UsedInTop = Num;
96  SSize = 0;
97 }
98 
99 
100 template <class Elem, int Num>
102 {
103 public:
105  Elem Elements[Num];
106 };
107 
108 template <class Elem, int Num>
109 class MemAlloc
110 {
111 private:
113  int AllocatedInLast;
114  Stack <Elem *, Num> UsedMem;
115 public:
116  MemAlloc() { Last = NULL; AllocatedInLast = Num; }
117  Elem *Alloc();
118  void Free (Elem *);
119  void Clear();
120  ~MemAlloc() { Clear(); }
121 };
122 
123 template <class Elem, int Num>
125 {
127  if (UsedMem.Size() > 0)
128  {
129  return UsedMem.Pop();
130  }
131  if (AllocatedInLast == Num)
132  {
133  aux = Last;
134  Last = new MemAllocNode <Elem, Num>;
135  Last->Prev = aux;
136  AllocatedInLast = 0;
137  }
138  return &(Last->Elements[AllocatedInLast++]);
139 }
140 
141 template <class Elem, int Num>
143 {
144  UsedMem.Push (E);
145 }
146 
147 template <class Elem, int Num>
149 {
151  while (Last != NULL)
152  {
153  aux = Last->Prev;
154  delete Last;
155  Last = aux;
156  }
157  AllocatedInLast = Num;
158  UsedMem.Clear();
159 }
160 
161 }
162 
163 #endif
Elem * Alloc()
Definition: mem_alloc.hpp:124
int Size()
Definition: mem_alloc.hpp:37
Elem Elements[Num]
Definition: mem_alloc.hpp:26
void Free(Elem *)
Definition: mem_alloc.hpp:142
Elem Elements[Num]
Definition: mem_alloc.hpp:105
StackPart< Elem, Num > * Prev
Definition: mem_alloc.hpp:25
void Push(Elem E)
Definition: mem_alloc.hpp:45
Elem Pop()
Definition: mem_alloc.hpp:67
void Clear()
Definition: mem_alloc.hpp:82
MemAllocNode< Elem, Num > * Prev
Definition: mem_alloc.hpp:104