blob: d60d6d9b5b99c9a85c9dbb1b184468510210af2f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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 ------------------------------ */
21struct mallinfo mallinfo(void)
22{
23 mstate av;
24 struct mallinfo mi;
25 unsigned int i;
26 mbinptr b;
27 mchunkptr p;
28 size_t avail;
29 size_t fastavail;
30 int nblocks;
31 int nfastblocks;
32
33 __MALLOC_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 __MALLOC_UNLOCK;
79 return mi;
80}
81libc_hidden_def(mallinfo)
82
83void malloc_stats(FILE *file)
84{
85 struct mallinfo mi;
86
87 if (file==NULL) {
88 file = stderr;
89 }
90
91 mi = mallinfo();
92 fprintf(file,
93 "total bytes allocated = %10u\n"
94 "total bytes in use bytes = %10u\n"
95 "total non-mmapped bytes allocated = %10d\n"
96 "number of mmapped regions = %10d\n"
97 "total allocated mmap space = %10d\n"
98 "total allocated sbrk space = %10d\n"
99#if 0
100 "number of free chunks = %10d\n"
101 "number of fastbin blocks = %10d\n"
102 "space in freed fastbin blocks = %10d\n"
103#endif
104 "maximum total allocated space = %10d\n"
105 "total free space = %10d\n"
106 "memory releasable via malloc_trim = %10d\n",
107
108 (unsigned int)(mi.arena + mi.hblkhd),
109 (unsigned int)(mi.uordblks + mi.hblkhd),
110 mi.arena,
111 mi.hblks,
112 mi.hblkhd,
113 mi.uordblks,
114#if 0
115 mi.ordblks,
116 mi.smblks,
117 mi.fsmblks,
118#endif
119 mi.usmblks,
120 mi.fordblks,
121 mi.keepcost
122 );
123}
124