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