Skip to content

Commit 7beeb0e

Browse files
committed
doc poolMalloc and others
1 parent 0fcfc29 commit 7beeb0e

File tree

3 files changed

+92
-33
lines changed

3 files changed

+92
-33
lines changed

src/mmdata.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,25 @@ vstring g_qsortKey; /* Used by qsortStringCmp; pointer only, do not deallocate *
204204
*/
205205
#define MEM_POOL_GROW 1000 /* Amount that a pool grows when it overflows. */
206206
/*??? Let user set this from menu. */
207+
/*!
208+
* \var long poolAbsoluteMax
209+
* The value is a memory amount in bytes.
210+
* \n
211+
* The \ref suballocator scheme must not hold more memory than is short term
212+
* useful. To the operating system all memory in \ref memFreePool appears as
213+
* allocated, although it is not really in use. To prevent the system from
214+
* taking unnecessary action such as saving RAM to disk, a limit to the amount
215+
* of free memory managed by the suballocator can be set up. This limit is
216+
* checked in frequent operations, and an automatic purge process is initiated
217+
* in \ref memFreePoolPurge should \ref poolTotalFree exceed this value.
218+
*/
207219
long poolAbsoluteMax = 1000000; /* Pools will be purged when this is reached */
208220
/*!
209221
* \var long poolTotalFree
210222
* contains the number of free space available in bytes, in both pools
211223
* \ref memFreePool and \ref memUsedPool, never counting the hidden headers at
212-
* the beginning of each block, see \ref block.
224+
* the beginning of each block, see \ref block. Exceeding \ref poolAbsoluteMax
225+
* may trigger an automatic purge process by \ref memFreePoolPurge.
213226
*/
214227
long poolTotalFree = 0; /* Total amount of free space allocated in pool */
215228
/*E*/long i1,j1_,k1; /* 'j1' is a built-in function */
@@ -556,6 +569,21 @@ void addToUsedPool(void *ptr)
556569
}
557570

