DosSubAllocMem allocates a block of memory from a heap that was previously initialized by DosSubSetMem. This is used when an application needs an area of memory that is smaller than an entire heap.
The size of the memory block is rounded up to the next higher multiple of 8 bytes.
The following code fragment allocates a block of memory from a heap that was previously initialized by DosSubSetMem. Assume that the Offset variable has been set to the address of the initialized heap already.
#define INCL_DOSMEMMGR /* Memory Manager values */ #include <os2.h> #include <stdio.h> PVOID pOffset; /* The heap to suballocate from */ PPVOID ppBlockOffset; /* Pointer to the variable where the offset of */ /* the suballocated memory block is returned */ ULONG ulSize; /* Size in bytes of the memory block requested */ APIRET ulrc; /* Return code */ ulSize = 102; /* Ask for 102 bytes. This will be rounded */ /* to 104 bytes (a multiple of 8 bytes). */ ulrc = DosSubAllocMem(pOffset, &ppBlockOffset, ulSize); if (ulrc != 0) { printf("DosSubAllocMem error: return code = %ld", ulrc); return; }
In this example, the address of the allocated block (from the heap) is stored in the BlockOffset variable.
Remember to call DosSubFreeMem to free this block of memory when you are finished with it.