DosAllocMem() Function

The DosAllocSeg() function implemented under previous versions of OS/2 is replaced in Version 2.0 by the DosAllocMem() function, which allows allocation of memory objects greater than 64KB in size. To take an example, Figure "Allocating Memory in Previous Versions of OS/2" shows the code necessary under OS/2 Version 1.3 to allocate a 72KB area of memory for use by an application. The application must then use pSegment1 to reference the lower 64KB and pSegment2 to reference the upper 8KB of the memory object. This requires conditional testing for each memory reference, and thereby introduces additional complication to the application code. Use of the DosAllocHuge() function simplifies this slightly, but arithmetic is still required in order to correctly calculate offsets within the higher area of memory.

Under OS/2 Version 2.0, a single DosAllocMem() function call is required in order to perform the same task, as shown in Figure "Allocating Memory in OS/2 Version 2.0". Subsequent references to this memory object may simply use a 32-bit offset within the allocated address range.

Note that since OS/2 Version 2.0 uses paged memory internally, memory allocated using DosAllocMem() is always allocated in multiples of 4KB. Thus, a request for 10 bytes will actually result in a full 4KB page being committed in real storage. Since this will lead to high fragmentation and consequent waste of memory, the allocation of many small memory objects using DosAllocMem() directly is not recommended. Application developers should initially use DosAllocMem() to allocate the maximum storage likely to be required, and then use the DosSubAlloc() function to allocate individual memory objects. This technique allows the storage of multiple small memory objects within the same 4KB page, thereby reducing fragmentation and making more efficient use of storage.

Note that the DosAllocHuge() function provided under previous versions of OS/2 has no counterpart under Version 2.0. This function is not required since DosAllocMem() allows the theoretical allocation of memory objects of a size up to that of the application's entire process address space.

Memory objects allocated using DosAllocMem() may be freed using the DosFreeMem() function.


[Back: The Flat Memory Model]
[Next: Allocating versus Committing Memory]