lib: fully detach CTF IR and CTF writer implementations
[babeltrace.git] / include / babeltrace / ctf-writer / fields-internal.h
CommitLineData
3dca2276
PP
1#ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
2#define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
3
4/*
5 * Babeltrace - CTF writer: Event Fields
6 *
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 *
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
31 */
32
33#include <stdint.h>
34#include <stddef.h>
35
16ca5ff0
PP
36#include <babeltrace/assert-pre-internal.h>
37#include <babeltrace/babeltrace-internal.h>
3dca2276 38#include <babeltrace/babeltrace.h>
16ca5ff0
PP
39#include <babeltrace/common-internal.h>
40#include <babeltrace/ctf-writer/field-types-internal.h>
3dca2276
PP
41#include <babeltrace/ctf-writer/fields.h>
42#include <babeltrace/ctf-writer/serialize-internal.h>
16ca5ff0
PP
43#include <babeltrace/ctf-writer/utils-internal.h>
44#include <babeltrace/object-internal.h>
45#include <babeltrace/types.h>
46#include <glib.h>
47#include <inttypes.h>
48#include <stdbool.h>
49#include <stdint.h>
50#include <string.h>
51
52#define BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
53 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
54 _name " has the wrong type ID: expected-type-id=%s, " \
55 "field-addr=%p", \
56 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
57
58#define BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
59 BT_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
60 _name " is not set: field-addr=%p", (_field))
61
62#define BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
63 BT_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
64
65struct bt_ctf_field_common;
66
67typedef void (*bt_ctf_field_common_method_set_is_frozen)(struct bt_ctf_field_common *,
68 bool);
69typedef int (*bt_ctf_field_common_method_validate)(struct bt_ctf_field_common *);
70typedef struct bt_ctf_field_common *(*bt_ctf_field_common_method_copy)(
71 struct bt_ctf_field_common *);
72typedef bt_bool (*bt_ctf_field_common_method_is_set)(struct bt_ctf_field_common *);
73typedef void (*bt_ctf_field_common_method_reset)(struct bt_ctf_field_common *);
74
75struct bt_ctf_field_common_methods {
76 bt_ctf_field_common_method_set_is_frozen set_is_frozen;
77 bt_ctf_field_common_method_validate validate;
78 bt_ctf_field_common_method_copy copy;
79 bt_ctf_field_common_method_is_set is_set;
80 bt_ctf_field_common_method_reset reset;
81};
82
83struct bt_ctf_field_common {
84 struct bt_object base;
85 struct bt_ctf_field_type_common *type;
86 struct bt_ctf_field_common_methods *methods;
87 bool payload_set;
88 bool frozen;
89
90 /*
91 * Specialized data for either CTF IR or CTF writer APIs.
92 * See comment in `field-types-internal.h` for more details.
93 */
94 union {
95 struct {
96 } ir;
97 struct {
98 void *serialize_func;
99 } writer;
100 } spec;
101};
102
103struct bt_ctf_field_common_integer {
104 struct bt_ctf_field_common common;
105 union {
106 int64_t signd;
107 uint64_t unsignd;
108 } payload;
109};
110
111struct bt_ctf_field_common_floating_point {
112 struct bt_ctf_field_common common;
113 double payload;
114};
115
116struct bt_ctf_field_common_structure {
117 struct bt_ctf_field_common common;
118
119 /* Array of `struct bt_ctf_field_common *`, owned by this */
120 GPtrArray *fields;
121};
122
123struct bt_ctf_field_common_variant {
124 struct bt_ctf_field_common common;
125
126 union {
127 uint64_t u;
128 int64_t i;
129 } tag_value;
130
131 /* Weak: belongs to `choices` below */
132 struct bt_ctf_field_common *current_field;
133
134 /* Array of `struct bt_ctf_field_common *`, owned by this */
135 GPtrArray *fields;
136};
137
138struct bt_ctf_field_common_array {
139 struct bt_ctf_field_common common;
140
141 /* Array of `struct bt_ctf_field_common *`, owned by this */
142 GPtrArray *elements;
143};
144
145struct bt_ctf_field_common_sequence {
146 struct bt_ctf_field_common common;
147
148 /*
149 * This is the true sequence field's length: its value can be
150 * less than `elements->len` below because we never shrink the
151 * array of elements to avoid reallocation.
152 */
153 uint64_t length;
154
155 /* Array of `struct bt_ctf_field_common *`, owned by this */
156 GPtrArray *elements;
157};
158
159struct bt_ctf_field_common_string {
160 struct bt_ctf_field_common common;
161 GArray *buf;
162 size_t size;
163};
164
165BT_HIDDEN
166struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field);
167
168BT_HIDDEN
169int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field,
170 struct bt_ctf_field_type_common *type,
171 bool is_shared, bt_object_release_func release_func,
172 struct bt_ctf_field_common_methods *methods,
173 bt_ctf_field_common_create_func field_create_func,
174 GDestroyNotify field_release_func);
175
176BT_HIDDEN
177int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field,
178 struct bt_ctf_field_type_common *type,
179 bool is_shared, bt_object_release_func release_func,
180 struct bt_ctf_field_common_methods *methods,
181 bt_ctf_field_common_create_func field_create_func,
182 GDestroyNotify field_destroy_func);
183
184BT_HIDDEN
185int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field,
186 struct bt_ctf_field_type_common *type,
187 bool is_shared, bt_object_release_func release_func,
188 struct bt_ctf_field_common_methods *methods,
189 GDestroyNotify field_destroy_func);
190
191BT_HIDDEN
192int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field,
193 struct bt_ctf_field_type_common *type,
194 bool is_shared, bt_object_release_func release_func,
195 struct bt_ctf_field_common_methods *methods,
196 bt_ctf_field_common_create_func field_create_func,
197 GDestroyNotify field_release_func);
198
199BT_HIDDEN
200int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
201 struct bt_ctf_field_type_common *type,
202 bool is_shared, bt_object_release_func release_func,
203 struct bt_ctf_field_common_methods *methods);
204
205BT_HIDDEN
206int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field);
207
208BT_HIDDEN
209int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field);
210
211BT_HIDDEN
212int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field);
213
214BT_HIDDEN
215int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field);
216
217BT_HIDDEN
218int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field);
219
220BT_HIDDEN
221void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field);
222
223BT_HIDDEN
224void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field);
225
226BT_HIDDEN
227void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field);
228
229BT_HIDDEN
230void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field);
231
232BT_HIDDEN
233void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field);
234
235BT_HIDDEN
236void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field,
237 bool is_frozen);
238
239BT_HIDDEN
240void bt_ctf_field_common_structure_set_is_frozen_recursive(
241 struct bt_ctf_field_common *field, bool is_frozen);
242
243BT_HIDDEN
244void bt_ctf_field_common_variant_set_is_frozen_recursive(
245 struct bt_ctf_field_common *field, bool is_frozen);
246
247BT_HIDDEN
248void bt_ctf_field_common_array_set_is_frozen_recursive(
249 struct bt_ctf_field_common *field, bool is_frozen);
250
251BT_HIDDEN
252void bt_ctf_field_common_sequence_set_is_frozen_recursive(
253 struct bt_ctf_field_common *field, bool is_frozen);
254
255BT_HIDDEN
256void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field,
257 bool is_frozen);
258
259BT_HIDDEN
260bt_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field);
261
262BT_HIDDEN
263bt_bool bt_ctf_field_common_structure_is_set_recursive(
264 struct bt_ctf_field_common *field);
265
266BT_HIDDEN
267bt_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field);
268
269BT_HIDDEN
270bt_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field);
271
272BT_HIDDEN
273bt_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field);
274
275#ifdef BT_DEV_MODE
276# define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
277# define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
278# define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
279# define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
280# define bt_ctf_field_common_set _bt_ctf_field_common_set
281#else
282# define bt_ctf_field_common_validate_recursive(_field) (-1)
283# define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
284# define bt_ctf_field_common_is_set_recursive(_field) (BT_FALSE)
285# define bt_ctf_field_common_reset_recursive(_field)
286# define bt_ctf_field_common_set(_field, _val)
287#endif
288
289BT_ASSERT_FUNC
290static inline bool field_type_common_has_known_id(
291 struct bt_ctf_field_type_common *ft)
292{
293 return (int) ft->id > BT_CTF_FIELD_TYPE_ID_UNKNOWN ||
294 (int) ft->id < BT_CTF_FIELD_TYPE_ID_NR;
295}
296
297static inline
298int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common *field)
299{
300 int ret = 0;
301
302 if (!field) {
303 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
304 ret = -1;
305 goto end;
306 }
307
308 BT_ASSERT(field_type_common_has_known_id(field->type));
309
310 if (field->methods->validate) {
311 ret = field->methods->validate(field);
312 }
313
314end:
315 return ret;
316}
317
318static inline
319void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common *field)
320{
321 BT_ASSERT(field);
322 BT_ASSERT(field->methods->reset);
323 field->methods->reset(field);
324}
325
326static inline
327void _bt_ctf_field_common_set(struct bt_ctf_field_common *field, bool value)
328{
329 BT_ASSERT(field);
330 field->payload_set = value;
331}
332
333static inline
334bt_bool _bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common *field)
335{
336 bt_bool is_set = BT_FALSE;
337
338 if (!field) {
339 goto end;
340 }
341
342 BT_ASSERT(field_type_common_has_known_id(field->type));
343 BT_ASSERT(field->methods->is_set);
344 is_set = field->methods->is_set(field);
345
346end:
347 return is_set;
348}
349
350static inline
351void bt_ctf_field_common_initialize(struct bt_ctf_field_common *field,
352 struct bt_ctf_field_type_common *ft, bool is_shared,
353 bt_object_release_func release_func,
354 struct bt_ctf_field_common_methods *methods)
355{
356 BT_ASSERT(field);
357 BT_ASSERT(ft);
358 bt_object_init(&field->base, is_shared, release_func);
359 field->methods = methods;
360 field->type = bt_get(ft);
361}
362
363static inline
364struct bt_ctf_field_type_common *bt_ctf_field_common_borrow_type(
365 struct bt_ctf_field_common *field)
366{
367 struct bt_ctf_field_type_common *ret = NULL;
368
369 BT_ASSERT_PRE_NON_NULL(field, "Field");
370 ret = field->type;
371 return ret;
372}
373
374static inline
375int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common *field)
376{
377 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
378
379 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
380 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_SEQUENCE,
381 "Field");
382 return (int64_t) sequence->length;
383}
384
385static inline
386int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common *field,
387 uint64_t length, bt_ctf_field_common_create_func field_create_func)
388{
389 int ret = 0;
390 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
391
392 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
393 BT_ASSERT_PRE(((int64_t) length) >= 0,
394 "Invalid sequence length (too large): length=%" PRId64,
395 length);
396 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "Sequence field");
397
398 if (unlikely(length > sequence->elements->len)) {
399 /* Make more room */
400 struct bt_ctf_field_type_common_sequence *sequence_ft;
401 uint64_t cur_len = sequence->elements->len;
402 uint64_t i;
403
404 g_ptr_array_set_size(sequence->elements, length);
405 sequence_ft = BT_CTF_FROM_COMMON(sequence->common.type);
406
407 for (i = cur_len; i < sequence->elements->len; i++) {
408 struct bt_ctf_field_common *elem_field =
409 field_create_func(sequence_ft->element_ft);
410
411 if (!elem_field) {
412 ret = -1;
413 goto end;
414 }
415
416 BT_ASSERT(!sequence->elements->pdata[i]);
417 sequence->elements->pdata[i] = elem_field;
418 }
419 }
420
421 sequence->length = length;
422
423end:
424 return ret;
425}
426
427static inline
428struct bt_ctf_field_common *bt_ctf_field_common_structure_borrow_field_by_name(
429 struct bt_ctf_field_common *field, const char *name)
430{
431 struct bt_ctf_field_common *ret = NULL;
432 GQuark field_quark;
433 struct bt_ctf_field_type_common_structure *structure_ft;
434 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
435 size_t index;
436 GHashTable *field_name_to_index;
437
438 BT_ASSERT_PRE_NON_NULL(field, "Structure field");
439 BT_ASSERT_PRE_NON_NULL(name, "Field name");
440 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
441 BT_CTF_FIELD_TYPE_ID_STRUCT, "Field");
442 structure_ft = BT_CTF_FROM_COMMON(field->type);
443 field_name_to_index = structure_ft->field_name_to_index;
444 field_quark = g_quark_from_string(name);
445 if (!g_hash_table_lookup_extended(field_name_to_index,
446 GUINT_TO_POINTER(field_quark),
447 NULL, (gpointer *) &index)) {
448 BT_LOGV("Invalid parameter: no such field in structure field's type: "
449 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
450 field, field->type, name);
451 goto error;
452 }
453
454 ret = structure->fields->pdata[index];
455 BT_ASSERT(ret);
456
457error:
458 return ret;
459}
460
461static inline
462struct bt_ctf_field_common *bt_ctf_field_common_structure_borrow_field_by_index(
463 struct bt_ctf_field_common *field, uint64_t index)
464{
465 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
466
467 BT_ASSERT_PRE_NON_NULL(field, "Structure field");
468 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
469 BT_CTF_FIELD_TYPE_ID_STRUCT, "Field");
470 BT_ASSERT_PRE(index < structure->fields->len,
471 "Index is out of bound: struct-field-addr=%p, "
472 "index=%" PRIu64 ", count=%u", field, index,
473 structure->fields->len);
474 return structure->fields->pdata[index];
475}
476
477static inline
478struct bt_ctf_field_common *bt_ctf_field_common_array_borrow_field(
479 struct bt_ctf_field_common *field, uint64_t index)
480{
481 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
482
483 BT_ASSERT_PRE_NON_NULL(field, "Array field");
484 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_ARRAY,
485 "Field");
486 BT_ASSERT_PRE(index < array->elements->len,
487 "Index is out of bound: array-field-addr=%p, "
488 "index=%" PRIu64 ", count=%u", field,
489 index, array->elements->len);
490 return array->elements->pdata[(size_t) index];
491}
492
493static inline
494struct bt_ctf_field_common *bt_ctf_field_common_sequence_borrow_field(
495 struct bt_ctf_field_common *field, uint64_t index)
496{
497 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
498
499 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
500 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_SEQUENCE,
501 "Field");
502 BT_ASSERT_PRE(index < sequence->length,
503 "Index is out of bound: seq-field-addr=%p, "
504 "index=%" PRIu64 ", count=%u", field, index,
505 sequence->elements->len);
506 return sequence->elements->pdata[(size_t) index];
507}
508
509static inline
510int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common *variant_field,
511 uint64_t tag_uval, bool is_signed)
512{
513 int ret = 0;
514 int64_t choice_index;
515 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
516
517 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
518 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
519 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
520
521 /* Find matching index in variant field's type */
522 choice_index = bt_ctf_field_type_common_variant_find_choice_index(
523 variant_field->type, tag_uval, is_signed);
524 if (choice_index < 0) {
525 ret = -1;
526 goto end;
527 }
528
529 /* Select corresponding field */
530 BT_ASSERT(choice_index < variant->fields->len);
531 variant->current_field = variant->fields->pdata[choice_index];
532 variant->tag_value.u = tag_uval;
533
534end:
535 return ret;
536}
537
538static inline
539struct bt_ctf_field_common *bt_ctf_field_common_variant_borrow_current_field(
540 struct bt_ctf_field_common *variant_field)
541{
542 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
543
544 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
545 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
546 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
547 BT_ASSERT_PRE(variant->current_field,
548 "Variant field has no current field: field-addr=%p", variant_field);
549 return variant->current_field;
550}
551
552static inline
553int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common *variant_field,
554 int64_t *tag)
555{
556 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
557
558 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
559 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
560 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
561 BT_ASSERT_PRE(variant->current_field,
562 "Variant field has no current field: field-addr=%p", variant_field);
563 *tag = variant->tag_value.i;
564 return 0;
565}
566
567static inline
568int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common *variant_field,
569 uint64_t *tag)
570{
571 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
572
573 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
574 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
575 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
576 BT_ASSERT_PRE(variant->current_field,
577 "Variant field has no current field: field-addr=%p", variant_field);
578 *tag = variant->tag_value.u;
579 return 0;
580}
581
582static inline
583int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common *field,
584 double *value)
585{
586 struct bt_ctf_field_common_floating_point *floating_point =
587 BT_CTF_FROM_COMMON(field);
588
589 BT_ASSERT_PRE_NON_NULL(field, "Floating point number field");
590 BT_ASSERT_PRE_NON_NULL(value, "Value");
591 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field");
592 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
593 BT_CTF_FIELD_TYPE_ID_FLOAT, "Field");
594 *value = floating_point->payload;
595 return 0;
596}
597
598static inline
599int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common *field,
600 double value)
601{
602 struct bt_ctf_field_common_floating_point *floating_point =
603 BT_CTF_FROM_COMMON(field);
604
605 BT_ASSERT_PRE_NON_NULL(field, "Floating point number field");
606 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "Floating point number field");
607 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
608 BT_CTF_FIELD_TYPE_ID_FLOAT, "Field");
609 floating_point->payload = value;
610 bt_ctf_field_common_set(field, true);
611 return 0;
612}
613
614static inline
615const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common *field)
616{
617 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
618
619 BT_ASSERT_PRE_NON_NULL(field, "String field");
620 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field");
621 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
622 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
623 return (const char *) string->buf->data;
624}
625
626static inline
627int bt_ctf_field_common_string_clear(struct bt_ctf_field_common *field)
628{
629 struct bt_ctf_field_common_string *string_field = BT_CTF_FROM_COMMON(field);
630
631 BT_ASSERT_PRE_NON_NULL(field, "String field");
632 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
633 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
634 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
635 string_field->size = 0;
636 bt_ctf_field_common_set(field, true);
637 return 0;
638}
639
640static inline
641int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common *field,
642 const char *value, unsigned int length)
643{
644 struct bt_ctf_field_common_string *string_field = BT_CTF_FROM_COMMON(field);
645 char *data;
646 size_t new_size;
647
648 BT_ASSERT_PRE_NON_NULL(field, "String field");
649 BT_ASSERT_PRE_NON_NULL(value, "Value");
650 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
651 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
652 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
653
654 /* Make sure no null bytes are appended */
655 BT_ASSERT_PRE(memchr(value, '\0', length) == NULL,
656 "String value to append contains a null character: "
657 "partial-value=\"%.32s\", length=%u", value, length);
658
659 new_size = string_field->size + length;
660
661 if (unlikely(new_size + 1 > string_field->buf->len)) {
662 g_array_set_size(string_field->buf, new_size + 1);
663 }
664
665 data = string_field->buf->data;
666 memcpy(data + string_field->size, value, length);
667 ((char *) string_field->buf->data)[new_size] = '\0';
668 string_field->size = new_size;
669 bt_ctf_field_common_set(field, true);
670 return 0;
671}
672
673static inline
674int bt_ctf_field_common_string_append(struct bt_ctf_field_common *field,
675 const char *value)
676{
677 BT_ASSERT_PRE_NON_NULL(value, "Value");
678 return bt_ctf_field_common_string_append_len(field, value,
679 strlen(value));
680}
681
682static inline
683int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common *field,
684 const char *value)
685{
686 BT_ASSERT_PRE_NON_NULL(field, "String field");
687 BT_ASSERT_PRE_NON_NULL(value, "Value");
688 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
689 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
690 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
691 bt_ctf_field_common_string_clear(field);
692 return bt_ctf_field_common_string_append_len(field,
693 value, strlen(value));
694}
695
696static inline
697void bt_ctf_field_common_finalize(struct bt_ctf_field_common *field)
698{
699 BT_ASSERT(field);
700 BT_LOGD_STR("Putting field's type.");
701 bt_put(field->type);
702}
703
704static inline
705void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common *field)
706{
707 BT_ASSERT(field);
708 BT_LOGD("Finalizing common integer field object: addr=%p", field);
709 bt_ctf_field_common_finalize(field);
710}
711
712static inline
713void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common *field)
714{
715 BT_ASSERT(field);
716 BT_LOGD("Finalizing common floating point number field object: addr=%p", field);
717 bt_ctf_field_common_finalize(field);
718}
719
720static inline
721void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common *field)
722{
723 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
724
725 BT_ASSERT(field);
726 BT_LOGD("Finalizing common structure field object: addr=%p", field);
727 bt_ctf_field_common_finalize(field);
728
729 if (structure->fields) {
730 g_ptr_array_free(structure->fields, TRUE);
731 }
732}
733
734static inline
735void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common *field)
736{
737 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
738
739 BT_ASSERT(field);
740 BT_LOGD("Finalizing common variant field object: addr=%p", field);
741 bt_ctf_field_common_finalize(field);
742
743 if (variant->fields) {
744 g_ptr_array_free(variant->fields, TRUE);
745 }
746}
747
748static inline
749void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common *field)
750{
751 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
752
753 BT_ASSERT(field);
754 BT_LOGD("Finalizing common array field object: addr=%p", field);
755 bt_ctf_field_common_finalize(field);
756
757 if (array->elements) {
758 g_ptr_array_free(array->elements, TRUE);
759 }
760}
761
762static inline
763void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common *field)
764{
765 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
766
767 BT_ASSERT(field);
768 BT_LOGD("Finalizing common sequence field object: addr=%p", field);
769 bt_ctf_field_common_finalize(field);
770
771 if (sequence->elements) {
772 g_ptr_array_free(sequence->elements, TRUE);
773 }
774}
775
776static inline
777void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common *field)
778{
779 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
780
781 BT_ASSERT(field);
782 BT_LOGD("Finalizing common string field object: addr=%p", field);
783 bt_ctf_field_common_finalize(field);
784
785 if (string->buf) {
786 g_array_free(string->buf, TRUE);
787 }
788}
789
790BT_ASSERT_PRE_FUNC
791static inline bool value_is_in_range_signed(unsigned int size, int64_t value)
792{
793 bool ret = true;
794 int64_t min_value, max_value;
795
796 min_value = -(1ULL << (size - 1));
797 max_value = (1ULL << (size - 1)) - 1;
798 if (value < min_value || value > max_value) {
799 BT_LOGF("Value is out of bounds: value=%" PRId64 ", "
800 "min-value=%" PRId64 ", max-value=%" PRId64,
801 value, min_value, max_value);
802 ret = false;
803 }
804
805 return ret;
806}
807
808BT_ASSERT_PRE_FUNC
809static inline bool value_is_in_range_unsigned(unsigned int size, uint64_t value)
810{
811 bool ret = true;
812 int64_t max_value;
813
814 max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1;
815 if (value > max_value) {
816 BT_LOGF("Value is out of bounds: value=%" PRIu64 ", "
817 "max-value=%" PRIu64,
818 value, max_value);
819 ret = false;
820 }
821
822 return ret;
823}
3dca2276 824
312c056a 825struct bt_ctf_field_enumeration {
16ca5ff0
PP
826 struct bt_ctf_field_common common;
827 struct bt_ctf_field_common_integer *container;
312c056a
PP
828};
829
830struct bt_ctf_field_variant {
16ca5ff0 831 struct bt_ctf_field_common_variant common;
312c056a
PP
832 struct bt_ctf_field_enumeration *tag;
833};
834
3dca2276
PP
835BT_HIDDEN
836int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field,
837 struct bt_ctf_stream_pos *pos,
838 enum bt_ctf_byte_order native_byte_order);
839
312c056a
PP
840BT_HIDDEN
841int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
842 const char *name, struct bt_ctf_field *value);
843
844BT_HIDDEN
845struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container(
846 struct bt_ctf_field *field);
847
3dca2276
PP
848static inline
849bt_bool bt_ctf_field_is_set_recursive(struct bt_ctf_field *field)
850{
16ca5ff0 851 return bt_ctf_field_common_is_set_recursive((void *) field);
3dca2276
PP
852}
853
854#endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */
This page took 0.057904 seconds and 4 git commands to generate.