4.14.10. __heapstats()

The __heapstats() functiondisplays statistics on the state of the storage allocation heap. It calls the __Heap_Stats() function, which you can re-implement if you choose to do your own storage management (see __Heap_Stats()). The ARM default implementation gives information on how many free blocks exist, and estimates their size ranges.

Example 4.20 shows an example of the output from __heapstats(). 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 the size of each block in bytes, expressed as a range. __heapstats() does not give information on the number of used blocks.

Example 4.20. heapstats output

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

The function outputs its results by calling the output function dprint, which 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. You must therefore ensure you have already sent some output to stderr in the above example.

If you are using the default single-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, as sequence such as:

  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 an ARM-specific library extension.


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

Copyright © 1999-2001 ARM Limited. All rights reserved.ARM DUI 0067D