558571
/* Free all arrays in the free pool. */
572+
/*!
573+
* \fn void memFreePoolPurge(flag untilOK)
574+
* \brief returns memory held in \ref memFreePool
575+
* Starting with the last entry in \ref memFreePool, memory held in that pool
576+
* is returned to the system until all, or at least a sufficient amount is
577+
* freed again (see \p untilOK).
578+
* \param[in] untilOK if 1 freeing \ref block "blocks" stops the moment
579+
* \ref poolTotalFree gets within the range of \ref poolAbsoluteMax again.
580+
* Note that it is not guaranteed that the limit \ref poolAbsoluteMax is
581+
* undercut because still too much free memory might be held in the
582+
* \ref memUsedPool.
583+
* \n
584+
* If 0, all \ref memFreePool entries are freed, and the pool itself is
585+
* shrunk back to \ref MEM_POOL_GROW size.
586+
*/
559587
void memFreePoolPurge(flag untilOK)
560588
{
561589
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("e0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
@@ -2677,9 +2705,9 @@ void pntrZapLen(pntrString *s, long length) {
26772705
* \ref pntrString.
26782706
*
26792707
* This function determines the length of the source \p t by scanning for a
2680-
* terminating null pointer element. The destination \p s must have enough
2681-
* space for receiving this amount of pointers, including the terminal null
2682-
* pointer. Then the source pointers are copied beginning with that at the
2708+
* terminal null pointer element. The destination \p s must have enough space
2709+
* for receiving this amount of pointers, including the terminal null pointer.
2710+
* Then the source pointers are copied beginning with that at the
26832711
* lowest address to the destination area \p t, including the terminal null
26842712
* pointer.
26852713
*
@@ -2695,8 +2723,8 @@ void pntrZapLen(pntrString *s, long length) {
26952723
* - \p t is terminated by the first null pointer element.
26962724
* - the target array \p s must have enough free space to hold the source array
26972725
* \p t including the terminal null pointer.
2698-
* - \p s and \p t can overlap if \p t points to a later element than \p s
2699-
* (move left semantics)
2726+
* - \p s and \p t can overlap if \p t points to a later or same element than
2727+
* \p s (move left semantics).
27002728
* \invariant
27012729
* If \p s is contained in a \ref block "block", its administrative header is
27022730
* NOT updated.

src/mmdata.h

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ extern flag g_globalDiscouragement; /* SET DISCOURAGEMENT */
307307

308308
/* Allocation and deallocation in memory pool */
309309
void *poolFixedMalloc(long size /* bytes */);
310+
/*!
311+
* \fn void *poolMalloc(long size)
312+
* \brief allocates and initializes a new \ref block
313+
*
314+
* allocates a \ref block, first removing and using the last element of the
315+
* \ref memFreePool. If this block exists, but has not sufficient size, it is
316+
* reallocated from the system. If the pool is empty, a new \ref block of
317+
* the given size is allocated from the system. In any case, the header of the
318+
* \ref block is properly initialized.
319+
* \param[in] size (in bytes) of the block, not including the block header.
320+
* \return a free and initialized block of memory with at least the requested
321+
* user space.
322+
* \bug uses outOfMemory that can stack up endlessly
323+
*/
310324
void *poolMalloc(long size /* bytes */);
311325
/*!
312326
* \fn poolFree(void *ptr)
@@ -317,7 +331,8 @@ void *poolMalloc(long size /* bytes */);
317331
* added.
318332
* \param[in] ptr pointer to a \ref block.
319333
* \pre
320-
* all memory pointed to by \p ptr is considered free.
334+
* all memory pointed to by \p ptr is considered free. This holds even if it
335+
* it is kept in \ref memUsedPool.
321336
* \post
322337
* \ref poolTotalFree is updated
323338
* \bug calls outOfMemory, that can stack up endlessly
@@ -327,8 +342,8 @@ void poolFree(void *ptr);
327342
* \fn addToUsedPool(void *ptr)
328343
* \brief announces a block with free capacity for further allocation
329344
*
330-
* This function temporarily freezes the usage of a block for an old client, and
331-
* allows temporary reallocation of the free capacity to a new client.
345+
* This function temporarily freezes the usage of a block for the current user,
346+
* and allows temporary reallocation of the free capacity to a new client.
332347
* \n
333348
* The program maintains pools of memory blocks with free capacity. In case of
334349
* demand such a \ref block can temporarily allocate this capacity for new
@@ -390,7 +405,7 @@ long getFreeSpace(long max);
390405

391406
/* Fatal memory allocation error */
392407
/*!
393-
* \fn outOfMemory
408+
* \fn outOfMemory(const char *msg)
394409
* \brief fatal memory allocation error.
395410
*
396411
* called when memory cannot be allocated, either because memory/address space
@@ -408,7 +423,7 @@ void outOfMemory(const char *msg);
408423

409424
/* Bug check error */
410425
/*!
411-
* \fn bug
426+
* \fn bug(int bugNum)
412427
*/
413428
void bug(int bugNum);
414429

@@ -672,6 +687,20 @@ temp_pntrString *pntrMakeTempAlloc(pntrString *s);
672687
/* String assignment - MUST be used to assign vstrings */
673688
/*!
674689
* \fn void pntrLet(pntrString **target, const pntrString *source)
690+
* \param[in,out] target (not null) the address of a pointer pointing to the
691+
* first byte of a \ref block receiving the copied \p source.
692+
* \param[in] source (not null) a pointer to the first \ref pntrString element
693+
* in a \ref block, to be copied from.
694+
* \pre
695+
* - source does not contain NULL pointer, but is terminated by one. This
696+
* NULL pointer is not part of the array, but must be present.
697+
* - the target \ref block does not contain used data.
698+
* \post
699+
* - the \ref block \p target points to is filled with a copy of
700+
* \ref pntrString elements \p source points to, padded with a terminal
701+
* NULL, or an outOfMemory error is raised.
702+
* - due to a possible reallocation the pointer \p target points to may
703+
* change.
675704
*/
676705
void pntrLet(pntrString **target, const pntrString *source);
677706

@@ -700,8 +729,10 @@ temp_pntrString *pntrPSpace(long n);
700729
* \brief Determine the length of a pntrString held in a \ref block "block"
701730
* dedicated to it.
702731
*
703-
* returns the number of **used** pointers in the array pointed to by \p s,
704-
* derived from administrative data in the surrounding block.
732+
* returns the number of **reserved** pointers in the array pointed to by \p s,
733+
* derived solely from administrative data in the surrounding \ref block. NULL
734+
* pointer in the array are included, a trailing one is not required to
735+
* determine the length.´
705736
*
706737
* \attention This is not the capacity of the array.
707738
* \param[in] s points to a element 0 of a \ref pntrString embedded in a block

src/mmvstr.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void let(vstring *target, const char *source);
335335
* The following parameters are pointers to NUL terminated strings as well,
336336
* except for the last parameter that must be NULL. It is allowed to
337337
* duplicate parameters.
338-
* \return the concatenated \ref temp_string terminated by a NUL character.
338+
* \return the concatenated \ref temp_vstring terminated by a NUL character.
339339
* \post the resulting string is pushed onto the \ref tempAllocStack.
340340
* \ref db is updated.
341341
* \bug a stack overflow of \ref tempAllocStack is not handled correctly.
@@ -358,50 +358,50 @@ int linput(FILE *stream, const char *ask, vstring *target);
358358
/* Emulation of BASIC string functions */
359359
/* Indices are 1-based */
360360
/*!
361-
* \fn temp_vstring seg(const char *sin, long p1, long p2)
361+
* \fn temp_vstring seg(const char *sin, long start, long stop)
362362
* Extracts a substring from a source and pushes it on \ref tempAllocStack.
363363
* Note: The bounding indices are 1-based and inclusive.
364364
*
365365
* \param[in] sin (not null) pointer to the NUL-terminated source text.
366-
* \param[in] p1 offset of the first byte of the substring, counted in bytes from
366+
* \param[in] start offset of the first byte of the substring, counted in bytes from
367367
* the first one of \p sin, a 1-based index. A value less than 1 is
368368
* internally corrected to 1, but it must not point beyond the terminating
369-
* NUL of \p sin, if \p p1 <= \p p2.
370-
* \param[in] p2 offset of the last byte of the substring, counted in bytes from
369+
* NUL of \p sin, if \p start <= \p stop.
370+
* \param[in] stop offset of the last byte of the substring, counted in bytes from
371371
* the first one of \p sin, a 1-based index. The natural bounds of this
372-
* value are \p p1 - 1 and the length of \p sin. Values outside of this
373-
* range are internally corrected to the closer of these limits. If \p p2 <
374-
* \p p1 the empty string is returned.
372+
* value are \p start - 1 and the length of \p sin. Values outside of this
373+
* range are internally corrected to the closer of these limits. If \p stop
374+
* < \p start the empty string is returned.
375375
* \attention the indices are 1-based: seg("hello", 2, 3) == "el"!
376376
* \return a pointer to new allocated \ref temp_vstring referencing the
377377
* requested substring, that is also pushed onto the top of
378378
* \ref tempAllocStack
379379
* \pre
380-
* \p p1 <= length(\p sin).
380+
* \p start <= length(\p sin).
381381
* \post
382382
* A pointer to the substring is pushed on \ref tempAllocStack, even if it
383383
* empty;
384384
* \warning not UTF-8 safe.
385385
* \bug a stack overflow of \ref tempAllocStack is not handled correctly;
386386
*/
387-
temp_vstring seg(const char *sin, long p1, long p2);
387+
temp_vstring seg(const char *sin, long start, long stop);
388388
/*!
389-
* \fn temp_vstring mid(const char *sin, long p, long l)
389+
* \fn temp_vstring mid(const char *sin, long start, long length)
390390
* Extracts a substring from a source and pushes it on \ref tempAllocStack
391391
*
392392
* \param[in] sin (not null) pointer to the NUL-terminated source text.
393-
* \param[in] p offset of the substring in bytes from the first byte of \p sin,
394-
* 1-based. A value less than 1 is internally corrected to 1, but it must
395-
* never point beyond the terminating NUL of \p sin.
396-
* \param[in] l length of substring in bytes. Negative values are corrected to 0.
397-
* If \p p + \p l exceeds the length of \p sin, then only the portion up
398-
* to the terminating NUL is taken.
399-
* \attention the index \p p is 1-based: mid("hello", 2, 1) == "e"!
393+
* \param[in] start offset of the substring in bytes from the first byte of
394+
* \p sin, 1-based. A value less than 1 is internally corrected to 1, but it
395+
* must never point beyond the terminating NUL of \p sin.
396+
* \param[in] length length of substring in bytes. Negative values are
397+
* corrected to 0. If \p start + \p length exceeds the length of \p sin,
398+
* then only the portion up to the terminating NUL is taken.
399+
* \attention the index \p start is 1-based: mid("hello", 2, 1) == "e"!
400400
* \return a pointer to new allocated \ref temp_vstring referencing the
401401
* requested substring, that is also pushed onto the top of
402402
* \ref tempAllocStack
403403
* \pre
404-
* \p p <= length(\p sin). This must hold even if the requested length is
404+
* \p start <= length(\p sin). This must hold even if the requested length is
405405
* 0, because its implementation in C requires the validity of the pointer,
406406
* even if it is not dereferenced.
407407
* \post
@@ -410,7 +410,7 @@ temp_vstring seg(const char *sin, long p1, long p2);
410410
* \warning not UTF-8 safe.
411411
* \bug a stack overflow of \ref tempAllocStack is not handled correctly;
412412
*/
413-
temp_vstring mid(const char *sin, long p, long l);
413+
temp_vstring mid(const char *sin, long start, long length);
414414
/*!
415415
* \fn temp_vstring left(const char *sin, long n)
416416
* \brief Extract leftmost n characters.

0 commit comments

Comments
 (0)