CUBRID Engine  latest
zoneheap.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /*
4 
5  Heap Layers: An Extensible Memory Allocation Infrastructure
6 
7  Copyright (C) 2000-2020 by Emery Berger
8  http://www.emeryberger.com
9  emery@cs.umass.edu
10 
11  Heap Layers is distributed under the terms of the Apache 2.0 license.
12 
13  You may obtain a copy of the License at
14  http://www.apache.org/licenses/LICENSE-2.0
15 
16 */
17 
28 #ifndef HL_ZONEHEAP_H
29 #define HL_ZONEHEAP_H
30 
31 #include <assert.h>
32 
33 #include "utility/align.h"
34 #include "wrappers/mallocinfo.h"
35 
36 namespace HL {
37 
38  template <class SuperHeap, size_t ChunkSize>
39  class ZoneHeap : public SuperHeap {
40  public:
41 
42  enum { Alignment = SuperHeap::Alignment };
43 
45  : _sizeRemaining (0),
48  {
49  m_chkSize = ChunkSize;
50  }
51 
53  {
54  // printf ("deleting arenas!\n");
55  // Delete all of our arenas.
56  Arena * ptr = _pastArenas;
57  while (ptr != NULL) {
58  void * oldPtr = (void *) ptr;
59  ptr = ptr->nextArena;
60  //printf ("deleting %x\n", ptr);
61  SuperHeap::free (oldPtr);
62  }
63  if (_currentArena != NULL)
64  //printf ("deleting %x\n", _currentArena);
65  SuperHeap::free ((void *) _currentArena);
66  }
67 
68  inline void reset(const int chkSize)
69  {
70  m_chkSize = chkSize;
71  }
72 
73  inline void * malloc (size_t sz) {
74  void * ptr = zoneMalloc (sz);
75  // assert ((size_t) ptr % Alignment == 0);
76  return ptr;
77  }
78 
80  inline void free (void *) {}
81 
83  inline int remove (void *) { return 0; }
84 
85 
86  private:
87 
88  int m_chkSize;
89 
90  ZoneHeap (const ZoneHeap&);
91  ZoneHeap& operator=(const ZoneHeap&);
92 
93  inline void * zoneMalloc (size_t sz) {
94  void * ptr;
95  // Round up size to an aligned value.
96  sz = HL::align<HL::MallocInfo::Alignment>(sz);
97  // Get more space in our arena if there's not enough room in this one.
98  if ((_currentArena == NULL) || (_sizeRemaining < sz)) {
99  // First, add this arena to our past arena list.
100  if (_currentArena != NULL) {
103  }
104  // Now get more memory.
105  size_t allocSize = m_chkSize;
106  if (allocSize < sz) {
107  allocSize = sz;
108  }
109  _currentArena =
110  (Arena *) SuperHeap::malloc (allocSize + sizeof(Arena));
111  if (_currentArena == NULL) {
112  return NULL;
113  }
114  _currentArena->arenaSpace = (char *) (_currentArena + 1);
116  _sizeRemaining = allocSize;
117  }
118  // Bump the pointer and update the amount of memory remaining.
119  _sizeRemaining -= sz;
120  ptr = _currentArena->arenaSpace;
121  _currentArena->arenaSpace += sz;
122  assert (ptr != NULL);
123  // assert ((size_t) ptr % SuperHeap::Alignment == 0);
124  return ptr;
125  }
126 
127  class Arena {
128  public:
129  Arena() {
130  static_assert((sizeof(Arena) % HL::MallocInfo::Alignment == 0),
131  "Alignment must match Arena size.");
132  }
133 
135  char * arenaSpace;
136  };
137 
140 
143 
146  };
147 
148 }
149 
150 #endif
Arena * _currentArena
The current arena.
Definition: zoneheap.h:142
Definition: heaplayers.h:34
size_t _sizeRemaining
Space left in the current arena.
Definition: zoneheap.h:139
#define assert(x)
int m_chkSize
Definition: zoneheap.h:88
void * zoneMalloc(size_t sz)
Definition: zoneheap.h:93
Arena * _pastArenas
A linked list of past arenas.
Definition: zoneheap.h:145
#define NULL
Definition: freelistheap.h:34
ZoneHeap & operator=(const ZoneHeap &)
void * malloc(size_t sz)
Definition: zoneheap.h:73
void reset(const int chkSize)
Definition: zoneheap.h:68
void free(void *)
Free in a zone allocator is a no-op.
Definition: zoneheap.h:80