Add internal nameless clock creation API
[babeltrace.git] / formats / ctf / ir / clock.c
1 /*
2 * clock.c
3 *
4 * Babeltrace CTF IR - Clock
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/ctf-writer/clock.h>
30 #include <babeltrace/ctf-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/writer-internal.h>
32 #include <babeltrace/compiler.h>
33 #include <inttypes.h>
34
35 static
36 void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
37
38 BT_HIDDEN
39 struct bt_ctf_clock *_bt_ctf_clock_create(void)
40 {
41 struct bt_ctf_clock *clock = g_new0(
42 struct bt_ctf_clock, 1);
43
44 if (!clock) {
45 goto end;
46 }
47
48 clock->precision = 1;
49 clock->frequency = 1000000000;
50 uuid_generate(clock->uuid);
51 bt_ctf_ref_init(&clock->ref_count);
52 end:
53 return clock;
54 }
55
56 BT_HIDDEN
57 int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
58 const char *name)
59 {
60 int ret = 0;
61
62 if (validate_identifier(name)) {
63 ret = -1;
64 goto end;
65 }
66
67 if (clock->name) {
68 g_string_free(clock->name, TRUE);
69 }
70
71 clock->name = g_string_new(name);
72 if (!clock->name) {
73 ret = -1;
74 goto end;
75 }
76 end:
77 return ret;
78 }
79
80 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
81 {
82 int ret;
83 struct bt_ctf_clock *clock = NULL;
84
85 clock = _bt_ctf_clock_create();
86 if (!clock) {
87 goto error;
88 }
89
90 ret = bt_ctf_clock_set_name(clock, name);
91 if (ret) {
92 goto error_destroy;
93 }
94
95 return clock;
96 error_destroy:
97 bt_ctf_clock_destroy(&clock->ref_count);
98 error:
99 return NULL;
100 }
101
102 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
103 {
104 const char *ret = NULL;
105
106 if (!clock) {
107 goto end;
108 }
109
110 if (clock->name) {
111 ret = clock->name->str;
112 }
113
114 end:
115 return ret;
116 }
117
118 const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
119 {
120 const char *ret = NULL;
121
122 if (!clock) {
123 goto end;
124 }
125
126 if (clock->description) {
127 ret = clock->description->str;
128 }
129 end:
130 return ret;
131 }
132
133 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
134 {
135 int ret = 0;
136
137 if (!clock || !desc || clock->frozen) {
138 ret = -1;
139 goto end;
140 }
141
142 clock->description = g_string_new(desc);
143 ret = clock->description ? 0 : -1;
144 end:
145 return ret;
146 }
147
148 uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
149 {
150 uint64_t ret = -1ULL;
151
152 if (!clock) {
153 goto end;
154 }
155
156 ret = clock->frequency;
157 end:
158 return ret;
159 }
160
161 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
162 {
163 int ret = 0;
164
165 if (!clock || clock->frozen) {
166 ret = -1;
167 goto end;
168 }
169
170 clock->frequency = freq;
171 end:
172 return ret;
173 }
174
175 uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
176 {
177 uint64_t ret = -1ULL;
178
179 if (!clock) {
180 goto end;
181 }
182
183 ret = clock->precision;
184 end:
185 return ret;
186 }
187
188 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
189 {
190 int ret = 0;
191
192 if (!clock || clock->frozen) {
193 ret = -1;
194 goto end;
195 }
196
197 clock->precision = precision;
198 end:
199 return ret;
200 }
201
202 uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
203 {
204 uint64_t ret = -1ULL;
205
206 if (!clock) {
207 goto end;
208 }
209
210 ret = clock->offset_s;
211 end:
212 return ret;
213 }
214
215 int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
216 {
217 int ret = 0;
218
219 if (!clock || clock->frozen) {
220 ret = -1;
221 goto end;
222 }
223
224 clock->offset_s = offset_s;
225 end:
226 return ret;
227 }
228
229 uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
230 {
231 uint64_t ret = -1ULL;
232
233 if (!clock) {
234 goto end;
235 }
236
237 ret = clock->offset;
238 end:
239 return ret;
240 }
241
242 int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
243 {
244 int ret = 0;
245
246 if (!clock || clock->frozen) {
247 ret = -1;
248 goto end;
249 }
250
251 clock->offset = offset;
252 end:
253 return ret;
254 }
255
256 int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
257 {
258 int ret = -1;
259
260 if (!clock) {
261 goto end;
262 }
263
264 ret = clock->absolute;
265 end:
266 return ret;
267 }
268
269 int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
270 {
271 int ret = 0;
272
273 if (!clock || clock->frozen) {
274 ret = -1;
275 goto end;
276 }
277
278 clock->absolute = !!is_absolute;
279 end:
280 return ret;
281 }
282
283 const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
284 {
285 const unsigned char *ret;
286
287 if (!clock) {
288 ret = NULL;
289 goto end;
290 }
291
292 ret = clock->uuid;
293 end:
294 return ret;
295 }
296
297 int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
298 {
299 int ret = 0;
300
301 if (!clock || !uuid) {
302 ret = -1;
303 goto end;
304 }
305
306 memcpy(clock->uuid, uuid, sizeof(uuid_t));
307 end:
308 return ret;
309 }
310
311 uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
312 {
313 uint64_t ret = -1ULL;
314
315 if (!clock) {
316 goto end;
317 }
318
319 ret = clock->time;
320 end:
321 return ret;
322 }
323
324 int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
325 {
326 int ret = 0;
327
328 /* Timestamps are strictly monotonic */
329 if (!clock || time < clock->time) {
330 ret = -1;
331 goto end;
332 }
333
334 clock->time = time;
335 end:
336 return ret;
337 }
338
339 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
340 {
341 if (!clock) {
342 return;
343 }
344
345 bt_ctf_ref_get(&clock->ref_count);
346 }
347
348 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
349 {
350 if (!clock) {
351 return;
352 }
353
354 bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
355 }
356
357 BT_HIDDEN
358 void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
359 {
360 if (!clock) {
361 return;
362 }
363
364 clock->frozen = 1;
365 }
366
367 BT_HIDDEN
368 void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
369 struct metadata_context *context)
370 {
371 unsigned char *uuid;
372
373 if (!clock || !context) {
374 return;
375 }
376
377 uuid = clock->uuid;
378 g_string_append(context->string, "clock {\n");
379 g_string_append_printf(context->string, "\tname = %s;\n",
380 clock->name->str);
381 g_string_append_printf(context->string,
382 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
383 uuid[0], uuid[1], uuid[2], uuid[3],
384 uuid[4], uuid[5], uuid[6], uuid[7],
385 uuid[8], uuid[9], uuid[10], uuid[11],
386 uuid[12], uuid[13], uuid[14], uuid[15]);
387 if (clock->description->len) {
388 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
389 clock->description->str);
390 }
391
392 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
393 clock->frequency);
394 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
395 clock->precision);
396 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
397 clock->offset_s);
398 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
399 clock->offset);
400 g_string_append_printf(context->string, "\tabsolute = %s;\n",
401 clock->absolute ? "TRUE" : "FALSE");
402 g_string_append(context->string, "};\n\n");
403 }
404
405 static
406 void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
407 {
408 struct bt_ctf_clock *clock;
409
410 if (!ref) {
411 return;
412 }
413
414 clock = container_of(ref, struct bt_ctf_clock, ref_count);
415 if (clock->name) {
416 g_string_free(clock->name, TRUE);
417 }
418
419 if (clock->description) {
420 g_string_free(clock->description, TRUE);
421 }
422
423 g_free(clock);
424 }
This page took 0.038425 seconds and 4 git commands to generate.