Dynamic Memory Management
The Library makes use of a dynamic memory manager which handles allocation and deallocation of
dynamic memory. The methods used for allocation and deallocation are wrapper functions around the
native malloc
, calloc
, realloc
, and free
methods. Hence, the functionality of teh module is very similar to the native C interface but it
allows for structured error recovery and application termination in case of failure. It covers
especially the following three situations:
- Handling of allocation and deallocation of dynamic memory
- Recovering from temporary lack of available memory
- Panic handling in case a new allocation fails
Memory Freer Functions
The dynamic memory freer functions are typically functions that are capable of freeing large chunks
of memory. In case a new allocation fails, the allocation method looks for any freer functions to
call. There can be multriple freer functions and after each call, the allocation method tries again
to allocate the desired amount of dynamic memory. The freer functions are called in reverse
order meaning that the last one registered gets called first. That way, it is easy
to add temporary free functions which then are guaranteed to be called first if a methods fails.
Add a Freer Function
You can add a freer function by using the following method. The Library itself registeres a set of
free functions during initialization. If the application does not register any freer functions then
the Library looks how it can free internal memory.
typedef void HTMemoryCallback(size_t size);
extern BOOL HTMemoryCall_add (HTMemoryCallback * cbf);
Delete a Freer Function
Freer functions can be deleted at any time in which case they are not called anymore.
extern BOOL HTMemoryCall_delete (HTMemoryCallback * cbf);
extern BOOL HTMemoryCall_deleteAll (void);
Panic Handling
If the freer functions are not capable of deallocation enough memory then the application must have
an organized way of closing down. This is done using the panic handler. In the libwww, each
allocation is tested and HT_OUTOFMEM is called if a NULL was returned. HT_OUTOFMEM is a macro which
calls HTMemory_outofmem. This function calls an exit function defined by the app in a call to
HTMemory_setExit. If the app has not defined this function, HTMemory_outofmem TTYPrints the error
message and calls exit(1).
typedef void HTMemoryExitCallback(char * name, char * file, unsigned long line);
extern void HTMemory_setExit(HTMemoryExitCallback * pExit);
extern HTMemoryExitCallback * HTMemory_exit(void);
Henrik Frystyk, libwww@w3.org,
@(#) $Id: Memory.html,v 1.4 1996/05/21 01:24:40 frystyk Exp $