Debian Codegraph @

src/libsysprof/sysprof-cpu-info.c

raw
1 /* sysprof-cpu-info.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-cpu-info.h"
24
25 struct _SysprofCpuInfo
26 {
27 GObject parent_instance;
28 char *model_name;
29 guint id;
30 guint core_id;
31 };
32
33 enum {
34 PROP_0,
35 PROP_ID,
36 PROP_CORE_ID,
37 PROP_MODEL_NAME,
38 N_PROPS
39 };
40
41 G_DEFINE_FINAL_TYPE (SysprofCpuInfo, sysprof_cpu_info, G_TYPE_OBJECT)
42
43 static GParamSpec *properties [N_PROPS];
44
45 static void
46 sysprof_cpu_info_finalize (GObject *object)
47 {
48 SysprofCpuInfo *self = (SysprofCpuInfo *)object;
49
50 g_clear_pointer (&self->model_name, g_free);
51
52 G_OBJECT_CLASS (sysprof_cpu_info_parent_class)->finalize (object);
53 }
54
55 static void
56 sysprof_cpu_info_get_property (GObject *object,
57 guint prop_id,
58 GValue *value,
59 GParamSpec *pspec)
60 {
61 SysprofCpuInfo *self = SYSPROF_CPU_INFO (object);
62
63 switch (prop_id)
64 {
65 case PROP_ID:
66 g_value_set_uint (value, self->id);
67 break;
68
69 case PROP_CORE_ID:
70 g_value_set_uint (value, self->core_id);
71 break;
72
73 case PROP_MODEL_NAME:
74 g_value_set_string (value, self->model_name);
75 break;
76
77 default:
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79 }
80 }
81
82 static void
83 sysprof_cpu_info_set_property (GObject *object,
84 guint prop_id,
85 const GValue *value,
86 GParamSpec *pspec)
87 {
88 SysprofCpuInfo *self = SYSPROF_CPU_INFO (object);
89
90 switch (prop_id)
91 {
92 case PROP_ID:
93 self->id = g_value_get_uint (value);
94 break;
95
96 case PROP_CORE_ID:
97 self->core_id = g_value_get_uint (value);
98 break;
99
100 case PROP_MODEL_NAME:
101 self->model_name = g_value_dup_string (value);
102 break;
103
104 default:
105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 }
107 }
108
109 static void
110 sysprof_cpu_info_class_init (SysprofCpuInfoClass *klass)
111 {
112 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113
114 object_class->finalize = sysprof_cpu_info_finalize;
115 object_class->get_property = sysprof_cpu_info_get_property;
116 object_class->set_property = sysprof_cpu_info_set_property;
117
118 properties[PROP_ID] =
119 g_param_spec_uint ("id", NULL, NULL,
120 0, G_MAXUINT, 0,
121 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
122
123 properties[PROP_CORE_ID] =
124 g_param_spec_uint ("core-id", NULL, NULL,
125 0, G_MAXUINT, 0,
126 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
127
128 properties[PROP_MODEL_NAME] =
129 g_param_spec_string ("model-name", NULL, NULL,
130 NULL,
131 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
132
133 g_object_class_install_properties (object_class, N_PROPS, properties);
134 }
135
136 static void
137 sysprof_cpu_info_init (SysprofCpuInfo *self)
138 {
139 }
140
141 const char *
142 sysprof_cpu_info_get_model_name (SysprofCpuInfo *self)
143 {
144 g_return_val_if_fail (SYSPROF_IS_CPU_INFO (self), NULL);
145
146 return self->model_name;
147 }
148
149 guint
150 sysprof_cpu_info_get_id (SysprofCpuInfo *self)
151 {
152 g_return_val_if_fail (SYSPROF_IS_CPU_INFO (self), 0);
153
154 return self->id;
155 }
156
157 void
158 _sysprof_cpu_info_set_model_name (SysprofCpuInfo *self,
159 const char *model_name)
160 {
161 g_return_if_fail (SYSPROF_IS_CPU_INFO (self));
162
163 if (g_set_str (&self->model_name, model_name))
164 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MODEL_NAME]);
165 }
166
167 guint
168 sysprof_cpu_info_get_core_id (SysprofCpuInfo *self)
169 {
170 g_return_val_if_fail (SYSPROF_IS_CPU_INFO (self), 0);
171
172 return self->core_id;
173 }
174
175 void
176 _sysprof_cpu_info_set_core_id (SysprofCpuInfo *self,
177 guint core_id)
178 {
179 g_return_if_fail (SYSPROF_IS_CPU_INFO (self));
180
181 if (self->core_id != core_id)
182 {
183 self->core_id = core_id;
184 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CORE_ID]);
185 }
186 }