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