Next: , Previous: heapdiff, Up: Tools


8.4 Memory allocation gauge

This file defines mgaugestart(), mgaugeend(), mgaugeon() and mgaugeoff() which produce and control a simple memory allocation gauge in a terminal window. The gauge is displayed in textual form using the standard I/O library rather than using a graphics library. Since it is updated in real-time, it makes no sense to send the output of the gauge to a file. Only one gauge can be in use at any one time.

The first argument to mgaugestart() is the filename of the file to write the gauge to. As mentioned before, this should be a terminal file that can be displayed in real-time, such as /dev/pts* on UNIX systems or CON:#? on AmigaOS. If it is a null pointer then the standard error file stream will be used.

The second argument to mgaugestart() specifies the character that will be used to represent allocated memory. If this is given as whitespace, `|' or `+' then `#' will be used instead. The third argument specifies the number of bytes that the gauge represents. If the total allocated memory exceeds this then `+' will be appended to the gauge. The final argument specifies the frequency of memory allocation events at which the gauge should be updated. If it is specified as zero then all events will cause the gauge to be updated.

      1  /*
      2   * Illustrates the use of mpatrol's memory allocation gauge
      3   * tool.
      4   */
     
     
      7  #include "mpatrol/mgauge.h"
     
     
     10  int main(void)
     11  {
     12      void *a[1024];
     13      size_t i;
     
     15      mgaugestart(NULL, '#', 8192, 0);
     16      mgaugeon();
     17      for (i = 0; i < 1024; i++)
     18          a[i] = malloc(6);
     19      for (i = 0; i < 1024; i++)
     20          free(a[i]);
     21      mgaugeoff();
     22      mgaugeend();
     23      return EXIT_SUCCESS;
     24  }

This produces an animated gauge similar to the one displayed below.

     0                                                           8192
     | ###################################                          |