cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / ctf-writer / fields.h
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
44 struct bt_ctf_field_common;
45
46 typedef void (*bt_ctf_field_common_method_set_is_frozen)(struct bt_ctf_field_common *,
47 bool);
48 typedef int (*bt_ctf_field_common_method_validate)(struct bt_ctf_field_common *);
49 typedef struct bt_ctf_field_common *(*bt_ctf_field_common_method_copy)(
50 struct bt_ctf_field_common *);
51 typedef bt_ctf_bool (*bt_ctf_field_common_method_is_set)(struct bt_ctf_field_common *);
52 typedef void (*bt_ctf_field_common_method_reset)(struct bt_ctf_field_common *);
53
54 struct 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
62 struct 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
82 struct 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
90 struct bt_ctf_field_common_floating_point {
91 struct bt_ctf_field_common common;
92 double payload;
93 };
94
95 struct 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
102 struct 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
117 struct 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
124 struct 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
138 struct bt_ctf_field_common_string {
139 struct bt_ctf_field_common common;
140 GArray *buf;
141 size_t size;
142 };
143
144 struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field);
145
146 int 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
153 int 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
160 int 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
166 int 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
173 int 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
178 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field);
179
180 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field);
181
182 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field);
183
184 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field);
185
186 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field);
187
188 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field);
189
190 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field);
191
192 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field);
193
194 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field);
195
196 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field);
197
198 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field,
199 bool is_frozen);
200
201 void bt_ctf_field_common_structure_set_is_frozen_recursive(
202 struct bt_ctf_field_common *field, bool is_frozen);
203
204 void bt_ctf_field_common_variant_set_is_frozen_recursive(
205 struct bt_ctf_field_common *field, bool is_frozen);
206
207 void bt_ctf_field_common_array_set_is_frozen_recursive(
208 struct bt_ctf_field_common *field, bool is_frozen);
209
210 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
211 struct bt_ctf_field_common *field, bool is_frozen);
212
213 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field,
214 bool is_frozen);
215
216 bt_ctf_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field);
217
218 bt_ctf_bool bt_ctf_field_common_structure_is_set_recursive(
219 struct bt_ctf_field_common *field);
220
221 bt_ctf_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field);
222
223 bt_ctf_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field);
224
225 bt_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
241 BT_ASSERT_DBG_FUNC
242 static 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
249 static inline
250 int _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
266 end:
267 return ret;
268 }
269
270 static inline
271 void _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
278 static inline
279 void _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
285 static inline
286 bt_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
298 end:
299 return is_set;
300 }
301
302 static inline
303 void 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
315 static inline
316 struct 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
326 static inline
327 int64_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
337 static inline
338 int 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
375 end:
376 return ret;
377 }
378
379 static inline
380 struct 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
409 error:
410 return ret;
411 }
412
413 static inline
414 struct 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
429 static inline
430 struct 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
445 static inline
446 struct 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
461 static inline
462 int 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
486 end:
487 return ret;
488 }
489
490 static inline
491 struct 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
504 static inline
505 int 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
519 static inline
520 int 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
534 static inline
535 int 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
550 static inline
551 int 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
566 static inline
567 const 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
578 static inline
579 int 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
592 static inline
593 int 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
625 static inline
626 int 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
634 static inline
635 int 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
648 static inline
649 void 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
656 static inline
657 void 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
664 static inline
665 void 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
672 static inline
673 void 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
686 static inline
687 void 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
700 static inline
701 void 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
714 static inline
715 void 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
728 static inline
729 void 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
742 BT_CTF_ASSERT_PRE_FUNC
743 static 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
760 BT_CTF_ASSERT_PRE_FUNC
761 static 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
777 struct bt_ctf_field_enumeration {
778 struct bt_ctf_field_common common;
779 struct bt_ctf_field_common_integer *container;
780 };
781
782 struct bt_ctf_field_variant {
783 struct bt_ctf_field_common_variant common;
784 struct bt_ctf_field_enumeration *tag;
785 };
786
787 int 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
791 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
792 const char *name, struct bt_ctf_field *value);
793
794 struct 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
803 static inline
804 bt_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.047446 seconds and 4 git commands to generate.