Syntax
#include <umalloc.h> Heap_t _uaddmem(Heap_t heap, void *block, size_t size, int clean);Description
If the memory block has been initialized to 0, specify _BLOCK_CLEAN for the clean parameter. If not, specify !_BLOCK_CLEAN. (This information makes calloc and _ucalloc more efficient).
Note: Memory returned by DosAllocMem is initialized to 0.
For fixed-size heaps, you must return all the blocks you added with _uaddmem to the system. (For expandable heaps, these blocks are returned by your release_fn when you call _udestroy.)
For more information about creating and using heaps, see "Managing Memory" in the VisualAge C++ Programming Guide.
Note: For every block of memory you add, a small number of bytes from it are used to store internal information. To reduce the total amount of overhead, it is better to add a few large blocks of memory than many small blocks.
The following example creates a heap myheap, and then uses _uaddmem to add memory to it.
#define INCL_DOSMEMMGR /* Memory Manager values */ #include <os2.h> #include <bsememf.h> /* Get flags for memory management */ #include <stdlib.h> #include <stdio.h> #include <umalloc.h> int main(void) { void *initial_block, *extra_chunk; APIRET rc; Heap_t myheap; char *p1, *p2; /* Call DosAllocMem to get the initial block of memory */ if (0 != (rc = DosAllocMem(&initial_block, 65536, PAG_WRITE | PAG_READ | PAG_COMMIT))) { printf("DosAllocMem for initial block failed: return code = %ld\n", rc); exit(EXIT_FAILURE); } /* Create a fixed size heap starting with the block declared earlier */ if (NULL == (myheap = _ucreate(initial_block, 65536, _BLOCK_CLEAN, _HEAP_REGULAR, NULL, NULL))) { puts("_ucreate failed."); exit(EXIT_FAILURE); } if (0 != _uopen(myheap)) { puts("_uopen failed."); exit(EXIT_FAILURE); } p1 = _umalloc(myheap, 100); /* Call DosAllocMem to get another block of memory */ if (0 != (rc = DosAllocMem(&extra_chunk, 10 * 65536, PAG_WRITE | PAG_READ | PAG_COMMIT))) { printf("DosAllocMem for extra chunk failed: return code = %ld\n", rc); exit(EXIT_FAILURE); } /* Add the second chunk of memory to user heap */ if (myheap != _uaddmem(myheap, extra_chunk, 10 * 65536, _BLOCK_CLEAN)) { puts("_uaddmem failed."); exit(EXIT_FAILURE); } p2 = _umalloc(myheap, 100000); free(p1); free(p2); if (0 != _uclose(myheap)) { puts("_uclose failed"); exit(EXIT_FAILURE); } if (0 != DosFreeMem(initial_block) || 0 != DosFreeMem(extra_chunk)) { puts("DosFreeMem error."); exit(EXIT_FAILURE); } return 0; }Related Information