• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <limits.h>
17 #include <malloc.h>
18 #include <stddef.h>
19 
20 #if 0
21 #include "glue.h"
22 #include "meta.h"
23 
24 static void accumulate_meta(struct mallinfo2 *mi, struct meta *g) {
25   int sc = g->sizeclass;
26   if (sc >= 48) {
27     // Large mmap allocation
28     mi->hblks++;
29     mi->uordblks += g->maplen*4096;
30     mi->hblkhd += g->maplen*4096;
31   } else {
32     if (g->freeable && !g->maplen) {
33       // Small size slots are embedded in a larger slot, avoid double counting
34       // by subtracing the size of the larger slot from the total used memory.
35       struct meta* outer_g = get_meta((void*)g->mem);
36       int outer_sc  = outer_g->sizeclass;
37       int outer_sz = size_classes[outer_sc]*UNIT;
38       mi->uordblks -= outer_sz;
39     }
40     int sz = size_classes[sc]*UNIT;
41     int mask = g->avail_mask | g->freed_mask;
42     int nr_unused = __builtin_popcount(mask);
43     mi->ordblks += nr_unused;
44     mi->uordblks += sz*(g->last_idx+1-nr_unused);
45     mi->fordblks += sz*nr_unused;
46   }
47 }
48 
49 static void accumulate_meta_area(struct mallinfo2 *mi, struct meta_area *ma) {
50   for (int i=0; i<ma->nslots; i++) {
51     if (ma->slots[i].mem) {
52       accumulate_meta(mi, &ma->slots[i]);
53     }
54   }
55 }
56 #endif
57 
mallinfo2()58 struct mallinfo2 mallinfo2() {
59 
60   struct mallinfo2 mi = {0};
61 
62 #if 0
63   rdlock();
64   struct meta_area *ma = ctx.meta_area_head;
65   while (ma) {
66     accumulate_meta_area(&mi, ma);
67     ma = ma->next;
68   }
69   unlock();
70 #endif
71 
72   return mi;
73 }
74 
75 #define cap(x) ((x > INT_MAX) ? INT_MAX : x)
76 
mallinfo()77 struct mallinfo mallinfo() {
78   struct mallinfo mi = {0};
79   struct mallinfo2 mi2 = mallinfo2();
80 
81   mi.arena = cap(mi2.arena);
82   mi.ordblks = cap(mi2.ordblks);
83   mi.smblks = cap(mi2.smblks);
84   mi.hblks = cap(mi2.hblks);
85   mi.hblkhd = cap(mi2.hblkhd);
86   mi.usmblks = cap(mi2.usmblks);
87   mi.fsmblks = cap(mi2.fsmblks);
88   mi.uordblks = cap(mi2.uordblks);
89   mi.fordblks = cap(mi2.fordblks);
90   mi.keepcost = cap(mi2.keepcost);
91 
92   return mi;
93 }
94