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