Programs allocate conventional memory (addresses 0000:0000 through A000:0000) by using Allocate Memory (Interrupt 21h Function 48h). This function searches for a block of memory at least as large as the requested block and returns the segment address of the new block. Since MS-DOS may allocate all available conventional memory when loading a program, Allocate Memory may return error value 0008h (ERROR_NOT_ENOUGH_MEMORY). If so, the BX register contains the size of the largest available block, in paragraphs.
If a program no longer needs the memory it has allocated, it can free the memory by using Free Allocated Memory (Interrupt 21h Function 49h). Once freed, the memory is available to be allocated again by the same program or by other programs. A program can increase or reduce the amount of memory in a block to a specified number of paragraphs by using Set Memory Block Size (Interrupt 21h Function 4Ah).
A program that runs another program (called a child program) often uses Set Memory Block Size to reduce its own size, making more memory available to the child program. In such a case, the parent program passes the segment address of its PSP to the function, along with the new size. However, the parent program must not free the memory containing its own code, data, and stack if subsequent memory allocations will destroy that memory. To avoid this situation, some programs copy their code and data to disk and free all but a small routine that reallocates the freed memory and reloads the code and data when they are needed again.
The current allocation strategy, set by Set Allocation Strategy (Interrupt 21h Function 5801h), determines how Allocate Memory searches for an available block of memory. The search can start from either the beginning or the end of conventional memory and ends upon reaching the first block that satisfies the request or, if none is available, the block that most closely matches the request. The allocation strategy also determines whether the function searches conventional memory or the upper memory area. A program can retrieve the current allocation strategy by using Get Allocation Strategy (Interrupt 21h Function 5800h).
Note:
If a program changes the allocation strategy, it should save the original allocation strategy and restore it before terminating.