Branch data Line data Source code
1 : : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2008 litl, LLC
4 : :
5 : : #include <config.h>
6 : :
7 : : #include <inttypes.h>
8 : :
9 : : #include <glib.h>
10 : :
11 : : #include "gjs/mem-private.h" // IWYU pragma: associated
12 : : #include "gjs/mem.h"
13 : : #include "util/log.h"
14 : :
15 : : namespace Gjs {
16 : : namespace Memory {
17 : : namespace Counters {
18 : : #define GJS_DEFINE_COUNTER(name, ix) Counter name(#name);
19 : :
20 : : GJS_DEFINE_COUNTER(everything, -1)
21 : : GJS_FOR_EACH_COUNTER(GJS_DEFINE_COUNTER)
22 : : } // namespace Counters
23 : : } // namespace Memory
24 : : } // namespace Gjs
25 : :
26 : : #define GJS_LIST_COUNTER(name, ix) &Gjs::Memory::Counters::name,
27 : :
28 : : static Gjs::Memory::Counter* counters[] = {
29 : : GJS_FOR_EACH_COUNTER(GJS_LIST_COUNTER)};
30 : :
31 : : void
32 : 98 : gjs_memory_report(const char *where,
33 : : bool die_if_leaks)
34 : : {
35 : : int i;
36 : : int n_counters;
37 : : int64_t total_objects;
38 : :
39 : 98 : gjs_debug(GJS_DEBUG_MEMORY,
40 : : "Memory report: %s",
41 : : where);
42 : :
43 : 98 : n_counters = G_N_ELEMENTS(counters);
44 : :
45 : 98 : total_objects = 0;
46 [ + + ]: 1666 : for (i = 0; i < n_counters; ++i) {
47 : 1568 : total_objects += counters[i]->value;
48 : : }
49 : :
50 [ - + ]: 98 : if (total_objects != GJS_GET_COUNTER(everything)) {
51 : 0 : gjs_debug(GJS_DEBUG_MEMORY,
52 : : "Object counts don't add up!");
53 : : }
54 : :
55 : 98 : gjs_debug(GJS_DEBUG_MEMORY, " %" PRId64 " objects currently alive",
56 : : GJS_GET_COUNTER(everything));
57 : :
58 [ + + ]: 98 : if (GJS_GET_COUNTER(everything) != 0) {
59 [ + + ]: 833 : for (i = 0; i < n_counters; ++i) {
60 : 1568 : gjs_debug(GJS_DEBUG_MEMORY, " %24s = %" PRId64,
61 : 1568 : counters[i]->name, counters[i]->value.load());
62 : : }
63 : :
64 [ - + ]: 49 : if (die_if_leaks)
65 : 0 : g_error("%s: JavaScript objects were leaked.", where);
66 : : }
67 : 98 : }
|