the Compartmented Robust Posix C++ Unit Test system

crpcut::heap

These functions are used for querying the current state of the heap, or to impose limits on the available heap space.

[Note]Note
The heap management functionality requires linking with -lcrpcut instead of -lcrpcut_basic

crpcut::heap::allocated_bytes()

Return the number of bytes allocated on the heap. This is the sum of bytes requested in calls to malloc(), calloc(), realloc(), operator new() and operator new[](). Heap management overhead is not included.

[Tip]Tip
You can monitor for memory leaks by writing a fixture that compares the return value from crpcut::heap::allocated_bytes() in the constructor and the destructor.

crpcut::heap::allocated_objects()

Return the number of memory objects allocated on the heap by the functions malloc(), calloc(), realloc(), operator new() and operator new[](). The allocation of an array of elements is one object.

crpcut::heap::set_limit(bytes)

Set a limit on the available heap in absolute bytes, including all heap space that is already used. The return value is the limit prior to the call. Call set_limit(allocated_bytes() + delta_bytes), to set a limit slightly above current use.

[Note]Note
If you set the limit lower than the return value of crpcut::heap::allocated_bytes() the test will fail immediately.

[Caution]Caution
Calling set_limit() outside of a test, for example in the constructor of a global object, will almost certainly cause crpcut to malfunction.