Chombo + EB  3.2
Arena.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _ARENA_H_
12 #define _ARENA_H_
13 
14 #include <cstddef>
15 
16 #ifdef CH_USE_MEMORY_TRACKING
17 #include <list>
18 #include <cstddef>
19 #endif
20 
21 #include <set>
22 #include <vector>
23 #include "BaseNamespaceHeader.H"
24 
25 #ifdef CH_USE_MEMORY_TRACKING
26 class Arena;
27 typedef std::list<Arena*> ArenaList;
28 #endif
29 
30 using std::set;
31 
32 /// A Virtual Base Class for Dynamic Memory Managemen
33 /**
34 A Virtual Base Class for Dynamic Memory Management
35 
36  This is a virtual base class for objects that manage their own dynamic
37  memory allocation. Since it is a virtual base class, you have to derive
38  something from it to use it.
39 */
40 class Arena
41 {
42 public:
43  /// base class constructor
44  /** base class constructor. registers Arena object with
45  memory tracking system
46  */
47  Arena();
48 
49  /// base class destructor.
50  /** base class destructor. unregisters Arena object with
51  memory tracking system
52  */
53  virtual ~Arena();
54 
55  /**
56  Allocate a dynamic memory arena of size a_sz.
57  A pointer to this memory should be returned.
58  */
59  virtual void* alloc(size_t a_sz) = 0;
60 
61  /**
62  A pure virtual function for deleting the arena pointed to by a_pt.
63  */
64  virtual void free(void* a_pt) = 0;
65 
66  /**
67  Given a minimum required arena size of a_sz bytes, this returns
68  the next largest arena size that will hold an integral number
69  of objects of the largest of the types void*, long,
70  double and function pointer.
71  */
72  static size_t align(size_t a_sz);
73 
74  typedef void (*FP)();
75 
76 #ifdef CH_USE_MEMORY_TRACKING
77  /**data members used by memory tracking system.*/
78 
79  /*@{*/
80  long int bytes;
81  long int peak;
82  static ArenaList* arenaList_;
83  static int NSIZE;
84  char name_[120];
85  /*@}*/
86 #endif
87 
88 
89 protected:
90  //
91  // Types used by align().
92  //
93 #ifndef DOXYGEN
94 
95  union Word
96  {
97  void* p;
98  double d;
99  long l;
100  FP f;
101  };
102 #endif
103 };
104 
105 //
106 // Inlines.
107 //
108 
109 inline size_t Arena::align (size_t a_s)
110 {
111  size_t x = a_s + sizeof(Word) - 1;
112  x -= x % sizeof(Word);
113  return x;
114 }
115 
116 /// A Concrete Class for Dynamic Memory Management
117 /**
118 
119  This is the simplest dynamic memory management class derived from Arena.
120 
121  Makes calls to ::operator new() and ::operator delete().
122 */
123 
124 class BArena: public Arena
125 {
126 public:
127  ///
128  /**
129  optional @param a_name used by memory tracker to distinguish
130  between different memory Arenas
131  */
132  BArena(const char* a_name = "unnamed");
133 
134  BArena(const std::string& a_name);
135 
136  /**: Allocates a dynamic memory arena of size a_sz. Returns a
137  pointer to this memory.
138  */
139  virtual void* alloc (size_t a_sz);
140 
141  /// Deletes the arena pointed to by a_pt.
142  virtual void free (void* a_pt);
143 };
144 
145 /// A Concrete Class for Dynamic Memory Management
146 /**
147 
148  This is a coalescing memory manager. It allocates (possibly) large
149  chunks of heap space and apportions it out as requested. It merges
150  together neighboring chunks on each free().
151 */
152 
153 class CArena: public Arena
154 {
155 public:
156  /**: Construct a coalescing memory manager. `a_hunk_size' is the
157  minimum size of hunks of memory to allocate from the heap.
158  If a_hunk_size == 0 we use DefaultHunkSize as specified below.
159  */
160  CArena(size_t a_hunk_size = 0);
161 
162  /// The destructor.
163  virtual ~CArena();
164 
165  /// Allocate some memory.
166  virtual void* alloc(size_t a_nbytes);
167 
168  /** Free up allocated memory. Merge neighboring free memory chunks
169  into largest possible chunk.
170  */
171  virtual void free(void* a_vp);
172 
173 #if 0
174  /** Mirror the C calloc() function. Returns zero'd memory of
175  size a_nmemb*a_size. This is here so that we can implement
176  malloc(3) and family. Users shouldn't use this function.
177  */
178  void* calloc(size_t a_nmemb,
179  size_t a_size);
180 
181  /** Mirror the C realloc() function. This is here so that we can
182  implement malloc(3) and family. Users shouldn't use this
183  function.
184  */
185  void* realloc (void* a_ptr,
186  size_t a_size);
187 #endif
188 
189  /// The default memory hunk size to grab from the heap.
190  enum
191  {
192  DefaultHunkSize = 1024*1024
193  };
194 
195 
196 protected:
197  //
198  // The nodes in our free list and block list.
199  //
200 #ifndef DOXYGEN
201  class Node
202  {
203  public:
204  //
205  // The default constructor.
206  //
207  Node()
208  :
209  m_block(0),
210  m_size(0)
211  {}
212  //
213  // Another constructor.
214  //
215  Node(void* a_block, size_t a_size)
216  :
217  m_block(a_block),
218  m_size(a_size)
219  {}
220  //
221  // The copy constructor.
222  //
223  Node(const Node& a_rhs)
224  :
225  m_block(a_rhs.m_block),
226  m_size(a_rhs.m_size)
227  {}
228  //
229  // The copy assignment constructor.
230  //
231  Node& operator = (const Node& a_rhs)
232  {
233  m_block = a_rhs.m_block;
234  m_size = a_rhs.m_size;
235  return *this;
236  }
237  //
238  // The "less-than" operator.
239  //
240  bool operator < (const Node& a_rhs) const
241  {
242  return m_block < a_rhs.m_block;
243  }
244  //
245  // The equality operator.
246  //
247  bool operator == (const Node& a_rhs) const
248  {
249  return m_block == a_rhs.m_block;
250  }
251  //
252  // The block address.
253  //
254  void* block() const
255  {
256  return m_block;
257  }
258  //
259  // Set block address.
260  //
261  void block (void* a_blk)
262  {
263  m_block = a_blk;
264  }
265  //
266  // The size of the memory block.
267  //
268  size_t size() const
269  {
270  return m_size;
271  }
272  //
273  // Set size.
274  //
275  void size(size_t a_sz)
276  {
277  m_size = a_sz;
278  }
279 
280  private:
281  //
282  // The block of memory we reference.
283  //
284  void* m_block;
285  //
286  // The size of the block we represent.
287  //
288  size_t m_size;
289  };
290 
291  //
292  // The type of our freelist and blocklist.
293  // We use a set sorted from lo to hi memory addresses.
294  //
295  typedef set < Node > NL;
296 
297  //
298  // The list of blocks allocated via ::operator new().
299  //
300  std::vector<void*> m_alloc;
301 
302  //
303  // The free list of allocated but not currently used blocks.
304  // Maintained in lo to hi memory sorted order.
305  //
306  NL m_freelist;
307 
308  //
309  // The list of busy blocks.
310  // A block is either on the freelist or on the blocklist, but not on both.
311  //
312  NL m_busylist;
313 
314  //
315  // The minimal size of hunks to request via ::operator new().
316  //
317  size_t m_hunk;
318 #endif
319 
320 
321 private:
322  //
323  // Disallowed.
324  //
325  CArena (const CArena& a_rhs);
326  CArena& operator= (const CArena& a_rhs);
327 };
328 
329 //
330 // The Arena used by BaseFab code.
331 //
332 extern Arena* The_FAB_Arena;
333 
334 #include "BaseNamespaceFooter.H"
335 #endif /*CH_ARENA*/
static size_t align(size_t a_sz)
Definition: Arena.H:109
A Concrete Class for Dynamic Memory Management.
Definition: Arena.H:124
virtual void * alloc(size_t a_sz)=0
virtual void free(void *a_pt)=0
Arena()
base class constructor
A Virtual Base Class for Dynamic Memory Managemen.
Definition: Arena.H:40
bool operator<(const FaceIndex &f1, const FaceIndex &f2)
Definition: FaceIndex.H:212
virtual ~Arena()
base class destructor.
A Concrete Class for Dynamic Memory Management.
Definition: Arena.H:153
Arena * The_FAB_Arena
void(* FP)()
Definition: Arena.H:74