### Example of Creating and Using a Heap (Windows)

The following program shows how you might create and use a heap.

    #include <windows.h>
    #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 = VirtualAlloc( NULL, *length,
       MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
       return (p);
    }

    static void release_fn(Heap_t usrheap, void *p, size_t size)
    {
       VirtualFree( p, 0, MEM_RELEASE );
       return;
    }

    int main(void)
    {
       void *initial_block;
       DWORD rc;
       Heap_t myheap;
       char *ptr;

       /* Call VirtualAlloc to get the initial block of memory */
       if((initial_block == VirtualAlloc( NULL, 65536, MEM_COMMIT|RESERVE,
             PAGE_READWRITE)) == NULL) {
          printf("VirtualAlloc error", GetLastError());
       }
       /* Create an expandable heap starting with the block declared earlier */
       if (NULL == (myheap = _ucreate(initial_block, 65536, _BLOCK_CLEAN,
                      _HEAP_REGULAR, get_fn, release_fn))) {
          puts("_ucreate failed.");
          exit(EXIT_FAILURE);
       }
       if (0 != _uopen(myheap)) {
          puts("_uopen failed.");
          exit(EXIT_FAILURE);
       }

       /* Force user heap to grow */
       ptr = _umalloc(myheap, 100000);

       _uclose(myheap);

       if (0 != _udestroy(myheap, _FORCE)) {
          puts("_udestroy failed.");
          exit(EXIT_FAILURE);
       }
       if (FALSE == VirtualFree( initial_block, 0, MEM_RELEASE)){
          printf("VirtualAlloc error", GetLastError());
       }
       return 0;
    }

Create a Fixed-Size Heap
Create an Expandable Heap
Use a User-Defined Heap

Example of Sharing a User Heap