MFEM  v3.0
 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.googlecode.com.
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 
17 namespace mfem
18 {
19 
20 template <class Elem, int Num>
21 class StackPart
22 {
23 public:
25  Elem Elements[Num];
26 };
27 
28 template <class Elem, int Num>
29 class Stack
30 {
31 private:
32  StackPart <Elem, Num> *TopPart, *TopFreePart;
33  int UsedInTop, SSize;
34 public:
35  Stack() { TopPart = TopFreePart = NULL; UsedInTop = Num; SSize = 0; };
36  int Size() { return SSize; };
37  void Push (Elem E);
38  Elem Pop();
39  void Clear();
40  ~Stack() { Clear(); };
41 };
42 
43 template <class Elem, int Num>
45 {
47  if (UsedInTop == Num)
48  {
49  if (TopFreePart == NULL)
50  aux = new StackPart <Elem, Num>;
51  else
52  TopFreePart = (aux = TopFreePart)->Prev;
53  aux->Prev = TopPart;
54  TopPart = aux;
55  UsedInTop = 0;
56  }
57  TopPart->Elements[UsedInTop++] = E;
58  SSize++;
59 }
60 
61 template <class Elem, int Num>
63 {
65  if (UsedInTop == 0)
66  {
67  TopPart = (aux = TopPart)->Prev;
68  aux->Prev = TopFreePart;
69  TopFreePart = aux;
70  UsedInTop = Num;
71  }
72  SSize--;
73  return TopPart->Elements[--UsedInTop];
74 }
75 
76 template <class Elem, int Num>
78 {
80  while (TopPart != NULL)
81  {
82  TopPart = (aux = TopPart)->Prev;
83  delete aux;
84  }
85  while (TopFreePart != NULL)
86  {
87  TopFreePart = (aux = TopFreePart)->Prev;
88  delete aux;
89  }
90  UsedInTop = Num;
91  SSize = 0;
92 }
93 
94 
95 template <class Elem, int Num>
97 {
98 public:
100  Elem Elements[Num];
101 };
102 
103 template <class Elem, int Num>
104 class MemAlloc
105 {
106 private:
108  int AllocatedInLast;
109  Stack <Elem *, Num> UsedMem;
110 public:
111  MemAlloc() { Last = NULL; AllocatedInLast = Num; };
112  Elem *Alloc();
113  void Free (Elem *);
114  void Clear();
115  ~MemAlloc() { Clear(); };
116 };
117 
118 template <class Elem, int Num>
120 {
122  if (UsedMem.Size() > 0)
123  return UsedMem.Pop();
124  if (AllocatedInLast == Num)
125  {
126  aux = Last;
127  Last = new MemAllocNode <Elem, Num>;
128  Last->Prev = aux;
129  AllocatedInLast = 0;
130  }
131  return &(Last->Elements[AllocatedInLast++]);
132 }
133 
134 template <class Elem, int Num>
136 {
137  UsedMem.Push (E);
138 }
139 
140 template <class Elem, int Num>
142 {
144  while (Last != NULL)
145  {
146  aux = Last->Prev;
147  delete Last;
148  Last = aux;
149  }
150  AllocatedInLast = Num;
151  UsedMem.Clear();
152 }
153 
154 }
155 
156 #endif
Elem * Alloc()
Definition: mem_alloc.hpp:119
int Size()
Definition: mem_alloc.hpp:36
Elem Elements[Num]
Definition: mem_alloc.hpp:25
void Free(Elem *)
Definition: mem_alloc.hpp:135
Elem Elements[Num]
Definition: mem_alloc.hpp:100
StackPart< Elem, Num > * Prev
Definition: mem_alloc.hpp:24
void Push(Elem E)
Definition: mem_alloc.hpp:44
Elem Pop()
Definition: mem_alloc.hpp:62
void Clear()
Definition: mem_alloc.hpp:77
MemAllocNode< Elem, Num > * Prev
Definition: mem_alloc.hpp:99