BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
mpool.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: mpool.h
4 // Purpose: Pool of memory pages (allocation, deallocation, statistics).
5 // Last Change: 04.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 2015-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
27 //=============================================================================
28 // Include File Frame:
29 //=============================================================================
30 
31 #ifndef MPOOL_INCLUDED
32 #define MPOOL_INCLUDED
33 
34 //=============================================================================
35 // Used Types and Macros:
36 //=============================================================================
37 
38 #ifndef VER_INCLUDED
39 #include "../base/ver.h"
40 #endif
41 
42 #ifndef STR_INCLUDED
43 #include "../base/str.h"
44 #endif
45 
46 #ifndef CHECK_INCLUDED
47 #include "../base/check.h"
48 #endif
49 
50 #ifndef PAGE_INCLUDED
51 #include "page.h"
52 #endif
53 
54 
55 //=============================================================================
56 // Private Constants:
57 //=============================================================================
58 
59 //-----------------------------------------------------------------------------
60 // MPOOL_MAGIC: Magic number (identifies objects of this class).
61 //-----------------------------------------------------------------------------
62 
63 static const long MPOOL_MAGIC = 0x4D504F0AL; // 'MPO\n'
64 
65 //-----------------------------------------------------------------------------
66 // MPOOL_PAGEARRAY_SIZE: Number of places in local array of allocated pages.
67 //-----------------------------------------------------------------------------
68 
69 static const int MPOOL_PAGEARRAY_SIZE = 10;
70 
71  // This reduces the overhead of keeping track of allocated pages
72  // if only a few pages are allocated.
73  // If more pages are allocated than fit in the array,
74  // entire pages are used to store the addresses of allocated pages.
75 
76 //=============================================================================
77 // Class for Pool of Dynamic Memory Pages:
78 //=============================================================================
79 
103 class mpool_c {
104  public:
105 
106 
107 //-----------------------------------------------------------------------------
108 // Constructor, Destructor:
109 //-----------------------------------------------------------------------------
110 
111  // Constructor:
112  mpool_c(str_t name);
113 
114  // Destructor (frees all allocated pages of this memory pool):
115  ~mpool_c();
116 
117 //-----------------------------------------------------------------------------
118 // Class Methods:
119 //-----------------------------------------------------------------------------
120 
121  // first_pool: Start of linked list of all memory pools.
122  static mpool_c* first_pool()
123  {
124  return FirstPool;
125  }
126 
127 //-----------------------------------------------------------------------------
128 // (Object) Methods:
129 //-----------------------------------------------------------------------------
130 
131  // alloc_page: Allocate a memory page.
132  page_t alloc_page();
133 
134  // next_pool: Follow link to next memory pool.
135  mpool_c* next_pool() const
136  {
137  CHECK_VALID("mpool_c::next_pool");
138  return NextPool;
139  }
140 
141  // name: Get name of this memory pool (for printing statistics):
142  str_t name() const
143  {
144  CHECK_VALID("mpool_c::name");
145  return Name;
146  }
147 
148  // num_alloc_pages: Number of mem pages that are currently allocated.
149  int num_alloc_pages() const
150  {
151  CHECK_VALID("mpool_c::num_alloc_pages");
152  return NumAllocPages;
153  }
154 
155 
156 //-----------------------------------------------------------------------------
157 // Debugging Support:
158 //-----------------------------------------------------------------------------
159 
160 #if VER_DEBUG
161  // Integrity check:
162  public:
163  str_t check() const;
164 
165  // Magic number (identifies objects of this class for debugging).
166  private:
167  long Magic; // Must be "MPOOL_MAGIC".
168 #endif
169 
170 #if VER_DUMP
171  // dump: Show data structure.
172  public:
173  void dump(str_t headline = STR_NULL) const;
174 #endif
175 
176 
177 //-----------------------------------------------------------------------------
178 // Copy-constructor and assignment operator are not supported for this class:
179 //-----------------------------------------------------------------------------
180 
181  private:
182 
183  mpool_c(const mpool_c& mpool); // Not implemented
184  mpool_c& operator=(const mpool_c& mpool); // Not implemented
185 
186 
187 //-----------------------------------------------------------------------------
188 // Private Class Members:
189 //-----------------------------------------------------------------------------
190 
191  private:
192 
193  // Linked list of all memory pools (for printing statistics).
194  static mpool_c *FirstPool;
195  static mpool_c *LastPool;
196 
197  // Total number of memory pages (allocated by any pool).
198  static int TotalPages;
199 
200 //-----------------------------------------------------------------------------
201 // Private Object Members:
202 //-----------------------------------------------------------------------------
203 
204  private:
205 
206  // Name of this memory pool:
207  str_t Name;
208 
209  // Current number of allocated memory pages.
210  int NumAllocPages;
211 
212  // Doubly linked list of all memory pools:
213  mpool_c *NextPool;
214  mpool_c *PrevPool;
215 
216  // Array for first allocated pages (if only a few pages are needed):
217  page_t PageDirArr[MPOOL_PAGEARRAY_SIZE];
218 
219  // Linked list of pages with links to allocated pages:
220  page_t *FirstDirPage;
221 
222  // Current page of the page directory (last in the list):
223  page_t *CurrDirPage;
224 
225  // Number of free places in the current page of the page directory:
226  int CurrDirFree;
227 
228  // Pointer to the next free place in the current directory page:
229  page_t *CurrDirPtr;
230 
231 //=============================================================================
232 // End of Class:
233 //=============================================================================
234 
235 };
236 
237 //-----------------------------------------------------------------------------
238 // Define pointer type:
239 //-----------------------------------------------------------------------------
240 
241 typedef mpool_c *mpool_t;
242 
243 //-----------------------------------------------------------------------------
244 // Define null pointer:
245 //-----------------------------------------------------------------------------
246 
247 #define MPOOL_NULL (static_cast<mpool_t>(0))
248 
249 //=============================================================================
250 // End of Include File:
251 //=============================================================================
252 
253 #endif
254 
Definition: mpool.h:103
Memory page.
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
#define STR_NULL
Definition: str.h:52