Example (_uopen -- Open Heap for Use) (Windows)

This example creates a fixed-size heap, then uses _uopen to open it. The program then performs operations on the heap, and closes and destroys it.

    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <umalloc.h>

    int main(void)
    {
       void    *initial_block;
       Heap_t  myheap;
       char    *p;

       /* Call VirtualAlloc to get the initial block of memory */
       if (NULL == (initial_block =
           VirtualAlloc(NULL, 65536, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE)))
       {
          printf("VirtualAlloc error: %d.\n", GetLastError());
          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);
       }
       p = (char *)_umalloc(myheap, 100);
       free(p);
       if (0 != _uclose(myheap)) {
          puts("_uclose failed");
          exit(EXIT_FAILURE);
       }
       if (FALSE == VirtualFree(initial_block, 0, MEM_RELEASE)) {
          printf("VirtualFree error: %d.\n", GetLastError());
          exit(EXIT_FAILURE);
       }
       return 0;
    }