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