A short overview of the jmem library contained in jlib.lib - created by John Findlay
library version 1.01
Current list and bookmarks for library functions
The JMEM library was specifically created to overcome the speed problem when allocating and free-ing many blocks of memory. The crtdll.dll (malloc/free) from Microsoft is very slow when free-ing thousands of blocks. JMEM achieves this by allocating large blocks and returning addresses within that large block for the use of your app. All allocated memory is zero-ed before use.
syntax:
InitJMem initialises the structure for managing memory and sets the overall pool of memory available for any JMEM object.
'blocksize' and 'numblocks' are used to set this overall size of the pool of memory.
Typically one would estimate how much memory will be required and what size blocks will be used. For example, InitJMem( 500000, 200 ) will cause jmalloc to allocate blocks of 500000 bytes up to a maximum of 200, this gives a total possible memory pool of 100 meg. If in doubt as to how much of a pool size you will need, go for big!
If there is a need, more functions can be added so that a re-sizing of the pool could take place.
Return values:
Handle the the memory object.
syntax:
jmalloc allocates 'size' bytes of memory for use. The returned pointer will be aligned on a 4 byte boundary.
Return values:
Just like malloc, a pointer to void. If the allocation fails for any reason NULL will be returned.
syntax:
jfree frees all allocated memory for the memory object hmem. This is fast because jfree only has to free the number of blocks used, not the number of calls to jmalloc. Once jfree has been called hmem can not be used again to jmalloc, you must call InitJMem again to re-initialise.
Return values:
No return value.
syntax:
addblocks adds 'num' number of blocks for use with object 'hmem'. This allows extending the overall memory pool for the memory object at run time. If no more memory is available addblocks will retrun NULL so do not assign the retrun value to the main memory object handle until you have checked against NULL.
Example usage.
HMEM hmem =
InitJMem( 20000, 14 ); // create new memory object
HMEM tmp = addblocks( hmem, 25 ); // add blocks
// if successful assign tmp to hmem
if (tmp)
hmem = tmp;
jfree( hmem ); // free at end
Return values:
New handle, can be the same of different from original handle, NULL on error.
syntax:
getblock retruns the current block in use. This allows extending the overall memory pool for the memory object at run time by using function addblocks.
Return values:
The current block in use. If this number is close to the total number of blocks that were assigned when the memory object was created you should extend the number of blocks.
syntax:
getnumblocks returns the total number of blocks when the memory object was last assigned. This may be when it was first created or after a call to function 'addblocks'.
syntax:
jmem_libvers displays a MessageBox showing which library version.
Return values:
No return value.