Remove unnecessary inclusions of "internal public" headers
[babeltrace.git] / src / ctf-writer / fields.h
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
8 */
9
10#ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
11#define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
12
13#include <glib.h>
14#include <inttypes.h>
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdint.h>
18#include <string.h>
19
20#include <babeltrace2-ctf-writer/fields.h>
21
22#include "common/macros.h"
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
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)), \
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
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), \
39 _name " is not set: field-addr=%p", (_field))
40
41#define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
42 BT_CTF_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
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 *);
51typedef bt_ctf_bool (*bt_ctf_field_common_method_is_set)(struct bt_ctf_field_common *);
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 {
63 struct bt_ctf_object base;
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.
71 * See comment in `field-types.h` for more details.
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
144struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field);
145
146int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field,
147 struct bt_ctf_field_type_common *type,
148 bool is_shared, bt_ctf_object_release_func release_func,
149 struct bt_ctf_field_common_methods *methods,
150 bt_ctf_field_common_create_func field_create_func,
151 GDestroyNotify field_release_func);
152
153int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field,
154 struct bt_ctf_field_type_common *type,
155 bool is_shared, bt_ctf_object_release_func release_func,
156 struct bt_ctf_field_common_methods *methods,
157 bt_ctf_field_common_create_func field_create_func,
158 GDestroyNotify field_destroy_func);
159
160int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field,
161 struct bt_ctf_field_type_common *type,
162 bool is_shared, bt_ctf_object_release_func release_func,
163 struct bt_ctf_field_common_methods *methods,
164 GDestroyNotify field_destroy_func);
165
166int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field,
167 struct bt_ctf_field_type_common *type,
168 bool is_shared, bt_ctf_object_release_func release_func,
169 struct bt_ctf_field_common_methods *methods,
170 bt_ctf_field_common_create_func field_create_func,
171 GDestroyNotify field_release_func);
172
173int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
174 struct bt_ctf_field_type_common *type,
175 bool is_shared, bt_ctf_object_release_func release_func,
176 struct bt_ctf_field_common_methods *methods);
177
178int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field);
179
180int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field);
181
182int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field);
183
184int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field);
185
186int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field);
187
188void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field);
189
190void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field);
191
192void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field);
193
194void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field);
195
196void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field);
197
198void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field,
199 bool is_frozen);
200
201void bt_ctf_field_common_structure_set_is_frozen_recursive(
202 struct bt_ctf_field_common *field, bool is_frozen);
203
204void bt_ctf_field_common_variant_set_is_frozen_recursive(
205 struct bt_ctf_field_common *field, bool is_frozen);
206
207void bt_ctf_field_common_array_set_is_frozen_recursive(
208 struct bt_ctf_field_common *field, bool is_frozen);
209
210void bt_ctf_field_common_sequence_set_is_frozen_recursive(
211 struct bt_ctf_field_common *field, bool is_frozen);
212
213void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field,
214 bool is_frozen);
215
216bt_ctf_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field);
217
218bt_ctf_bool bt_ctf_field_common_structure_is_set_recursive(
219 struct bt_ctf_field_common *field);
220
221bt_ctf_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field);
222
223bt_ctf_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field);
224
225bt_ctf_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field);
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)
236# define bt_ctf_field_common_is_set_recursive(_field) (BT_CTF_FALSE)
237# define bt_ctf_field_common_reset_recursive(_field)
238# define bt_ctf_field_common_set(_field, _val)
239#endif
240
241BT_ASSERT_DBG_FUNC
242static inline bool field_type_common_has_known_id(
243 struct bt_ctf_field_type_common *ft)
244{
245 return (int) ft->id > BT_CTF_FIELD_TYPE_ID_UNKNOWN &&
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) {
255 BT_CTF_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
256 ret = -1;
257 goto end;
258 }
259
260 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
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{
273 BT_ASSERT_DBG(field);
274 BT_ASSERT_DBG(field->methods->reset);
275 field->methods->reset(field);
276}
277
278static inline
279void _bt_ctf_field_common_set(struct bt_ctf_field_common *field, bool value)
280{
281 BT_ASSERT_DBG(field);
282 field->payload_set = value;
283}
284
285static inline
286bt_ctf_bool _bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common *field)
287{
288 bt_ctf_bool is_set = BT_CTF_FALSE;
289
290 if (!field) {
291 goto end;
292 }
293
294 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
295 BT_ASSERT_DBG(field->methods->is_set);
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,
305 bt_ctf_object_release_func release_func,
306 struct bt_ctf_field_common_methods *methods)
307{
308 BT_ASSERT_DBG(field);
309 BT_ASSERT_DBG(ft);
310 bt_ctf_object_init(&field->base, is_shared, release_func);
311 field->methods = methods;
312 field->type = (void *) bt_ctf_object_get_ref(ft);
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
321 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
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
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,
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
344 BT_CTF_ASSERT_PRE_NON_NULL(field, "Sequence field");
345 BT_CTF_ASSERT_PRE(((int64_t) length) >= 0,
346 "Invalid sequence length (too large): length=%" PRId64,
347 length);
348 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "Sequence field");
349
350 if (G_UNLIKELY(length > sequence->elements->len)) {
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
368 BT_ASSERT_DBG(!sequence->elements->pdata[i]);
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
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,
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)) {
400 BT_LOGT("Invalid parameter: no such field in structure field's type: "
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];
407 BT_ASSERT_DBG(ret);
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
419 BT_CTF_ASSERT_PRE_NON_NULL(field, "Structure field");
420 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
421 BT_CTF_FIELD_TYPE_ID_STRUCT, "Field");
422 BT_CTF_ASSERT_PRE(index < structure->fields->len,
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
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,
437 "Field");
438 BT_CTF_ASSERT_PRE(index < array->elements->len,
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
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,
453 "Field");
454 BT_CTF_ASSERT_PRE(index < sequence->length,
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
469 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
470 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
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 */
482 BT_ASSERT_DBG(choice_index < variant->fields->len);
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
496 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
497 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
498 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
499 BT_CTF_ASSERT_PRE(variant->current_field,
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
510 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
511 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
512 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
513 BT_CTF_ASSERT_PRE(variant->current_field,
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
525 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
526 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
527 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
528 BT_CTF_ASSERT_PRE(variant->current_field,
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
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,
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
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,
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
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,
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
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,
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
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,
604 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
605
606 /* Make sure no null bytes are appended */
607 BT_CTF_ASSERT_PRE(!memchr(value, '\0', length),
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
613 if (G_UNLIKELY(new_size + 1 > string_field->buf->len)) {
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{
629 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
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{
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,
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{
651 BT_ASSERT_DBG(field);
652 BT_LOGD_STR("Putting field's type.");
653 bt_ctf_object_put_ref(field->type);
654}
655
656static inline
657void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common *field)
658{
659 BT_ASSERT_DBG(field);
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{
667 BT_ASSERT_DBG(field);
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
677 BT_ASSERT_DBG(field);
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
691 BT_ASSERT_DBG(field);
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
705 BT_ASSERT_DBG(field);
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
719 BT_ASSERT_DBG(field);
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
733 BT_ASSERT_DBG(field);
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
742BT_CTF_ASSERT_PRE_FUNC
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
760BT_CTF_ASSERT_PRE_FUNC
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}
776
777struct bt_ctf_field_enumeration {
778 struct bt_ctf_field_common common;
779 struct bt_ctf_field_common_integer *container;
780};
781
782struct bt_ctf_field_variant {
783 struct bt_ctf_field_common_variant common;
784 struct bt_ctf_field_enumeration *tag;
785};
786
787int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field,
788 struct bt_ctfser *ctfser,
789 enum bt_ctf_byte_order native_byte_order);
790
791int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
792 const char *name, struct bt_ctf_field *value);
793
794struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container(
795 struct bt_ctf_field *field);
796
797#ifndef BT_DEV_MODE
798#define BT_FIELD_UNUSED_ATTR __attribute__((unused))
799#else
800#define BT_FIELD_UNUSED_ATTR
801#endif
802
803static inline
804bt_ctf_bool bt_ctf_field_is_set_recursive(
805 struct bt_ctf_field *field BT_FIELD_UNUSED_ATTR)
806{
807 return bt_ctf_field_common_is_set_recursive((void *) field);
808}
809
810#endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */
This page took 0.025905 seconds and 4 git commands to generate.