Skip to content

Commit bc48424

Browse files
committed
describe temporary stacks in more detail
1 parent d7ebf77 commit bc48424

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/mmdata.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,32 @@ vstring g_qsortKey; /* Used by qsortStringCmp; pointer only, do not deallocate *
167167
* ubiquitous and frequent, that the processor, and all relevant program
168168
* languages provide simple mechanisms for de/allocation of such __local__
169169
* data. Metamath is no exception to this.
170+
*
170171
* While the C compiler silently cares about __local__ variables, it must not
171172
* interfere with data managed by a \ref suballocation "Suballocator". Instead
172173
* of tracking all __locally__ created memory individually for later
173-
* deallocation, a stack like \ref Pool "pool" allows a command like
174-
* `free all memory` that has been created after a certain point. This greatly
175-
* automatizes handling of these data.
174+
* deallocation, a stack like \ref Pool "pool" is used to automate this
175+
* handling.
176+
*
177+
* Stacks of temporary data only contain pointers to dynamically allocated
178+
* memory from the heap or the \ref suballocator. This stack functions like an
179+
* operand stack. A final result depends on fragments, temporary results and
180+
* similar, all pushed onto this stack. When the final operation is executed,
181+
* and its result is persisted in some variable, the dependency on its
182+
* temporary operands ceases. Consequently, they should be freed again. To
183+
* automate this operation, such a stack maintains a __start__ index. A
184+
* client saves this value and sets it to the current stack top, then starts
185+
* pushing dynamically allocated operands on the stack. After the result is
186+
* persisted, all entries beginning with the element at index __start__ are
187+
* deallocated again, and the stack top is reset to the __start__ value, while
188+
* the __start__ value is reset to the saved value, to accomodate nesting of
189+
* this procedure.
176190
*
177-
* A stack is not the same as a \ref block "block", though similar. Like a
178-
* block it is defined as an array of elements, but it comes with no hidden
179-
* header. Instead openly accessible stack pointer (actually indices) directly
180-
* support stack semantics.
191+
* This scheme needs a few conditions to be met:
192+
* - No operand is used in more than one evaluation context;
193+
* - Operations are executed strictly sequential, or in a nested manner. No two
194+
* operations interleave pushing operands.
195+
* push operands interleaved
181196
*/
182197

183198
/* Memory pools are used to reduce the number of malloc and alloc calls that
@@ -2452,9 +2467,11 @@ long g_pntrStartTempAllocStack = 0; /* Where to start freeing temporary alloca
24522467
* \var pntrString *pntrTempAllocStack[]
24532468
* \brief a \ref stack "stack" of \ref temp_pntrString.
24542469
*
2455-
* Holds pointers to temporarily allocated data of type \ref pntrString. Such
2470+
* Holds pointers to temporarily allocated data of type \ref temp_pntrString. Such
24562471
* a \ref stack "stack" contains strictly __local__ data of a function, not
24572472
* accessed from outer levels.
2473+
* \bug The element type should be temp_pntrString, because a NULL_PNTRSTRING
2474+
* must not be pushed on the stack.
24582475
*/
24592476
pntrString *pntrTempAllocStack[M_MAX_ALLOC_STACK];
24602477

@@ -2476,8 +2493,8 @@ pntrString *pntrTempAllocStack[M_MAX_ALLOC_STACK];
24762493
* \post
24772494
* - \p size > 0: memory for \p size entries is reserved in the \ref block
24782495
* "block's" header, but the data is still random.
2479-
* - Exits on out-of-memory
24802496
* - updates \ref db2
2497+
* - Exits on out-of-memory
24812498
*/
24822499
temp_pntrString *pntrTempAlloc(long size) {
24832500
/* pntrString memory allocation/deallocation */

src/mmdata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void memFreePoolPurge(flag untilOK);
410410
* accumulated bytes consumed by usage so far. This value includes the hidden
411411
* header of the block.
412412
* \pre Do not call within \ref bugfn "bug".\n
413-
* Submit only non-null pointers, even if not all information is requested.\n
413+
* Submit only non-null pointers, even if not all information is needed.\n
414414
* Pointers to irrelevant information may be the same.
415415
* \post Statistic data is copied to the locations the parameters point to.
416416
*/

0 commit comments

Comments
 (0)