Commit | Line | Data |
---|---|---|
e22b45d0 PP |
1 | #ifndef BABELTRACE_GRAPH_CLOCK_VALUE_SET_H |
2 | #define BABELTRACE_GRAPH_CLOCK_VALUE_SET_H | |
3 | ||
4 | /* | |
5 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include <stdint.h> | |
27 | #include <glib.h> | |
28 | #include <babeltrace/ctf-ir/clock-value-internal.h> | |
29 | #include <babeltrace/ctf-ir/clock-class-internal.h> | |
30 | #include <babeltrace/assert-internal.h> | |
31 | ||
32 | struct bt_clock_value_set { | |
33 | /* Unique objects owned by this */ | |
34 | GPtrArray *clock_values; | |
35 | ||
36 | /* Weak; points to one of the clock values above */ | |
37 | struct bt_clock_value *default_cv; | |
38 | }; | |
39 | ||
40 | static inline | |
41 | int bt_clock_value_set_initialize(struct bt_clock_value_set *cv_set) | |
42 | { | |
43 | int ret = 0; | |
44 | ||
45 | cv_set->clock_values = g_ptr_array_sized_new(1); | |
46 | if (!cv_set->clock_values) { | |
47 | #ifdef BT_LOGE_STR | |
48 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
49 | #endif | |
50 | ||
51 | ret = -1; | |
52 | goto end; | |
53 | } | |
54 | ||
55 | cv_set->default_cv = NULL; | |
56 | ||
57 | end: | |
58 | return ret; | |
59 | } | |
60 | ||
61 | static inline | |
62 | void bt_clock_value_set_reset(struct bt_clock_value_set *cv_set) | |
63 | { | |
64 | uint64_t i; | |
65 | ||
66 | BT_ASSERT(cv_set); | |
67 | BT_ASSERT(cv_set->clock_values); | |
68 | ||
69 | for (i = 0; i < cv_set->clock_values->len; i++) { | |
70 | struct bt_clock_value *cv = cv_set->clock_values->pdata[i]; | |
71 | ||
72 | BT_ASSERT(cv); | |
73 | bt_clock_value_reset(cv); | |
74 | bt_clock_value_set_is_frozen(cv, false); | |
75 | } | |
76 | ||
77 | cv_set->default_cv = NULL; | |
78 | } | |
79 | ||
80 | static inline | |
81 | void bt_clock_value_set_finalize(struct bt_clock_value_set *cv_set) | |
82 | { | |
83 | uint64_t i; | |
84 | ||
85 | BT_ASSERT(cv_set); | |
86 | ||
87 | if (cv_set->clock_values) { | |
88 | for (i = 0; i < cv_set->clock_values->len; i++) { | |
89 | struct bt_clock_value *cv = | |
90 | cv_set->clock_values->pdata[i]; | |
91 | ||
92 | BT_ASSERT(cv); | |
93 | bt_clock_value_recycle(cv); | |
94 | } | |
95 | ||
96 | g_ptr_array_free(cv_set->clock_values, TRUE); | |
97 | } | |
98 | ||
99 | cv_set->default_cv = NULL; | |
100 | } | |
101 | ||
102 | static inline | |
103 | int bt_clock_value_set_set_clock_value( | |
104 | struct bt_clock_value_set *cv_set, | |
105 | struct bt_clock_class *cc, uint64_t raw_value, | |
106 | bt_bool is_default) | |
107 | { | |
108 | int ret = 0; | |
109 | struct bt_clock_value *clock_value = NULL; | |
110 | uint64_t i; | |
111 | ||
112 | BT_ASSERT(cv_set); | |
113 | BT_ASSERT(cc); | |
114 | ||
115 | /* Check if we already have a value for this clock class */ | |
116 | for (i = 0; i < cv_set->clock_values->len; i++) { | |
117 | struct bt_clock_value *cv = cv_set->clock_values->pdata[i]; | |
118 | ||
119 | BT_ASSERT(cv); | |
120 | ||
121 | if (cv->clock_class == cc) { | |
122 | clock_value = cv; | |
123 | break; | |
124 | } | |
125 | } | |
126 | ||
127 | if (!clock_value) { | |
128 | clock_value = bt_clock_value_create(cc); | |
129 | if (!clock_value) { | |
130 | #ifdef BT_LIB_LOGE | |
131 | BT_LIB_LOGE("Cannot create a clock value from a clock class: " | |
132 | "%![cc-]+K", cc); | |
133 | #endif | |
134 | ||
135 | ret = -1; | |
136 | goto end; | |
137 | } | |
138 | ||
139 | g_ptr_array_add(cv_set->clock_values, clock_value); | |
140 | } | |
141 | ||
142 | #ifdef BT_ASSERT_PRE_HOT | |
143 | BT_ASSERT_PRE_HOT(clock_value, "Clock value", ": %!+k", clock_value); | |
144 | #endif | |
145 | ||
146 | ret = bt_clock_value_set_value_inline(clock_value, raw_value); | |
147 | if (ret) { | |
148 | goto end; | |
149 | } | |
150 | ||
151 | if (is_default) { | |
152 | cv_set->default_cv = clock_value; | |
153 | } | |
154 | ||
155 | end: | |
156 | return ret; | |
157 | } | |
158 | ||
159 | #endif /* BABELTRACE_GRAPH_CLOCK_VALUE_SET_H */ |