2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_LOG_TAG "CLOCK-VALUE"
24 #include <babeltrace/lib-logging-internal.h>
26 #include <babeltrace/assert-pre-internal.h>
27 #include <babeltrace/compat/uuid-internal.h>
28 #include <babeltrace/ctf-ir/clock-class-internal.h>
29 #include <babeltrace/ctf-ir/clock-value-internal.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/types.h>
33 #include <babeltrace/compat/string-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/assert-internal.h>
39 void bt_clock_value_destroy(struct bt_clock_value
*clock_value
)
41 BT_LIB_LOGD("Destroying clock value: %!+k", clock_value
);
42 bt_put(clock_value
->clock_class
);
47 struct bt_clock_value
*bt_clock_value_new(struct bt_clock_class
*clock_class
)
49 struct bt_clock_value
*ret
= NULL
;
51 BT_ASSERT(clock_class
);
52 BT_LIB_LOGD("Creating clock value object: %![cc-]+K=",
54 ret
= g_new0(struct bt_clock_value
, 1);
56 BT_LOGE_STR("Failed to allocate one clock value.");
60 bt_object_init_unique(&ret
->base
);
61 ret
->clock_class
= bt_get(clock_class
);
62 bt_clock_class_freeze(clock_class
);
63 BT_LIB_LOGD("Created clock value object: %!+k", ret
);
70 struct bt_clock_value
*bt_clock_value_create(struct bt_clock_class
*clock_class
)
72 struct bt_clock_value
*clock_value
= NULL
;
74 BT_ASSERT(clock_class
);
75 clock_value
= bt_object_pool_create_object(&clock_class
->cv_pool
);
77 BT_LIB_LOGE("Cannot allocate one clock value from clock class's clock value pool: "
78 "%![cc-]+K", clock_class
);
82 if (likely(!clock_value
->clock_class
)) {
83 clock_value
->clock_class
= bt_get(clock_class
);
90 bt_clock_value_recycle(clock_value
);
99 void bt_clock_value_recycle(struct bt_clock_value
*clock_value
)
101 struct bt_clock_class
*clock_class
;
103 BT_ASSERT(clock_value
);
104 BT_LIB_LOGD("Recycling clock value: %!+k", clock_value
);
107 * Those are the important ordered steps:
109 * 1. Reset the clock value object, but do NOT put its clock
110 * class's reference. This clock class contains the pool to
111 * which we're about to recycle this clock value object, so
112 * we must guarantee its existence thanks to this existing
115 * 2. Move the clock class reference to our `clock_class`
116 * variable so that we can set the clock value's clock class
117 * member to NULL before recycling it. We CANNOT do this
118 * after we put the clock class reference because this
119 * bt_put() could destroy the clock class, also destroying
120 * its clock value pool, thus also destroying our clock value
121 * object (this would result in an invalid write access).
123 * 3. Recycle the clock value object.
125 * 4. Put our clock class reference.
127 bt_clock_value_reset(clock_value
);
128 clock_class
= clock_value
->clock_class
;
129 BT_ASSERT(clock_class
);
130 clock_value
->clock_class
= NULL
;
131 bt_object_pool_recycle_object(&clock_class
->cv_pool
, clock_value
);
135 uint64_t bt_clock_value_get_value(struct bt_clock_value
*clock_value
)
137 BT_ASSERT_PRE_NON_NULL(clock_value
, "Clock value");
138 BT_ASSERT_PRE(clock_value
->is_set
,
139 "Clock value is not set: %!+k", clock_value
);
140 return clock_value
->value_cycles
;
143 int bt_clock_value_get_ns_from_origin(struct bt_clock_value
*clock_value
,
144 int64_t *ret_value_ns
)
147 BT_ASSERT_PRE_NON_NULL(clock_value
, "Clock value");
148 BT_ASSERT_PRE_NON_NULL(ret_value_ns
, "Value (ns) (output)");
149 BT_ASSERT_PRE(clock_value
->is_set
,
150 "Clock value is not set: %!+k", clock_value
);
152 if (clock_value
->ns_from_origin_overflows
) {
153 BT_LIB_LOGD("Clock value, once converted to nanoseconds from origin, "
154 "overflows the signed 64-bit integer range: "
155 "%![cv-]+k", clock_value
);
160 *ret_value_ns
= clock_value
->ns_from_origin
;
166 struct bt_clock_class
*bt_clock_value_borrow_clock_class(
167 struct bt_clock_value
*clock_value
)
169 BT_ASSERT_PRE_NON_NULL(clock_value
, "Clock value");
170 return clock_value
->clock_class
;
This page took 0.033828 seconds and 4 git commands to generate.