BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
strmem.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: strmem.h
4 // Purpose: String Memory Pool (Permits to Save/Copy Strings in own Memory)
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 STRMEM_INCLUDED
27 #define STRMEM_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 MPOOL_INCLUDED
46 #include "../mem/mpool.h"
47 #endif
48 
49 
50 //=============================================================================
51 // Private Constants:
52 //=============================================================================
53 
54 //-----------------------------------------------------------------------------
55 // STRMEM_MAGIC: Magic number (identifies objects of this class).
56 //-----------------------------------------------------------------------------
57 
58 static const long STRMEM_MAGIC = 0x534D450AL; // 'SME\n'
59 
60 
61 //=============================================================================
62 // Class for String Memory Pool (Permits to Save/Copy Strings in OWn Memory)
63 //=============================================================================
64 
65 class strmem_c {
66  public:
67 
68 
69 //-----------------------------------------------------------------------------
70 // Constructor, Destructor:
71 //-----------------------------------------------------------------------------
72 
73  // Constructor:
74  explicit strmem_c(mpool_t mem_pool);
75 
76  // Destructor:
77  ~strmem_c();
78 
79 //-----------------------------------------------------------------------------
80 // (Object) Methods:
81 //-----------------------------------------------------------------------------
82 
83  // insert: Copy a string into this string memory pool, return address.
84  str_t insert(str_t start, str_t end);
85 
86  // num_strings: Returns the number of strings stored in this pool.
87  int num_strings() const
88  {
89  return NumStrings;
90  }
91 
92  // num_pages: Returns the number of memory pages used.
93  int num_pages() const
94  {
95  return NumPages;
96  }
97 
98 #if VER_DUMP
99  // dump: Show data structure.
100  void dump(str_t headline = STR_NULL) const;
101 #endif
102 
103 //-----------------------------------------------------------------------------
104 // Debugging Support:
105 //-----------------------------------------------------------------------------
106 
107 #if VER_DEBUG
108  // Integrity check:
109  public:
110  str_t check() const;
111 
112  // Magic number (identifies objects of this class for debugging).
113  private:
114  long Magic; // Must be "STRMEM_MAGIC".
115 #endif
116 
117 //-----------------------------------------------------------------------------
118 // Copy-constructor and assignment operator are not supported for this class:
119 //-----------------------------------------------------------------------------
120 
121  private:
122 
123  strmem_c(const strmem_c& obj); // Not implemented
124  strmem_c& operator=(const strmem_c& obj); // Not implemented
125 
126 //-----------------------------------------------------------------------------
127 // Private Object Members:
128 //-----------------------------------------------------------------------------
129 
130  private:
131 
132  // Memory Pool for Allocating Storage Pages:
133  mpool_t MemPool;
134 
135  // Current Page for Storing String Data:
136  page_t MemPage;
137 
138  // Pointer to next free byte in current page:
139  char *FreePtr;
140 
141  // Number of free bytes in current page:
142  int FreeBytes;
143 
144  // Number of strings in this string memory pool:
145  int NumStrings;
146 
147  // Number pages used so far:
148  int NumPages;
149 
150 //=============================================================================
151 // End of Class:
152 //=============================================================================
153 
154 };
155 
156 //-----------------------------------------------------------------------------
157 // Define pointer type:
158 //-----------------------------------------------------------------------------
159 
160 typedef strmem_c *strmem_t;
161 
162 //-----------------------------------------------------------------------------
163 // Define null pointer:
164 //-----------------------------------------------------------------------------
165 
166 #define STRMEM_NULL (static_cast<strmem_t>(0))
167 
168 //=============================================================================
169 // End of Include File:
170 //=============================================================================
171 
172 #endif
173 
Definition: mpool.h:103
Definition: strmem.h:65
const char * str_t
Definition: str.h:41
#define STR_NULL
Definition: str.h:52