_debug_uheapmin — Free unused memory in a user-created heap

Format

#include <umalloc.h>
int _debug_uheapmin(Heap_t heap, const char *file, size_t line);

Purpose

This is the debug version of _uheapmin. Like _uheapmin, it returns all unused memory blocks from the specified heap to the operating system.

To return the memory, _debug_uheapmin calls the heap-shrinking function you supply when you create the heap with _ucreate. If you do not supply a heap-shrinking function, _debug_uheapmin has no effect and returns 0.

In addition, _debug_uheapmin makes an implicit call to _uheap_check to validate the heap.

Return values

If successful, returns 0. A nonzero return value indicates failure. If the heap specified is not valid, generates an error message with the file name and line number in which the call to _debug_uheapmin was made.

Examples

This example creates a heap and allocates memory from it, then uses _debug_heapmin to release the memory.

/*  _debug_uheapmin.c   */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <umalloc.h>
int main(void)
{
   Heap_t  myheap;
   char    *ptr;
   /* Use default heap as user heap */
   myheap = _udefault(NULL);
   /* Allocate a large object */
   if (NULL == (ptr = (char*)_umalloc(myheap, 60000))) {
      puts("Cannot allocate memory from user heap.\n");
      exit(EXIT_FAILURE);
   }
   memset(ptr, 'x', 60000);
   free(ptr);
   /* _debug_uheapmin will attempt to return the freed object to the system */
   if (0 != _uheapmin(myheap)) {
      puts("_debug_uheapmin returns failed.\n");
      exit(EXIT_FAILURE);
   }
   return 0;
}