lib: make trace IR API const-correct
[babeltrace.git] / lib / trace-ir / clock-value.c
CommitLineData
44c440bc
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23#define BT_LOG_TAG "CLOCK-VALUE"
24#include <babeltrace/lib-logging-internal.h>
25
26#include <babeltrace/assert-pre-internal.h>
27#include <babeltrace/compat/uuid-internal.h>
56e18c4c
PP
28#include <babeltrace/trace-ir/clock-class-internal.h>
29#include <babeltrace/trace-ir/clock-value-internal.h>
65300d60 30#include <babeltrace/object.h>
44c440bc
PP
31#include <babeltrace/compiler-internal.h>
32#include <babeltrace/types.h>
33#include <babeltrace/compat/string-internal.h>
34#include <inttypes.h>
35#include <babeltrace/object-internal.h>
36#include <babeltrace/assert-internal.h>
37
38BT_HIDDEN
39void bt_clock_value_destroy(struct bt_clock_value *clock_value)
40{
41 BT_LIB_LOGD("Destroying clock value: %!+k", clock_value);
238b7404 42 BT_OBJECT_PUT_REF_AND_RESET(clock_value->clock_class);
44c440bc
PP
43 g_free(clock_value);
44}
45
46BT_HIDDEN
47struct bt_clock_value *bt_clock_value_new(struct bt_clock_class *clock_class)
48{
49 struct bt_clock_value *ret = NULL;
50
51 BT_ASSERT(clock_class);
52 BT_LIB_LOGD("Creating clock value object: %![cc-]+K=",
53 clock_class);
54 ret = g_new0(struct bt_clock_value, 1);
55 if (!ret) {
56 BT_LOGE_STR("Failed to allocate one clock value.");
57 goto end;
58 }
59
60 bt_object_init_unique(&ret->base);
398454ed
PP
61 ret->clock_class = clock_class;
62 bt_object_get_no_null_check(clock_class);
44c440bc
PP
63 bt_clock_class_freeze(clock_class);
64 BT_LIB_LOGD("Created clock value object: %!+k", ret);
65
66end:
67 return ret;
68}
69
70BT_HIDDEN
71struct bt_clock_value *bt_clock_value_create(struct bt_clock_class *clock_class)
72{
73 struct bt_clock_value *clock_value = NULL;
74
75 BT_ASSERT(clock_class);
76 clock_value = bt_object_pool_create_object(&clock_class->cv_pool);
77 if (!clock_value) {
78 BT_LIB_LOGE("Cannot allocate one clock value from clock class's clock value pool: "
79 "%![cc-]+K", clock_class);
80 goto error;
81 }
82
83 if (likely(!clock_value->clock_class)) {
398454ed
PP
84 clock_value->clock_class = clock_class;
85 bt_object_get_no_null_check(clock_class);
44c440bc
PP
86 }
87
88 goto end;
89
90error:
91 if (clock_value) {
92 bt_clock_value_recycle(clock_value);
93 clock_value = NULL;
94 }
95
96end:
97 return clock_value;
98}
99
100BT_HIDDEN
101void bt_clock_value_recycle(struct bt_clock_value *clock_value)
102{
103 struct bt_clock_class *clock_class;
104
105 BT_ASSERT(clock_value);
106 BT_LIB_LOGD("Recycling clock value: %!+k", clock_value);
107
108 /*
109 * Those are the important ordered steps:
110 *
111 * 1. Reset the clock value object, but do NOT put its clock
112 * class's reference. This clock class contains the pool to
113 * which we're about to recycle this clock value object, so
114 * we must guarantee its existence thanks to this existing
115 * reference.
116 *
117 * 2. Move the clock class reference to our `clock_class`
118 * variable so that we can set the clock value's clock class
119 * member to NULL before recycling it. We CANNOT do this
120 * after we put the clock class reference because this
65300d60 121 * bt_object_put_ref() could destroy the clock class, also destroying
44c440bc
PP
122 * its clock value pool, thus also destroying our clock value
123 * object (this would result in an invalid write access).
124 *
125 * 3. Recycle the clock value object.
126 *
127 * 4. Put our clock class reference.
128 */
129 bt_clock_value_reset(clock_value);
130 clock_class = clock_value->clock_class;
131 BT_ASSERT(clock_class);
132 clock_value->clock_class = NULL;
133 bt_object_pool_recycle_object(&clock_class->cv_pool, clock_value);
65300d60 134 bt_object_put_ref(clock_class);
44c440bc
PP
135}
136
40f4ba76 137uint64_t bt_clock_value_get_value(const struct bt_clock_value *clock_value)
44c440bc
PP
138{
139 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value");
140 BT_ASSERT_PRE(clock_value->is_set,
141 "Clock value is not set: %!+k", clock_value);
142 return clock_value->value_cycles;
143}
144
40f4ba76 145int bt_clock_value_get_ns_from_origin(const struct bt_clock_value *clock_value,
44c440bc
PP
146 int64_t *ret_value_ns)
147{
148 int ret = 0;
149 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value");
150 BT_ASSERT_PRE_NON_NULL(ret_value_ns, "Value (ns) (output)");
151 BT_ASSERT_PRE(clock_value->is_set,
152 "Clock value is not set: %!+k", clock_value);
153
154 if (clock_value->ns_from_origin_overflows) {
155 BT_LIB_LOGD("Clock value, once converted to nanoseconds from origin, "
156 "overflows the signed 64-bit integer range: "
157 "%![cv-]+k", clock_value);
158 ret = -1;
159 goto end;
160 }
161
162 *ret_value_ns = clock_value->ns_from_origin;
163
164end:
165 return ret;
166}
167
40f4ba76
PP
168const struct bt_clock_class *bt_clock_value_borrow_clock_class_const(
169 const struct bt_clock_value *clock_value)
44c440bc
PP
170{
171 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value");
172 return clock_value->clock_class;
173}
This page took 0.029589 seconds and 4 git commands to generate.