Reuse existing g_string instance when setting a clock name
[babeltrace.git] / formats / ctf / ir / clock.c
CommitLineData
273b65be
JG
1/*
2 * clock.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Clock
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
adc315b8 29#include <babeltrace/ctf-ir/clock-internal.h>
273b65be
JG
30#include <babeltrace/ctf-writer/writer-internal.h>
31#include <babeltrace/compiler.h>
32#include <inttypes.h>
33
34static
35void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
36
4c426c17
JG
37BT_HIDDEN
38struct bt_ctf_clock *_bt_ctf_clock_create(void)
273b65be 39{
4c426c17
JG
40 struct bt_ctf_clock *clock = g_new0(
41 struct bt_ctf_clock, 1);
42
43 if (!clock) {
44 goto end;
45 }
46
47 clock->precision = 1;
48 clock->frequency = 1000000000;
4c426c17
JG
49 bt_ctf_ref_init(&clock->ref_count);
50end:
51 return clock;
52}
53
54BT_HIDDEN
55int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
56 const char *name)
57{
58 int ret = 0;
273b65be
JG
59
60 if (validate_identifier(name)) {
4c426c17
JG
61 ret = -1;
62 goto end;
273b65be
JG
63 }
64
4c426c17 65 if (clock->name) {
e1ae7645
JG
66 g_string_assign(clock->name, name);
67 } else {
68 clock->name = g_string_new(name);
69 if (!clock->name) {
70 ret = -1;
71 goto end;
72 }
273b65be
JG
73 }
74
4c426c17
JG
75end:
76 return ret;
77}
78
79struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
80{
81 int ret;
82 struct bt_ctf_clock *clock = NULL;
83
84 clock = _bt_ctf_clock_create();
85 if (!clock) {
86 goto error;
87 }
88
89 ret = bt_ctf_clock_set_name(clock, name);
90 if (ret) {
273b65be
JG
91 goto error_destroy;
92 }
93
be018f15
JG
94 ret = babeltrace_uuid_generate(clock->uuid);
95 if (ret) {
96 goto error_destroy;
97 }
98
99 clock->uuid_set = 1;
273b65be
JG
100 return clock;
101error_destroy:
102 bt_ctf_clock_destroy(&clock->ref_count);
103error:
87d76bb1
JG
104 return NULL;
105}
106
107const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
108{
109 const char *ret = NULL;
110
111 if (!clock) {
112 goto end;
113 }
114
115 if (clock->name) {
116 ret = clock->name->str;
117 }
118
119end:
120 return ret;
121}
122
123const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
124{
125 const char *ret = NULL;
126
127 if (!clock) {
128 goto end;
129 }
130
131 if (clock->description) {
132 ret = clock->description->str;
133 }
134end:
135 return ret;
273b65be
JG
136}
137
138int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
139{
140 int ret = 0;
141
142 if (!clock || !desc || clock->frozen) {
143 ret = -1;
144 goto end;
145 }
146
87d76bb1 147 clock->description = g_string_new(desc);
273b65be
JG
148 ret = clock->description ? 0 : -1;
149end:
150 return ret;
151}
152
87d76bb1
JG
153uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
154{
155 uint64_t ret = -1ULL;
156
157 if (!clock) {
158 goto end;
159 }
160
161 ret = clock->frequency;
162end:
163 return ret;
164}
165
273b65be
JG
166int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
167{
168 int ret = 0;
169
170 if (!clock || clock->frozen) {
171 ret = -1;
172 goto end;
173 }
174
175 clock->frequency = freq;
176end:
177 return ret;
178}
179
87d76bb1
JG
180uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
181{
182 uint64_t ret = -1ULL;
183
184 if (!clock) {
185 goto end;
186 }
187
188 ret = clock->precision;
189end:
190 return ret;
191}
192
273b65be
JG
193int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
194{
195 int ret = 0;
196
197 if (!clock || clock->frozen) {
198 ret = -1;
199 goto end;
200 }
201
202 clock->precision = precision;
203end:
204 return ret;
205}
206
87d76bb1
JG
207uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
208{
209 uint64_t ret = -1ULL;
210
211 if (!clock) {
212 goto end;
213 }
214
215 ret = clock->offset_s;
216end:
217 return ret;
218}
219
273b65be
JG
220int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
221{
222 int ret = 0;
223
224 if (!clock || clock->frozen) {
225 ret = -1;
226 goto end;
227 }
228
229 clock->offset_s = offset_s;
230end:
231 return ret;
232}
233
87d76bb1
JG
234uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
235{
236 uint64_t ret = -1ULL;
237
238 if (!clock) {
239 goto end;
240 }
241
242 ret = clock->offset;
243end:
244 return ret;
245}
246
273b65be
JG
247int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
248{
249 int ret = 0;
250
251 if (!clock || clock->frozen) {
252 ret = -1;
253 goto end;
254 }
255
256 clock->offset = offset;
257end:
258 return ret;
259}
260
87d76bb1
JG
261int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
262{
263 int ret = -1;
264
265 if (!clock) {
266 goto end;
267 }
268
269 ret = clock->absolute;
270end:
271 return ret;
272}
273
273b65be
JG
274int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
275{
276 int ret = 0;
277
278 if (!clock || clock->frozen) {
279 ret = -1;
280 goto end;
281 }
282
283 clock->absolute = !!is_absolute;
284end:
285 return ret;
286}
287
85b743f4
JG
288const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
289{
290 const unsigned char *ret;
291
be018f15 292 if (!clock || !clock->uuid_set) {
85b743f4
JG
293 ret = NULL;
294 goto end;
295 }
296
297 ret = clock->uuid;
298end:
299 return ret;
300}
301
302int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
303{
304 int ret = 0;
305
be018f15 306 if (!clock || !uuid || clock->frozen) {
85b743f4
JG
307 ret = -1;
308 goto end;
309 }
310
311 memcpy(clock->uuid, uuid, sizeof(uuid_t));
be018f15 312 clock->uuid_set = 1;
85b743f4
JG
313end:
314 return ret;
315}
316
87d76bb1
JG
317uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
318{
319 uint64_t ret = -1ULL;
320
321 if (!clock) {
322 goto end;
323 }
324
325 ret = clock->time;
326end:
327 return ret;
328}
329
273b65be
JG
330int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
331{
332 int ret = 0;
333
334 /* Timestamps are strictly monotonic */
335 if (!clock || time < clock->time) {
336 ret = -1;
337 goto end;
338 }
339
340 clock->time = time;
341end:
342 return ret;
343}
344
345void bt_ctf_clock_get(struct bt_ctf_clock *clock)
346{
347 if (!clock) {
348 return;
349 }
350
351 bt_ctf_ref_get(&clock->ref_count);
352}
353
354void bt_ctf_clock_put(struct bt_ctf_clock *clock)
355{
356 if (!clock) {
357 return;
358 }
359
360 bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
361}
362
363BT_HIDDEN
364void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
365{
366 if (!clock) {
367 return;
368 }
369
370 clock->frozen = 1;
371}
372
373BT_HIDDEN
374void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
375 struct metadata_context *context)
376{
377 unsigned char *uuid;
378
379 if (!clock || !context) {
380 return;
381 }
382
383 uuid = clock->uuid;
384 g_string_append(context->string, "clock {\n");
385 g_string_append_printf(context->string, "\tname = %s;\n",
386 clock->name->str);
387 g_string_append_printf(context->string,
388 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
389 uuid[0], uuid[1], uuid[2], uuid[3],
390 uuid[4], uuid[5], uuid[6], uuid[7],
391 uuid[8], uuid[9], uuid[10], uuid[11],
392 uuid[12], uuid[13], uuid[14], uuid[15]);
393 if (clock->description->len) {
394 g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
395 clock->description->str);
396 }
397
398 g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
399 clock->frequency);
400 g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
401 clock->precision);
402 g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
403 clock->offset_s);
404 g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
405 clock->offset);
406 g_string_append_printf(context->string, "\tabsolute = %s;\n",
407 clock->absolute ? "TRUE" : "FALSE");
408 g_string_append(context->string, "};\n\n");
409}
410
273b65be
JG
411static
412void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
413{
414 struct bt_ctf_clock *clock;
415
416 if (!ref) {
417 return;
418 }
419
420 clock = container_of(ref, struct bt_ctf_clock, ref_count);
421 if (clock->name) {
422 g_string_free(clock->name, TRUE);
423 }
424
425 if (clock->description) {
426 g_string_free(clock->description, TRUE);
427 }
428
429 g_free(clock);
430}
This page took 0.040294 seconds and 4 git commands to generate.