Fix: lib: add NULL check for destroy_listeners in destroy_component_class
[babeltrace.git] / src / lib / trace-ir / clock-class.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/CLOCK-CLASS"
9 #include "lib/logging.h"
10
11 #include "lib/assert-cond.h"
12 #include "common/uuid.h"
13 #include <babeltrace2/trace-ir/clock-class.h>
14 #include "clock-class.h"
15 #include "clock-snapshot.h"
16 #include "utils.h"
17 #include "compat/compiler.h"
18 #include <babeltrace2/types.h>
19 #include "compat/string.h"
20 #include <inttypes.h>
21 #include <stdbool.h>
22 #include "lib/object.h"
23 #include "common/assert.h"
24 #include "lib/func-status.h"
25 #include "lib/value.h"
26
27 #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
28 BT_ASSERT_PRE_DEV_HOT("clock-class", (_cc), "Clock class", \
29 ": %!+K", (_cc))
30
31 static
32 void destroy_clock_class(struct bt_object *obj)
33 {
34 struct bt_clock_class *clock_class = (void *) obj;
35
36 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
37 BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes);
38
39 if (clock_class->name.str) {
40 g_string_free(clock_class->name.str, TRUE);
41 clock_class->name.str = NULL;
42 clock_class->name.value = NULL;
43 }
44
45 if (clock_class->description.str) {
46 g_string_free(clock_class->description.str, TRUE);
47 clock_class->description.str = NULL;
48 clock_class->description.value = NULL;
49 }
50
51 bt_object_pool_finalize(&clock_class->cs_pool);
52 g_free(clock_class);
53 }
54
55 static
56 void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
57 struct bt_clock_class *clock_class)
58 {
59 bt_clock_snapshot_destroy(clock_snapshot);
60 }
61
62 static inline
63 void set_base_offset(struct bt_clock_class *clock_class)
64 {
65 clock_class->base_offset.overflows = bt_util_get_base_offset_ns(
66 clock_class->offset_seconds, clock_class->offset_cycles,
67 clock_class->frequency, &clock_class->base_offset.value_ns);
68 }
69
70 struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
71 {
72 int ret;
73 struct bt_clock_class *clock_class = NULL;
74
75 BT_ASSERT_PRE_NO_ERROR();
76 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
77 BT_LOGD_STR("Creating default clock class object");
78
79 clock_class = g_new0(struct bt_clock_class, 1);
80 if (!clock_class) {
81 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
82 goto error;
83 }
84
85 bt_object_init_shared(&clock_class->base, destroy_clock_class);
86
87 clock_class->user_attributes = bt_value_map_create();
88 if (!clock_class->user_attributes) {
89 BT_LIB_LOGE_APPEND_CAUSE(
90 "Failed to create a map value object.");
91 goto error;
92 }
93
94 clock_class->name.str = g_string_new(NULL);
95 if (!clock_class->name.str) {
96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
97 goto error;
98 }
99
100 clock_class->description.str = g_string_new(NULL);
101 if (!clock_class->description.str) {
102 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
103 goto error;
104 }
105
106 clock_class->frequency = UINT64_C(1000000000);
107 clock_class->origin_is_unix_epoch = BT_TRUE;
108 set_base_offset(clock_class);
109 ret = bt_object_pool_initialize(&clock_class->cs_pool,
110 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
111 (bt_object_pool_destroy_object_func)
112 free_clock_snapshot,
113 clock_class);
114 if (ret) {
115 BT_LIB_LOGE_APPEND_CAUSE(
116 "Failed to initialize clock snapshot pool: ret=%d",
117 ret);
118 goto error;
119 }
120
121 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
122 goto end;
123
124 error:
125 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
126
127 end:
128 return clock_class;
129 }
130
131 const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
132 {
133 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
134 return clock_class->name.value;
135 }
136
137 enum bt_clock_class_set_name_status bt_clock_class_set_name(
138 struct bt_clock_class *clock_class, const char *name)
139 {
140 BT_ASSERT_PRE_NO_ERROR();
141 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
142 BT_ASSERT_PRE_NAME_NON_NULL(name);
143 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
144 g_string_assign(clock_class->name.str, name);
145 clock_class->name.value = clock_class->name.str->str;
146 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
147 return BT_FUNC_STATUS_OK;
148 }
149
150 const char *bt_clock_class_get_description(
151 const struct bt_clock_class *clock_class)
152 {
153 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
154 return clock_class->description.value;
155 }
156
157 enum bt_clock_class_set_description_status bt_clock_class_set_description(
158 struct bt_clock_class *clock_class, const char *descr)
159 {
160 BT_ASSERT_PRE_NO_ERROR();
161 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
162 BT_ASSERT_PRE_DESCR_NON_NULL(descr);
163 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
164 g_string_assign(clock_class->description.str, descr);
165 clock_class->description.value = clock_class->description.str->str;
166 BT_LIB_LOGD("Set clock class's description: %!+K",
167 clock_class);
168 return BT_FUNC_STATUS_OK;
169 }
170
171 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
172 {
173 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
174 return clock_class->frequency;
175 }
176
177 void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
178 uint64_t frequency)
179 {
180 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
181 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
182 BT_ASSERT_PRE("valid-frequency",
183 frequency != UINT64_C(-1) && frequency != 0,
184 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
185 clock_class, frequency);
186 BT_ASSERT_PRE("offset-cycles-lt-frequency",
187 clock_class->offset_cycles < frequency,
188 "Offset (cycles) is greater than clock class's frequency: "
189 "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency);
190 clock_class->frequency = frequency;
191 set_base_offset(clock_class);
192 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
193 }
194
195 uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
196 {
197 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
198 return clock_class->precision;
199 }
200
201 void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
202 uint64_t precision)
203 {
204 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
205 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
206 BT_ASSERT_PRE("valid-precision", precision != UINT64_C(-1),
207 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
208 clock_class, precision);
209 clock_class->precision = precision;
210 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
211 }
212
213 void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
214 int64_t *seconds, uint64_t *cycles)
215 {
216 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
217 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds,
218 "Seconds (output)");
219 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles, "Cycles (output)");
220 *seconds = clock_class->offset_seconds;
221 *cycles = clock_class->offset_cycles;
222 }
223
224 void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
225 int64_t seconds, uint64_t cycles)
226 {
227 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
228 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
229 BT_ASSERT_PRE("offset-cycles-lt-frequency",
230 cycles < clock_class->frequency,
231 "Offset (cycles) is greater than clock class's frequency: "
232 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
233 clock_class->offset_seconds = seconds;
234 clock_class->offset_cycles = cycles;
235 set_base_offset(clock_class);
236 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
237 }
238
239 bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
240 {
241 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
242 return (bool) clock_class->origin_is_unix_epoch;
243 }
244
245 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
246 bt_bool origin_is_unix_epoch)
247 {
248 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
249 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
250 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
251 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
252 clock_class);
253 }
254
255 bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
256 {
257 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
258 return clock_class->uuid.value;
259 }
260
261 void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
262 bt_uuid uuid)
263 {
264 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
265 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
266 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
267 bt_uuid_copy(clock_class->uuid.uuid, uuid);
268 clock_class->uuid.value = clock_class->uuid.uuid;
269 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
270 }
271
272 BT_HIDDEN
273 void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
274 {
275 BT_ASSERT(clock_class);
276
277 if (clock_class->frozen) {
278 return;
279 }
280
281 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
282 clock_class->user_attributes);
283 bt_value_freeze(clock_class->user_attributes);
284 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
285 ((struct bt_clock_class *) clock_class)->frozen = 1;
286 }
287
288 enum bt_clock_class_cycles_to_ns_from_origin_status
289 bt_clock_class_cycles_to_ns_from_origin(
290 const struct bt_clock_class *clock_class,
291 uint64_t cycles, int64_t *ns)
292 {
293 int ret;
294
295 BT_ASSERT_PRE_DEV_NO_ERROR();
296 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
297 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns,
298 "Nanoseconds (output)");
299 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
300 if (ret) {
301 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
302 "from origin for given clock class: "
303 "value overflows the signed 64-bit integer range: "
304 "%![cc-]+K, cycles=%" PRIu64,
305 clock_class, cycles);
306 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
307 }
308
309 return ret;
310 }
311
312 const struct bt_value *bt_clock_class_borrow_user_attributes_const(
313 const struct bt_clock_class *clock_class)
314 {
315 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
316 return clock_class->user_attributes;
317 }
318
319 struct bt_value *bt_clock_class_borrow_user_attributes(
320 struct bt_clock_class *clock_class)
321 {
322 return (void *) bt_clock_class_borrow_user_attributes_const(
323 (void *) clock_class);
324 }
325
326 void bt_clock_class_set_user_attributes(
327 struct bt_clock_class *clock_class,
328 const struct bt_value *user_attributes)
329 {
330 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
331 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
332 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
333 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
334 bt_object_put_ref_no_null_check(clock_class->user_attributes);
335 clock_class->user_attributes = (void *) user_attributes;
336 bt_object_get_ref_no_null_check(clock_class->user_attributes);
337 }
338
339 void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
340 {
341 bt_object_get_ref(clock_class);
342 }
343
344 void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
345 {
346 bt_object_put_ref(clock_class);
347 }
This page took 0.038 seconds and 4 git commands to generate.