4.12 __heapstats()

Defined in stdlib.h, the __heapstats() function displays statistics on the state of the storage allocation heap.


void __heapstats(int (*dprint)(void *param, char const *format,...), void *param);


The default implementation in the compiler gives information on how many free blocks exist, and estimates their size ranges.

The __heapstats() function generates output as follows:

32272 bytes in 2 free blocks (avge size 16136)
1 blocks 2^12+1 to 2^13
1 blocks 2^13+1 to 2^14

Line 1 of the output displays the total number of bytes, the number of free blocks, and the average size. The following lines give an estimate of the size of each block in bytes, expressed as a range. __heapstats() does not give information on the number of used blocks.

The function outputs its results by calling the output function dprint(), that must work like fprintf(). The first parameter passed to dprint() is the supplied pointer param. You can pass fprintf() itself, provided you cast it to the right function pointer type. This type is defined as a typedef for convenience. It is called __heapprt. For example:

__heapstats((__heapprt)fprintf, stderr);


If you call fprintf() on a stream that you have not already sent output to, the library calls malloc() internally to create a buffer for the stream. If this happens in the middle of a call to __heapstats(), the heap might be corrupted. Therefore, you must ensure you have already sent some output to stderr.

If you are using the default one-region memory model, heap memory is allocated only as it is required. This means that the amount of free heap changes as you allocate and deallocate memory. For example, the sequence:

int *ip;
__heapstats((__heapprt)fprintf,stderr);   // print initial free heap size
ip = malloc(200000);
__heapstats((__heapprt)fprintf,stderr);    // print heap size after freeing

gives output such as:

4076 bytes in 1 free blocks (avge size 4076)
1 blocks 2^10+1 to 2^11
2008180 bytes in 1 free blocks (avge size 2008180)
1 blocks 2^19+1 to 2^20

This function is not part of the C library standard, but the ARM® C library supports it as an extension.

Non-ConfidentialPDF file icon PDF versionARM DUI0475M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.