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 : : // SPDX-FileCopyrightText: 2021 Canonical, Ltd
5 : :
6 : : #pragma once
7 : :
8 : : #include <config.h>
9 : :
10 : : #include <stddef.h> // for size_t
11 : :
12 : : #include <atomic>
13 : :
14 : : // clang-format off
15 : : #define GJS_FOR_EACH_COUNTER(macro) \
16 : : macro(boxed_instance, 0) \
17 : : macro(boxed_prototype, 1) \
18 : : macro(closure, 2) \
19 : : macro(function, 3) \
20 : : macro(fundamental_instance, 4) \
21 : : macro(fundamental_prototype, 5) \
22 : : macro(gerror_instance, 6) \
23 : : macro(gerror_prototype, 7) \
24 : : macro(interface, 8) \
25 : : macro(module, 9) \
26 : : macro(ns, 10) \
27 : : macro(object_instance, 11) \
28 : : macro(object_prototype, 12) \
29 : : macro(param, 13) \
30 : : macro(union_instance, 14) \
31 : : macro(union_prototype, 15)
32 : : // clang-format on
33 : :
34 : : namespace Gjs {
35 : : namespace Memory {
36 : :
37 : : struct Counter {
38 : 2193 : explicit Counter(const char* n) : name(n) {}
39 : : std::atomic_int64_t value = ATOMIC_VAR_INIT(0);
40 : : const char* name;
41 : : };
42 : :
43 : : namespace Counters {
44 : : #define GJS_DECLARE_COUNTER(name, ix) extern Counter name;
45 : : GJS_DECLARE_COUNTER(everything, -1)
46 : : GJS_FOR_EACH_COUNTER(GJS_DECLARE_COUNTER)
47 : : #undef GJS_DECLARE_COUNTER
48 : :
49 : : template <Counter* counter>
50 : 56444 : constexpr void inc() {
51 : 56444 : everything.value++;
52 : 56444 : counter->value++;
53 : 56444 : }
54 : :
55 : : template <Counter* counter>
56 : 56225 : constexpr void dec() {
57 : 56225 : counter->value--;
58 : 56225 : everything.value--;
59 : 56225 : }
60 : :
61 : : } // namespace Counters
62 : : } // namespace Memory
63 : : } // namespace Gjs
64 : :
65 : : #define COUNT(name, ix) +1
66 : : static constexpr size_t GJS_N_COUNTERS = 0 GJS_FOR_EACH_COUNTER(COUNT);
67 : : #undef COUNT
68 : :
69 : : static constexpr const char GJS_COUNTER_DESCRIPTIONS[GJS_N_COUNTERS][52] = {
70 : : // max length of description string ---------------v
71 : : "Number of boxed type wrapper objects",
72 : : "Number of boxed type prototype objects",
73 : : "Number of signal handlers",
74 : : "Number of introspected functions",
75 : : "Number of fundamental type wrapper objects",
76 : : "Number of fundamental type prototype objects",
77 : : "Number of GError wrapper objects",
78 : : "Number of GError prototype objects",
79 : : "Number of GObject interface objects",
80 : : "Number of modules",
81 : : "Number of GI namespace objects",
82 : : "Number of GObject wrapper objects",
83 : : "Number of GObject prototype objects",
84 : : "Number of GParamSpec wrapper objects",
85 : : "Number of C union wrapper objects",
86 : : "Number of C union prototype objects",
87 : : };
88 : :
89 : : #define GJS_INC_COUNTER(name) \
90 : : (Gjs::Memory::Counters::inc<&Gjs::Memory::Counters::name>());
91 : :
92 : : #define GJS_DEC_COUNTER(name) \
93 : : (Gjs::Memory::Counters::dec<&Gjs::Memory::Counters::name>());
94 : :
95 : : #define GJS_GET_COUNTER(name) (Gjs::Memory::Counters::name.value.load())
|