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