Skip to content

Commit bde4622

Browse files
wlammendigama0
andauthored
some corrections (#68)
* some corrections * fix singleton usage * Update src/mmdata.h Co-authored-by: Mario Carneiro <[email protected]> * add attention tag again * completely revert changes to the attention remark in nullPntrStruct. This was accepted in a previous PR * describe pools, fix the explanation of element at offset -3 * typo * fix description of header element -3 again * improve description of used block array * typo * fix brief description of free pool * typo Co-authored-by: Wolf Lammen <[email protected]> Co-authored-by: Mario Carneiro <[email protected]>
1 parent e2c2050 commit bde4622

File tree

3 files changed

+149
-19
lines changed

3 files changed

+149
-19
lines changed

src/metamath.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
/*! \def MVERSION
6262
* The current version of metamath. It is incremented each time the software
6363
* is modified. When main versions are released, the version consists of a
64-
* main version, followed by a dot and a three-digit subversion. Intermediate
65-
* versions are further followed by a free style suffix that should allow
66-
* ordering.
64+
* major version, followed by a dot and a three-digit minor version.
65+
* Pre-release versions are further followed by a free style suffix that
66+
* should allow ordering.
6767
* The version string is extracted and then processed by shell and perl
6868
* scripts. To avoid problems during replacements:
6969
* - use only printable characters from the ASCII range;

src/mmdata.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,144 @@ vstring g_qsortKey; /* Used by qsortStringCmp; pointer only, do not deallocate *
9797
long poolAbsoluteMax = 1000000; /* Pools will be purged when this is reached */
9898
long poolTotalFree = 0; /* Total amount of free space allocated in pool */
9999
/*E*/long i1,j1_,k1; /* 'j1' is a built-in function */
100+
/*!
101+
* \var void** memUsedPool
102+
* \brief pointer to the pool of fragmented memory blocks
103+
* Memory fragmentation is kept simple in Metamath. If a block contains both
104+
* consumed and free space, all of the free space is at the end. Fragmented
105+
* blocks with free space are kept in the used block array, that memUsedPool
106+
* points to. For the notion of block, suballocator see \a memFreePool. This
107+
* scheme supports in particular stack like memory, where data is pushed at and
108+
* popped off the end.
109+
*
110+
* The used blocks array does initially not exist. This is indicated by a
111+
* null value. Once this array is needed, space for it it is allocated from
112+
* the system.
113+
*
114+
* The used block array may only be partially occupied, in which case elements
115+
* at the end of the array are unused. Its current usage is given by
116+
* \a memUsedPoolSize. Its current size including unused elements, is given by
117+
* \a memUsedPoolMax.
118+
*
119+
* \invariant Each block in the used blocks array has its index noted in its
120+
* hidden header, for backward reference.
121+
*
122+
* \attention Desite the name of this variable fully occupied blocks are never
123+
* kept in the used block array.
124+
*/
100125
void **memUsedPool = NULL;
126+
/*!
127+
* \var long memUsedPoolSize
128+
* \attention this is the number of individual blocks, not the accumulated
129+
* (unused) bytes contained.
130+
*
131+
* The Metamath suballocator holds fragmented blocks in a used block array.
132+
* The number of occupied entries is kept in this variable. Elements at the
133+
* end of the used block array may be unused. The fill size is given by this
134+
* variable. For further information see \a memUsedPool.
135+
*
136+
* \invariant memUsedPoolSize <= \a memUsedPoolMax.
137+
*/
101138
long memUsedPoolSize = 0; /* Current # of partially filled arrays in use */
139+
/*!
140+
* \var long memUsedPoolMax
141+
* \attention this is the number of individual free blocks, not the accumulated
142+
* bytes contained.
143+
*
144+
* The Metamath suballocator holds fragmented blocks in the used block
145+
* array. This array may only partially be occupied. Its total capacity is
146+
* kept in this variable. For further information see \a memUsedPool.
147+
*
148+
* This variable may grow during a reallocation process.
149+
*
150+
* \invariant (memUsedPoolMax > 0) == (\a memUsedPool != 0)
151+
*/
102152
long memUsedPoolMax = 0; /* Maximum # of entries in 'in use' table (grows
103153
as nec.) */
154+
/*!
155+
* \var void** memFreePool
156+
* \brief pointer to the pool of completely free memory blocks
157+
*
158+
* **suballocator**
159+
*
160+
* Metamath does not free memory by returning it to the operating system again.
161+
* Instead it has a suballocator that marks them as unused. During execution
162+
* chunks of memory become unused, either completely, or through fragmentation.
163+
* The suballocator keeps track of these by entering them either into a free
164+
* block array, or into a used block array. The idea behind this suballocation
165+
* scheme is to reduce the number of system malloc and alloc calls.
166+
*
167+
* Although the suballocator tries to avoid returning memory to the system, it
168+
* can do so under extreme memory constraints.
169+
*
170+
* The suballocator is initially neither equipped with a free block array,
171+
* pointed to by memFreePool, or a used block array, see \a memUsedPool. Both
172+
* are null then, but once memory is returned to the suballocator again, it
173+
* allocates some space for the needed array.
174+
*
175+
* The free block array contains totally unused blocks. The array may only
176+
* partially be occupied, in which case The elements at the end are the unused
177+
* ones. Its current fill size is given by \a memFreePoolSize. Its capacity
178+
* is given by \a memFreePoolMax.
179+
*
180+
* Fragmented blocks are kept in a separate \a memUsedPool. The suballocator
181+
* never tracks fully used blocks.
182+
*
183+
* **block of memory**
184+
*
185+
* Each block used by the suballocater is formally treated as an array of
186+
* pointer (void*). It is divided into an administrative header, followed by
187+
* elements reserved for application data. The header is assigned elements -3
188+
* to -1 in the formal array, so that application data starts with element 0.
189+
* A pointer to the block always refers to element 0, so the header appears
190+
* somewhat hidden.
191+
*
192+
* The header elements are reinterpreted as long integer. The values support
193+
* a stack, where data is pushed at and popped off the end during the course of
194+
* execution. The semantics of the header elements are:
195+
*
196+
* offset -1:\n
197+
* is the current size of the stack (in bytes, not elements!),
198+
* without header data. When interpreted as an offset into the stack, it
199+
* references the first element past the top of the stack.
200+
*
201+
* offset -2:\n
202+
* the allocated size of the array, in bytes, not counting the
203+
* header. When used as a stack, it marks the limit where the stack
204+
* overflows.
205+
*
206+
* offset -3:\n
207+
* If this block has free space at the end (is fragmented), then it contains
208+
* its index in the used blocks array, see \a memUsedPool. A value of -1
209+
* indicates it has no free space left, hence is not held in this pool.
210+
*/
104211
void **memFreePool = NULL;
212+
/*!
213+
* \var long memFreePoolSize
214+
* \attention this is the number of individual free blocks, not the accumulated
215+
* bytes contained.
216+
*
217+
* The Metamath suballocator holds free blocks in a free block array. The
218+
* number of occupied entries is kept in this variable. Elements at the end of
219+
* the free block array may not be used. The fill size is given by this
220+
* variable. For further information see \a memFreePool.
221+
*
222+
* \invariant memFreePoolSize <= \a memFreePoolMax.
223+
*/
105224
long memFreePoolSize = 0; /* Current # of available, allocated arrays */
225+
/*!
226+
* \var long memFreePoolMax
227+
* \attention this is the number of individual free blocks, not the accumulated
228+
* bytes contained.
229+
*
230+
* The Metamath suballocator holds free blocks in a free block array. It may
231+
* only partially be occupied. Its total capacity is kept in this variable. For
232+
* further information see \a memFreePool.
233+
*
234+
* This variable may grow during a reallocation process.
235+
*
236+
* \invariant (memFreePoolMax > 0) == (\a memFreePool != 0)
237+
*/
106238
long memFreePoolMax = 0; /* Maximum # of entries in 'free' table (grows
107239
as nec.) */
108240

src/mmdata.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
*/
2222
typedef char flag;
2323

24-
/*!
24+
/*!
25+
* \deprecated
2526
* \var flag g_listMode.
2627
* Obsolete. Now fixed to 0. Historically the metamath sources were also used
2728
* for other purposes than maintaining Metamath files. One such application, a
@@ -51,19 +52,9 @@ typedef long nmbrString; /* String of numbers */
5152
* addressed with offsets -1 to -3 relative to the first element of the stack.
5253
* This allows for easy embedding of a stack within an even larger memory
5354
* pool. The fields of this hidden structure, although formally pointers,
54-
* are loaded with long int values describing properties of the stack:
55-
*
56-
* offset -1: is the current size of the stack (in bytes, not elements!). When
57-
* interpreted as an offset into the stack, it references the first element
58-
* past the top of the stack.
59-
*
60-
* offset -2: the allocated size of the array, in bytes. When used with a
61-
* stack, it marks the limit where the stack overflows. Current size
62-
* <= allocated size is an invariant of this structure.
63-
*
64-
* offset -3. If this array is a subarray (or sub-stack) of a larger pool of
65-
* pointers, then it marks the offset in bytes of the heading structure in the
66-
* larger pool.
55+
* are loaded with long int values describing properties of the stack.
56+
*
57+
* For details see \a memFreePool.
6758
*/
6859
typedef void* pntrString; /* String of pointers */
6960

@@ -276,19 +267,22 @@ struct nullPntrStruct {
276267
*/
277268
long poolLoc;
278269
/*!
279-
* allocated size of the memory block containing the \a pntrString.
270+
* allocated size of the memory block containing the \a pntrString,
271+
* excluding any hidden administrative data.
280272
* Note: this is the number of bytes, not elements! Fixed to the size of a
281273
* single void* instance.
282274
*/
283275
long allocSize;
284276
/*!
285-
* currently used size of the memory block containing the \a pntrString.
277+
* currently used size of the memory block containing the \a pntrString,
278+
* excluding any hidden administrative data.
286279
* Note: this is the number of bytes, not elements! Fixed to the size of a
287280
* single pointer element.
288281
*/
289282
long actualSize;
290283
/*!
291284
* memory for a single void* instance, set and fixed to the null pointer.
285+
* A null marks the end of the array.
292286
*/
293287
pntrString nullElement; };
294288
/*!
@@ -299,6 +293,10 @@ struct nullPntrStruct {
299293
* \attention mark as const
300294
*/
301295
extern struct nullPntrStruct g_PntrNull;
296+
/*!
297+
* \def NULL_PNTRSTRING
298+
* yields the address of a global null pointer element.
299+
*/
302300
#define NULL_PNTRSTRING &(g_PntrNull.nullElement)
303301
#define pntrString_def(x) pntrString *x = NULL_PNTRSTRING
304302
#define free_pntrString(x) pntrLet(&x, NULL_PNTRSTRING)

0 commit comments

Comments
 (0)