The program below shows how you might create and use a heap.
Assuming that the program file is called t.c, compile it with the following command:
/usr/ibmcxx/bin/cc -qheapdebug t.c -lhu
#include <stdlib.h> #include <stdio.h> #include <umalloc.h> static void *get_fn(Heap_t usrheap, size_t *length, int *clean) { void *p; /* Round up to the next chunk size */ *length = ((*length) / 65536) * 65536 + 65536; *clean = _BLOCK_CLEAN; p = calloc(*length,1); return (p); } static void release_fn(Heap_t usrheap, void *p, size_t size) { free( p ); return; } int main(void) { void *initial_block; long rc; Heap_t myheap; char *ptr; int initial_sz; /* Get initial area to start heap */ initial_sz = 65536; initial_block = malloc(initial_sz); if(initial_block == NULL) return (1); /* create a user heap */ myheap = _ucreate(initial_block, initial_sz, _BLOCK_CLEAN, _HEAP_REGULAR, get_fn, release_fn); if (myheap == NULL) return(2); /* allocate from user heap and cause it to grow */ ptr = _umalloc(myheap, 100000); _ufree(ptr); /* destroy user heap */ if (_udestroy(myheap, _FORCE)) return(3); /* return initial block used to create heap */ free(initial_block); return 0; }
Memory Management Functions
Managing Memory with Multiple
Memory Heaps
Types of Memory
Debugging Memory Heaps
Creating and Using a Fixed Size Heap
Creating and Using an Expandable Heap
Debugging Problems with Heap Memory
Changing the Default Heap Used in a
Program
Example of Creating and Using a
Shared-Memory User Heap