mallocx()

Purpose

Gets memory from the heap

Syntax

ANY PTR mallocx(size_t size);

Parameters

size_t size

The amount of memory required

Description

This is a low-level function which gets a pointer to an unused area in the heap of the size specified. There is no heap initially; you must create one by calling freex() or heap_init(). From 4.620 mallocx() always rounds up the number of bytes requested to the nearest acceptable size (by calling mallocxable()). This is usually a multiple of four or eight bytes. In previous versions it did not return aligned memory, unless you allocate an even number of bytes, or set mallocx_round to TRUE. Be careful of asking for less than:

sizeof(size_t) + sizeof(char *) bytes

(See freex() for further details).

Error handling can be provided by setting the variable mallocx_handler to point to an error handler. This error handler is called when mallocx() is about to return 0.

DP4 library functions do not assume that any kind of memory allocation succeeds automatically and they can, therefore, all be used inside a handler. The cache_xxxx, malloc, realloc and buffered versions of tf_xxxx file functions call mallocx() internally, so bear this in mind when allocating the heap size if your program uses these functions.

Return values

Returns a pointer to an area of memory of size bytes. If the memory cannot be found, mallocx() returns 0.

See also

mallocxable(), mallocp(), freex()