Suballocating Memory

DosAllocMem can be used to create a memory heap.

Before an application can allocate small portions of the heap, it must use the DosSubSetMem function to set up the memory for suballocation. The size of the heap is rounded up to the next higher multiple of 8 bytes.

Then, the application uses DosSubAllocMem to allocate sections of the heap and the DosSubFreeMem function to release the memory.

DosSubAllocMem returns a 32-bit pointer to a block of memory. The pointer can be used to access the memory without further modification.

The following code fragment sets up 8192 bytes for suballocation and then allocates two small blocks of memory:

    #define  INCL_DOSMEMMGR   /* Memory Manager values */
    #include <os2.h>

    APIRET  ulrc;
    PBYTE   pbBase,
            pb1,
            pb2;

    ulrc = DosAllocMem((PVOID *) &pbBase,
                       8192,
                       fALLOC);     /* Allocate 8K object    */

    ulrc = DosSubSetMem(pbBase,
                        DOSSUB_INIT,
                        8192);      /* Set up object         */
                                    /* for suballocation     */

    ulrc = DosSubAllocMem(pbBase,
                          (PVOID *) &pb1,
                          100);     /* Suballocate 100 bytes */

    ulrc = DosSubAllocMem(pbBase,
                          (PVOID *) &pb2,
                          500);     /* Suballocate 500 bytes */

    ulrc = DosSubFreeMem(pbBase,
                         pb1,
                         100);      /* Free 1st suballocation*/

    ulrc = DosSubAllocMem(pbBase,
                          (PVOID *) &pb1,
                          50);      /* Suballocate 50 bytes  */


[Back: Using Suballocation and Heaps]
[Next: Increasing the Size of a Heap]