OSDN Git Service

Some requested additional malloc entry points
[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
20 /* ------------------------------ mallinfo ------------------------------ */
21 struct mallinfo mallinfo(void)
22 {
23     mstate av;
24     struct mallinfo mi;
25     int i;
26     mbinptr b;
27     mchunkptr p;
28     size_t avail;
29     size_t fastavail;
30     int nblocks;
31     int nfastblocks;
32
33     LOCK;
34     av = get_malloc_state();
35     /* Ensure initialization */
36     if (av->top == 0)  {
37         __malloc_consolidate(av);
38     }
39
40     check_malloc_state();
41
42     /* Account for top */
43     avail = chunksize(av->top);
44     nblocks = 1;  /* top always exists */
45
46     /* traverse fastbins */
47     nfastblocks = 0;
48     fastavail = 0;
49
50     for (i = 0; i < NFASTBINS; ++i) {
51         for (p = av->fastbins[i]; p != 0; p = p->fd) {
52             ++nfastblocks;
53             fastavail += chunksize(p);
54         }
55     }
56
57     avail += fastavail;
58
59     /* traverse regular bins */
60     for (i = 1; i < NBINS; ++i) {
61         b = bin_at(av, i);
62         for (p = last(b); p != b; p = p->bk) {
63             ++nblocks;
64             avail += chunksize(p);
65         }
66     }
67
68     mi.smblks = nfastblocks;
69     mi.ordblks = nblocks;
70     mi.fordblks = avail;
71     mi.uordblks = av->sbrked_mem - avail;
72     mi.arena = av->sbrked_mem;
73     mi.hblks = av->n_mmaps;
74     mi.hblkhd = av->mmapped_mem;
75     mi.fsmblks = fastavail;
76     mi.keepcost = chunksize(av->top);
77     mi.usmblks = av->max_total_mem;
78     UNLOCK;
79     return mi;
80 }
81
82 void malloc_stats(FILE *file)
83 {
84     struct mallinfo mi;
85
86     if (file==NULL) {
87         file = stderr;
88     }
89
90     mi = mallinfo();
91     fprintf(file, "total bytes allocated             = %10lu\n", (unsigned int)(mi.arena + mi.hblkhd));
92     fprintf(file, "total bytes in use bytes          = %10lu\n", (unsigned int)(mi.uordblks + mi.hblkhd));
93     fprintf(file, "total non-mmapped bytes allocated = %10lu\n", (unsigned int)(mi.arena));
94     fprintf(file, "number of mmapped regions         = %10lu\n", (unsigned int)(mi.hblks));
95     fprintf(file, "total allocated mmap space        = %10lu\n", (unsigned int)(mi.hblkhd));
96     fprintf(file, "total allocated sbrk space        = %10lu\n", (unsigned int)(mi.uordblks));
97 #if 0
98     fprintf(file, "number of free chunks             = %10lu\n", (unsigned int)(mi.ordblks));
99     fprintf(file, "number of fastbin blocks          = %10lu\n", (unsigned int)(mi.smblks));
100     fprintf(file, "space in freed fastbin blocks     = %10lu\n", (unsigned int)(mi.fsmblks));
101 #endif
102     fprintf(file, "maximum total allocated space     = %10lu\n", (unsigned int)(mi.usmblks));
103     fprintf(file, "total free space                  = %10lu\n", (unsigned int)(mi.fordblks));
104     fprintf(file, "memory releasable via malloc_trim = %10lu\n", (unsigned int)(mi.keepcost));
105 }
106