Next Previous Contents

2. Package for allocating memory with fixing some allocation errors

Developing modern compilers it is necessary to avoid arbitrary limits on the length or number of any data structure by allocating all data structures dynamically. Here the package `allocate' which implements allocation and freeing memory with automatic fixing allocation errors is suggested.

The package automatically calls action on situation `no memory'. The action never returns control back because after calling function which processes allocation error the function `abort' is always called. Therefore the function which processes allocation error should not return control back.

The interface part of the package is file `allocate.h'. Defining macro `NDEBUG' (e.g. by option `-D' in C compiler command line) before the package macros usage disables fixing some internal errors and errors of usage of the package. The implementation part is file `allocate.c'. The interface contains the following external definitions and macros:

Function `change_allocation_error_function'

        `void change_allocation_error_function
              (void (*error_function) (void))) (void)'
        
is used for changing up action on the situation `no memory'. The function also returns former function which was early action on the situation `no memory'.

Function `default_allocation_error_function'

        `void default_allocation_error_function (void)'
        
is default action of the package on the situation `no memory'. This action consists of output message `*** no memory ***' to standard error stream and calling function `exit' with code equals to 1.

Macro `MALLOC'

        `MALLOC(ptr, size)'
        
is analogous to ANSI C library function `malloc'. But the macro has two parameters. The first is pointer variable which is set up by address of allocated memory. The second is size of needed memory.

Macro `CALLOC'

        `CALLOC(ptr, nel, size)'
        
is analogous to ANSI C library function `calloc'. But the macro has three parameters. The first is pointer variable which is set up by address of allocated memory. The last two parameters have the same sense as in standard function `calloc'.

Macro `FREE'

        `FREE(ptr)'
        
is analogous to ANSI C library function `free' but can accept nil pointer value. In this case macro does nothing.

Macro `REALLOC'

        `REALLOC(new, old, size)'
        
is analogous to ANSI C library function `realloc' but has three parameters. The first parameter is variable which is set up by new address of reallocated memory. The second is old address of reallocated memory. And third is new size of reallocated memory.


Next Previous Contents