mallocx_handler

Description

Provides an error handler for mallocx()

Syntax

handler DP4D mallocx_handler;

Parameters

handler ANY DP4P (* handler)()

Remarks

If this variable is set to point to an error-handling function, it is called when mallocx() is about to return 0 as a result of heap exhaustion. For example, a simple but effective error handler is:

ANY PTR out_of_memory()
{
map_pause(OUT_OF_MEMORY);
halt(); // stops program
return 0; // to satisfy compiler
}

This avoids you having to check the return code of mallocx(). A more sophisticated version is as follows:

ANY PTR find_some_more_memory(size)
{
if (find_some_more_memory(size)== OK) /* user-written
function which gets memory from somewhere*/
return ptr;
map_pause(OUT_OF_MEMORY);
halt();
return 0;
}

To prevent infinite recursion where mallocx() is called from the handler, set mallocx_handler to 0 temporarily before the handler function is called and restore it afterwards. If you set mallocx_handler again within the handler routine, it is your responsibility to control recursion.