@@ -167,17 +167,32 @@ vstring g_qsortKey; /* Used by qsortStringCmp; pointer only, do not deallocate *
167
167
* ubiquitous and frequent, that the processor, and all relevant program
168
168
* languages provide simple mechanisms for de/allocation of such __local__
169
169
* data. Metamath is no exception to this.
170
+ *
170
171
* While the C compiler silently cares about __local__ variables, it must not
171
172
* interfere with data managed by a \ref suballocation "Suballocator". Instead
172
173
* 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.
176
190
*
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
181
196
*/
182
197
183
198
/* 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
2452
2467
* \var pntrString *pntrTempAllocStack[]
2453
2468
* \brief a \ref stack "stack" of \ref temp_pntrString.
2454
2469
*
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
2456
2471
* a \ref stack "stack" contains strictly __local__ data of a function, not
2457
2472
* 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.
2458
2475
*/
2459
2476
pntrString * pntrTempAllocStack [M_MAX_ALLOC_STACK ];
2460
2477
@@ -2476,8 +2493,8 @@ pntrString *pntrTempAllocStack[M_MAX_ALLOC_STACK];
2476
2493
* \post
2477
2494
* - \p size > 0: memory for \p size entries is reserved in the \ref block
2478
2495
* "block's" header, but the data is still random.
2479
- * - Exits on out-of-memory
2480
2496
* - updates \ref db2
2497
+ * - Exits on out-of-memory
2481
2498
*/
2482
2499
temp_pntrString * pntrTempAlloc (long size ) {
2483
2500
/* pntrString memory allocation/deallocation */
0 commit comments