OSDN Git Service

just drop all of the debug code
[uclinux-h8/uClibc.git] / libc / stdlib / malloc-standard / mallinfo.c
1 /*
2   This is a version (aka dlmalloc) of malloc/free/realloc written by
3   Doug Lea and released to the public domain.  Use, modify, and
4   redistribute this code without permission or acknowledgement in any
5   way you wish.  Send questions, comments, complaints, performance
6   data, etc to dl@cs.oswego.edu
7
8   VERSION 2.7.2 Sat Aug 17 09:07:30 2002  Doug Lea  (dl at gee)
9
10   Note: There may be an updated version of this malloc obtainable at
11            ftp://gee.cs.oswego.edu/pub/misc/malloc.c
12   Check before installing!
13
14   Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
15 */
16
17 #include "malloc.h"
18
19 libc_hidden_proto(fprintf)
20 libc_hidden_proto(stderr)
21
22 /* ------------------------------ mallinfo ------------------------------ */
23 libc_hidden_proto(mallinfo)
24 struct mallinfo mallinfo(void)
25 {
26     mstate av;
27     struct mallinfo mi;
28     unsigned int i;
29     mbinptr b;
30     mchunkptr p;
31     size_t avail;
32     size_t fastavail;
33     int nblocks;
34     int nfastblocks;
35
36     LOCK;
37     av = get_malloc_state();
38     /* Ensure initialization */
39     if (av->top == 0)  {
40         __malloc_consolidate(av);
41     }
42
43     check_malloc_state();
44
45     /* Account for top */
46     avail = chunksize(av->top);
47     nblocks = 1;  /* top always exists */
48
49     /* traverse fastbins */
50     nfastblocks = 0;
51     fastavail = 0;
52
53     for (i = 0; i < NFASTBINS; ++i) {
54         for (p = av->fastbins[i]; p != 0; p = p->fd) {
55             ++nfastblocks;
56             fastavail += chunksize(p);
57         }
58     }
59
60     avail += fastavail;
61
62     /* traverse regular bins */
63     for (i = 1; i < NBINS; ++i) {
64         b = bin_at(av, i);
65         for (p = last(b); p != b; p = p->bk) {
66             ++nblocks;
67             avail += chunksize(p);
68         }
69     }
70
71     mi.smblks = nfastblocks;
72     mi.ordblks = nblocks;
73     mi.fordblks = avail;
74     mi.uordblks = av->sbrked_mem - avail;
75     mi.arena = av->sbrked_mem;
76     mi.hblks = av->n_mmaps;
77     mi.hblkhd = av->mmapped_mem;
78     mi.fsmblks = fastavail;
79     mi.keepcost = chunksize(av->top);
80     mi.usmblks = av->max_total_mem;
81     UNLOCK;
82     return mi;
83 }
84 libc_hidden_def(mallinfo)
85
86 void malloc_stats(FILE *file)
87 {
88     struct mallinfo mi;
89
90     if (file==NULL) {
91         file = stderr;
92     }
93
94     mi = mallinfo();
95     fprintf(file, "total bytes allocated             = %10u\n", (unsigned int)(mi.arena + mi.hblkhd));
96     fprintf(file, "total bytes in use bytes          = %10u\n", (unsigned int)(mi.uordblks + mi.hblkhd));
97     fprintf(file, "total non-mmapped bytes allocated = %10d\n", mi.arena);
98     fprintf(file, "number of mmapped regions         = %10d\n", mi.hblks);
99     fprintf(file, "total allocated mmap space        = %10d\n", mi.hblkhd);
100     fprintf(file, "total allocated sbrk space        = %10d\n", mi.uordblks);
101 #if 0
102     fprintf(file, "number of free chunks             = %10d\n", mi.ordblks);
103     fprintf(file, "number of fastbin blocks          = %10d\n", mi.smblks);
104     fprintf(file, "space in freed fastbin blocks     = %10d\n", mi.fsmblks);
105 #endif
106     fprintf(file, "maximum total allocated space     = %10d\n", mi.usmblks);
107     fprintf(file, "total free space                  = %10d\n", mi.fordblks);
108     fprintf(file, "memory releasable via malloc_trim = %10d\n", mi.keepcost);
109 }
110