Memory Manager Library
This library provides routines for manipulating dynamic memory allocation. Dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a program during the runtime of that program.
Dynamically allocated memory exists until it is released. This is in contrast to static memory allocation, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.
The heap memory size can be configured in the Edit Project window. Also, user can override heap memory size in the code, by setting the HEAP_SIZE
constant.
Library Routines
MM_Init
Prototype |
procedure MM_Init(); |
---|---|
Description |
Initializes the memory manager. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
MM_Init(); // Initializes memory manager |
Notes |
None. |
GetMem
Prototype |
procedure GetMem(var P: ^byte; Size: word); |
---|---|
Description |
Allocates a block of memory from the memory Heap. |
Parameters |
|
Returns |
Returns a pointer to the fetched memory (of "Size" bytes) in P, if success; Otherwise 0 (no free blocks of memory are large enough). |
Requires |
Nothing. |
Example |
GetMem(ptr,20*sizeof(PBuffer)); // ptr will point to a memory block where PBuffer is allocated |
Notes |
None. |
Malloc
Prototype |
function Malloc(Size: word) : ^byte; |
---|---|
Description |
Allocates a block of memory from the memory Heap, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. |
Parameters |
|
Returns |
Returns a pointer to the memory block allocated by the function. If the function failed to allocate the requested block of memory, a zero is returned |
Requires |
Nothing. |
Example |
var p : ^dword; // pointer to byte var a : array[100] of dword; // array of dwords p := dword(Malloc(sizeof(a))); // p will point to a memory block where the array is allocated |
Notes |
None. |
FreeMem
Prototype |
procedure FreeMem(var P: ^byte; Size: word); |
---|---|
Description |
This function is used to free memory block allocated by GetMem or Malloc. |
Parameters | |
Returns |
Nothing. |
Requires |
Nothing. |
Example |
FreeMem(ptr,20*sizeof(PBuffer)); // ptr will point to a memory block where PBuffer is allocated |
Notes |
None. |
MM_LargestFreeMemBlock
Prototype |
function MM_LargestFreeMemBlock() : word; |
---|---|
Description |
This function is used to determine largest available free memory of contiguous memory on the heap. |
Parameters |
None. |
Returns |
Returns, after defragmentation of the freelist the size (in bytes) of the largest free block of contiguous memory on the heap. |
Requires |
Nothing. |
Example |
var block : word; begin block := MM_LargestFreeMemBlock(); end; |
Notes |
None. |
MM_TotalFreeMemSize
Prototype |
function MM_TotalFreeMemSize() : word; |
---|---|
Description |
This function is used to determine total free memory size on the heap. |
Parameters |
None. |
Returns |
Returns the size (in bytes) of the total free memory on the heap. |
Requires |
Nothing. |
Example |
var total : word; begin total := MM_TotalFreeMemSize(); end; |
Notes |
None. |
What do you think about this topic ? Send us feedback!