ILE C/C++ Programmer's Guide
To create modules that allocate storage dynamically from teraspace, specify
TERASPACE(*YES *TSIFC) on the CRTCPPMOD and CRTBNDCPP
commands. This remaps C run-time functions (such as malloc, calloc, and
free) to new teraspace equivalents. This does not affect storage
dynamically allocated using the new and delete
operators. If you want the new and delete operators to allocate storage
dynamically from teraspace, you can override these operators.
The following code redefines the global new and
delete operators to call functions (such as malloc or free) that
use teraspace.
- Note:
- To find the declaration of _set_mt_new_handler, the following
source must be compiled with DEFINE('__MULTI__') .
Figure 326. Example of Source Code that Redefines the Global new and delete Operators
#include <stdlib.h>
#include <new>
using namespace std;
void *operator new(size_t size) throw (bad_alloc)
{
// if size == 0, we must return a valid object! (ARM 5.3.3)
if (size <= 0)
size = 1;
void *ret = (malloc)(size);
while (ret == NULL)
{
// The malloc failed. Call the new_handler to try to free more memory
void (*temp_new_handler)();
// First check to see if a thread local new handler is defined.
temp_new_handler = _set_mt_new_handler(0);
_set_mt_new_handler(temp_new_handler);
// If there is no thread local handler try the global handler.
if (temp_new_handler == NULL)
{
// Note that the following code is not thread safe
// If the application is threaded and a new handler is set then
// all calls to set_new_handler() in the application must be
// blocked. Otherwise for the sake of speed, do not use a locking
// mechanism.
temp_new_handler = set_new_handler(0);
set_new_handler(temp_new_handler);
}
if (temp_new_handler != NULL)
{
temp_new_handler();
// Try one more time
ret = (malloc)(size);
}
else
throw bad_alloc();
}
// just return the result to the user
return ret;
}
void operator delete(void *ptr)
{
// delete of NULL is okay
if (ptr == NULL)
return;
(free)(ptr);
}
|
[ Top of Page | Previous Page | Next Page | Table of Contents ]
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.