128
K
A
DAK
AMX Memory Manager
10.3 Memory Allocation
The Memory Manager maintains a single memory pool. The memory pool consists of
any number of memory sections of varying sizes measured in bytes. Any memory
section size which is a multiple of 4 and greater than or equal to 64 bytes is allowed.
Usually the memory pool consists of a single large memory section.
Memory sections are given to the Memory Manager by your Memory Assignment
Procedure at startup. Once AMX is up and running, memory sections cannot be added or
removed from the memory pool.
When the Memory Manager assigns a memory section to its memory pool, it treats the
memory section as one large free memory block linked together with other free blocks on
a free list. This free list is internal to the Memory Manager, hidden from view.
Get Memory
Once the Memory Manager's memory pool has been initialized, a task can call the
Memory Manager procedure ajmget to get a block of memory of any size from the pool.
Only tasks are allowed to call the Memory Manager to acquire memory blocks.
If the Memory Manager is able to find a block of memory in the memory pool large
enough to meet the caller's requirements, the caller will be given a pointer to the memory
block and an indication of the actual size of that block. The block may be marginally
larger than the size requested. The memory block use count is set to one.
If the Memory Manager cannot locate a memory block in the memory pool large enough
to meet the caller's requirement, it returns an error indication to the caller and identifies
the largest block which is available in the pool at that instant. Note, however, that if the
calling task immediately requests a block of memory equal to this largest memory block
size, the request may still fail because, in a multitasking system, other higher priority
tasks may have grabbed some of that memory before the task can make another request to
the Memory Manager.
Free Memory
When use of a memory block is no longer required, the owner can call procedure ajmfre
to release the block. The caller specifies the block to be released by passi ng the M emor y
Manager the same pointer that it received when the Memory Manager originally allocated
the block.
The Memory Manager decrements the memory block's use count and, if the count goes to
zero, returns the block to its memory pool. If there are any free memory blocks adjacent
to the released block, they are merged with the block being released to form a larger
block.
Once a block is released, no task in the system can make reference to it. The block enters
the private domain of the Memory Manager.