/** * malloc-info * Some experiments to see what information we can learn from malloc. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For malloc and free #include // For malloc_stats #include // For printf and such #include "expt.h" // For EXPERIMENT and PRINT_PTR // +--------+--------------------------------------------------------- // | Macros | // +--------+ /** * MEMINFO () - Print information on memory usage. */ #define MEMINFO() \ BLOCK ( \ PRINT_INT (mem_allocated ()); \ ) // The next two lines may be added as desired. // EXPERIMENT (malloc_stats ()()); \ // EXPERIMENT (mem_info ()); \ // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Determine how much memory has been allocated by malloc. */ int mem_allocated () { struct mallinfo info = mallinfo (); return info.uordblks; } // mem_allocated /** * Print out the mallinfo struct. */ void mem_info () { struct mallinfo info = mallinfo (); fprintf (stderr, "arena: %d\n", info.arena); fprintf (stderr, "ordblks: %d\n", info.ordblks); fprintf (stderr, "smblks: %d\n", info.smblks); fprintf (stderr, "hblks: %d\n", info.hblks); fprintf (stderr, "hblkhd: %d\n", info.hblkhd); fprintf (stderr, "usmblks: %d\n", info.smblks); fprintf (stderr, "uordblks: %d\n", info.uordblks); fprintf (stderr, "fordblks: %d\n", info.fordblks); fprintf (stderr, "keepcost: %d\n", info.keepcost); } // mem_info // +------+----------------------------------------------------------- // | Main | // +------+ int main () { char *p1; char *p2; MEMINFO(); EXPERIMENT (p1 = (char *) malloc (100)); PRINT_PTR (p1); MEMINFO (); EXPERIMENT (p2 = (char *) malloc (32)); PRINT_PTR (p2); MEMINFO (); EXPERIMENT (free (p1)); MEMINFO (); EXPERIMENT (free (p2)); MEMINFO (); return 0; } // main