1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "jemalloc/internal/jemalloc_internal.h"
18
19extern unsigned narenas_auto;
20extern malloc_mutex_t arenas_lock;
21extern arena_t **arenas;
22
23// This is an implementation that uses the same arena access pattern found
24// in the arena_stats_merge function from src/arena.c.
25struct mallinfo je_mallinfo() {
26  struct mallinfo mi;
27  memset(&mi, 0, sizeof(mi));
28
29  malloc_mutex_lock(&arenas_lock);
30  for (unsigned i = 0; i < narenas_auto; i++) {
31    if (arenas[i] != NULL) {
32      malloc_mutex_lock(&arenas[i]->lock);
33      mi.hblkhd += arenas[i]->stats.mapped;
34      mi.uordblks += arenas[i]->stats.allocated_large;
35      mi.uordblks += arenas[i]->stats.allocated_huge;
36      malloc_mutex_unlock(&arenas[i]->lock);
37
38      for (unsigned j = 0; j < NBINS; j++) {
39        arena_bin_t* bin = &arenas[i]->bins[j];
40
41        malloc_mutex_lock(&bin->lock);
42        mi.uordblks += bin->stats.allocated;
43        malloc_mutex_unlock(&bin->lock);
44      }
45    }
46  }
47  malloc_mutex_unlock(&arenas_lock);
48  mi.fordblks = mi.hblkhd - mi.uordblks;
49  mi.usmblks = mi.hblkhd;
50  return mi;
51}
52