BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
mem.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: mem.h
4 // Purpose: Big pieces of dynamic memory, from which pages are taken
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 
22 //=============================================================================
23 // Include File Frame:
24 //=============================================================================
25 
26 #ifndef MEM_INCLUDED
27 #define MEM_INCLUDED
28 
29 //=============================================================================
30 // Used Types and Macros:
31 //=============================================================================
32 
33 #ifndef VER_INCLUDED
34 #include "../base/ver.h"
35 #endif
36 
37 #ifndef STR_INCLUDED
38 #include "../base/str.h"
39 #endif
40 
41 #ifndef CHECK_INCLUDED
42 #include "../base/check.h"
43 #endif
44 
45 #ifndef PAGE_INCLUDED
46 #include "page.h"
47 #endif
48 
49 
50 //=============================================================================
51 // Private Constants:
52 //=============================================================================
53 
54 //-----------------------------------------------------------------------------
55 // MEM_MAGIC: Magic number (identifies objects of this class).
56 //-----------------------------------------------------------------------------
57 
58 static const long MEM_MAGIC = 0x4D454D0AL; // 'MEM\n'
59 
60 //-----------------------------------------------------------------------------
61 // MEM_CHUNK_PAGES: Number of memory pages per chunk.
62 //-----------------------------------------------------------------------------
63 
64 static const int MEM_CHUNK_PAGES = 1022; // Chunk size slightly < 4MB
65 
66 //=============================================================================
67 // Class for Management of Dynamic Memory Pages:
68 //=============================================================================
69 
70 class mem_c {
71  public:
72 
73 //-----------------------------------------------------------------------------
74 // Class Methods:
75 //-----------------------------------------------------------------------------
76 
77  // alloc_page: Get a new memory page.
78  static page_t alloc_page();
79 
80  // free_page: Deallocate a memory page (enter into free list).
81  static void free_page(page_t page);
82 
83  // num_alloc_pages: Get current number of allocated memory pages.
84  static int num_alloc_pages() {
85  return NumAllocPages;
86  }
87 
88  // num_chunks: Get number of allocated memory chunks.
89  static int num_chunks() {
90  return NumChunks;
91  }
92 
93 
94 //-----------------------------------------------------------------------------
95 // Debugging Support:
96 //-----------------------------------------------------------------------------
97 
98 #if VER_DEBUG
99  // Integrity check:
100  public:
101  str_t check() const;
102 
103  // Magic number (identifies objects of this class for debugging).
104  private:
105  long Magic; // Must be "MEM_MAGIC".
106 #endif
107 
108 //-----------------------------------------------------------------------------
109 // Constructor, Destructor (not used):
110 //-----------------------------------------------------------------------------
111 
112  private:
113 
114  // Constructor:
115  mem_c(); // Not implemented - use init()
116 
117  // Destructor:
118  ~mem_c(); // Not implemented
119 
120 //-----------------------------------------------------------------------------
121 // Copy-constructor and assignment operator are not supported for this class:
122 //-----------------------------------------------------------------------------
123 
124  private:
125 
126  mem_c(const mem_c& mem); // Not implemented
127  mem_c& operator=(const mem_c& mem); // Not implemented
128 
129 
130 //-----------------------------------------------------------------------------
131 // Auxiliary Methods:
132 //-----------------------------------------------------------------------------
133 
134  private:
135 
136  // Initialize object (replaces constructor):
137  void init();
138 
139 //-----------------------------------------------------------------------------
140 // Private Class Members:
141 //-----------------------------------------------------------------------------
142 
143  private:
144 
145  // Linked list of all memory chunks.
146  static mem_c *FirstChunk;
147  static mem_c *LastChunk;
148 
149  // Total number of allocated memory chunks.
150  static int NumChunks;
151 
152  // Linked list of free memory pages:
153  static page_t FreePageList;
154 
155  // Total number of allocated memory pages.
156  static int NumAllocPages;
157 
158 
159 //-----------------------------------------------------------------------------
160 // Private Object Members:
161 //-----------------------------------------------------------------------------
162 
163  private:
164 
165  // Number of free memory pages in this chunk:
166  int NumFreePages;
167 
168  // Pointer to first memory page:
169  VER_BYTE *FirstPage;
170 
171  // Pointer to next memory page:
172  VER_BYTE *NextPage;
173 
174  // Linked list of all allocated memory chunks:
175  mem_c *NextChunk;
176 
177  // Memory:
178  VER_BYTE Memory[VER_PAGESIZE * MEM_CHUNK_PAGES + (VER_PAGESIZE - 1)];
179 
180 //=============================================================================
181 // End of Class:
182 //=============================================================================
183 
184 };
185 
186 //-----------------------------------------------------------------------------
187 // Define pointer type:
188 //-----------------------------------------------------------------------------
189 
190 typedef mem_c *mem_t;
191 
192 //-----------------------------------------------------------------------------
193 // Define null pointer:
194 //-----------------------------------------------------------------------------
195 
196 #define MEM_NULL (static_cast<mem_t>(0))
197 
198 //=============================================================================
199 // End of Include File:
200 //=============================================================================
201 
202 #endif
203 
#define VER_PAGESIZE
Definition: ver.h:186
Definition: mem.h:70
Memory page.
const char * str_t
Definition: str.h:41
char VER_BYTE
Definition: ver.h:758