aboutsummaryrefslogtreecommitdiff
path: root/src/android_je_mallinfo.c
blob: 1637edd9e637b0b11f470aaf35970d08eedeadd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* This is an implementation that uses the same arena access pattern found
 * in the arena_stats_merge function from src/arena.c.
 */
struct mallinfo je_mallinfo() {
  struct mallinfo mi;
  memset(&mi, 0, sizeof(mi));

  malloc_mutex_lock(&arenas_lock);
  for (unsigned i = 0; i < narenas_auto; i++) {
    if (arenas[i] != NULL) {
      malloc_mutex_lock(&arenas[i]->lock);
      mi.hblkhd += arenas[i]->stats.mapped;
      mi.uordblks += arenas[i]->stats.allocated_large;
      mi.uordblks += arenas[i]->stats.allocated_huge;
      malloc_mutex_unlock(&arenas[i]->lock);

      for (unsigned j = 0; j < NBINS; j++) {
        arena_bin_t* bin = &arenas[i]->bins[j];

        malloc_mutex_lock(&bin->lock);
        mi.uordblks += arena_bin_info[j].reg_size * bin->stats.curregs;
        malloc_mutex_unlock(&bin->lock);
      }
    }
  }
  malloc_mutex_unlock(&arenas_lock);
  mi.fordblks = mi.hblkhd - mi.uordblks;
  mi.usmblks = mi.hblkhd;
  return mi;
}

size_t __mallinfo_narenas() {
  return narenas_auto;
}

size_t __mallinfo_nbins() {
  return NBINS;
}

struct mallinfo __mallinfo_arena_info(size_t aidx) {
  struct mallinfo mi;
  memset(&mi, 0, sizeof(mi));

  malloc_mutex_lock(&arenas_lock);
  if (aidx < narenas_auto) {
    if (arenas[aidx] != NULL) {
      malloc_mutex_lock(&arenas[aidx]->lock);
      mi.hblkhd = arenas[aidx]->stats.mapped;
      mi.ordblks = arenas[aidx]->stats.allocated_large;
      mi.uordblks = arenas[aidx]->stats.allocated_huge;
      malloc_mutex_unlock(&arenas[aidx]->lock);

      for (unsigned j = 0; j < NBINS; j++) {
        arena_bin_t* bin = &arenas[aidx]->bins[j];

        malloc_mutex_lock(&bin->lock);
        mi.fsmblks += arena_bin_info[j].reg_size * bin->stats.curregs;
        malloc_mutex_unlock(&bin->lock);
      }
    }
  }
  malloc_mutex_unlock(&arenas_lock);
  return mi;
}

struct mallinfo __mallinfo_bin_info(size_t aidx, size_t bidx) {
  struct mallinfo mi;
  memset(&mi, 0, sizeof(mi));

  malloc_mutex_lock(&arenas_lock);
  if (aidx < narenas_auto && bidx < NBINS) {
    if (arenas[aidx] != NULL) {
      arena_bin_t* bin = &arenas[aidx]->bins[bidx];

      malloc_mutex_lock(&bin->lock);
      mi.ordblks = arena_bin_info[bidx].reg_size * bin->stats.curregs;
      mi.uordblks = bin->stats.nmalloc;
      mi.fordblks = bin->stats.ndalloc;
      malloc_mutex_unlock(&bin->lock);
    }
  }
  malloc_mutex_unlock(&arenas_lock);
  return mi;
}