lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / include / babeltrace / ctf-ir / fields-internal.h
1 #ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
3
4 /*
5 * Babeltrace - CTF IR: Event Fields internal
6 *
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/ctf-ir/field-types-internal.h>
33 #include <babeltrace/ctf-ir/utils-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/types.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <stdbool.h>
40 #include <glib.h>
41
42 #define BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
43 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
44 _name " has the wrong type ID: expected-type-id=%s, " \
45 "%![field-]+_f", \
46 bt_common_field_type_id_string((int) (_type_id)), (_field))
47
48 #define BT_ASSERT_PRE_FIELD_COMMON_IS_SET(_field, _name) \
49 BT_ASSERT_PRE(bt_field_common_is_set_recursive(_field), \
50 _name " is not set: %!+_f", (_field))
51
52 #define BT_ASSERT_PRE_FIELD_COMMON_HOT(_field, _name) \
53 BT_ASSERT_PRE_HOT((_field), (_name), ": +%!+_f", (_field))
54
55 struct bt_field;
56 struct bt_field_common;
57
58 typedef void (*bt_field_common_method_set_is_frozen)(struct bt_field_common *,
59 bool);
60 typedef int (*bt_field_common_method_validate)(struct bt_field_common *);
61 typedef struct bt_field_common *(*bt_field_common_method_copy)(
62 struct bt_field_common *);
63 typedef bt_bool (*bt_field_common_method_is_set)(struct bt_field_common *);
64 typedef void (*bt_field_common_method_reset)(struct bt_field_common *);
65
66 struct bt_field_common_methods {
67 bt_field_common_method_set_is_frozen set_is_frozen;
68 bt_field_common_method_validate validate;
69 bt_field_common_method_copy copy;
70 bt_field_common_method_is_set is_set;
71 bt_field_common_method_reset reset;
72 };
73
74 struct bt_field_common {
75 struct bt_object base;
76 struct bt_field_type_common *type;
77 struct bt_field_common_methods *methods;
78 bool payload_set;
79 bool frozen;
80
81 /*
82 * Specialized data for either CTF IR or CTF writer APIs.
83 * See comment in `field-types-internal.h` for more details.
84 */
85 union {
86 struct {
87 } ir;
88 struct {
89 void *serialize_func;
90 } writer;
91 } spec;
92 };
93
94 struct bt_field_common_integer {
95 struct bt_field_common common;
96 union {
97 int64_t signd;
98 uint64_t unsignd;
99 } payload;
100 };
101
102 struct bt_field_common_floating_point {
103 struct bt_field_common common;
104 double payload;
105 };
106
107 struct bt_field_common_structure {
108 struct bt_field_common common;
109
110 /* Array of `struct bt_field_common *`, owned by this */
111 GPtrArray *fields;
112 };
113
114 struct bt_field_common_variant {
115 struct bt_field_common common;
116
117 union {
118 uint64_t u;
119 int64_t i;
120 } tag_value;
121
122 /* Weak: belongs to `choices` below */
123 struct bt_field_common *current_field;
124
125 /* Array of `struct bt_field_common *`, owned by this */
126 GPtrArray *fields;
127 };
128
129 struct bt_field_common_array {
130 struct bt_field_common common;
131
132 /* Array of `struct bt_field_common *`, owned by this */
133 GPtrArray *elements;
134 };
135
136 struct bt_field_common_sequence {
137 struct bt_field_common common;
138
139 /*
140 * This is the true sequence field's length: its value can be
141 * less than `elements->len` below because we never shrink the
142 * array of elements to avoid reallocation.
143 */
144 uint64_t length;
145
146 /* Array of `struct bt_field_common *`, owned by this */
147 GPtrArray *elements;
148 };
149
150 struct bt_field_common_string {
151 struct bt_field_common common;
152 GString *payload;
153 };
154
155 struct bt_field_enumeration {
156 struct bt_field_common_integer common;
157 };
158
159 BT_HIDDEN
160 struct bt_field_common *bt_field_common_copy(struct bt_field_common *field);
161
162 BT_HIDDEN
163 int bt_field_common_structure_initialize(struct bt_field_common *field,
164 struct bt_field_type_common *type,
165 bt_object_release_func release_func,
166 struct bt_field_common_methods *methods,
167 bt_field_common_create_func field_create_func,
168 GDestroyNotify field_release_func);
169
170 BT_HIDDEN
171 int bt_field_common_array_initialize(struct bt_field_common *field,
172 struct bt_field_type_common *type,
173 bt_object_release_func release_func,
174 struct bt_field_common_methods *methods,
175 bt_field_common_create_func field_create_func,
176 GDestroyNotify field_destroy_func);
177
178 BT_HIDDEN
179 int bt_field_common_sequence_initialize(struct bt_field_common *field,
180 struct bt_field_type_common *type,
181 bt_object_release_func release_func,
182 struct bt_field_common_methods *methods,
183 GDestroyNotify field_destroy_func);
184
185 BT_HIDDEN
186 int bt_field_common_variant_initialize(struct bt_field_common *field,
187 struct bt_field_type_common *type,
188 bt_object_release_func release_func,
189 struct bt_field_common_methods *methods,
190 bt_field_common_create_func field_create_func,
191 GDestroyNotify field_release_func);
192
193 BT_HIDDEN
194 int bt_field_common_generic_validate(struct bt_field_common *field);
195
196 BT_HIDDEN
197 int bt_field_common_structure_validate_recursive(struct bt_field_common *field);
198
199 BT_HIDDEN
200 int bt_field_common_variant_validate_recursive(struct bt_field_common *field);
201
202 BT_HIDDEN
203 int bt_field_common_array_validate_recursive(struct bt_field_common *field);
204
205 BT_HIDDEN
206 int bt_field_common_sequence_validate_recursive(struct bt_field_common *field);
207
208 BT_HIDDEN
209 void bt_field_common_generic_reset(struct bt_field_common *field);
210
211 BT_HIDDEN
212 void bt_field_common_structure_reset_recursive(struct bt_field_common *field);
213
214 BT_HIDDEN
215 void bt_field_common_variant_reset_recursive(struct bt_field_common *field);
216
217 BT_HIDDEN
218 void bt_field_common_array_reset_recursive(struct bt_field_common *field);
219
220 BT_HIDDEN
221 void bt_field_common_sequence_reset_recursive(struct bt_field_common *field);
222
223 BT_HIDDEN
224 void bt_field_common_string_reset(struct bt_field_common *field);
225
226 BT_HIDDEN
227 void bt_field_common_generic_set_is_frozen(struct bt_field_common *field,
228 bool is_frozen);
229
230 BT_HIDDEN
231 void bt_field_common_structure_set_is_frozen_recursive(
232 struct bt_field_common *field, bool is_frozen);
233
234 BT_HIDDEN
235 void bt_field_common_variant_set_is_frozen_recursive(
236 struct bt_field_common *field, bool is_frozen);
237
238 BT_HIDDEN
239 void bt_field_common_array_set_is_frozen_recursive(
240 struct bt_field_common *field, bool is_frozen);
241
242 BT_HIDDEN
243 void bt_field_common_sequence_set_is_frozen_recursive(
244 struct bt_field_common *field, bool is_frozen);
245
246 BT_HIDDEN
247 void _bt_field_common_set_is_frozen_recursive(struct bt_field_common *field,
248 bool is_frozen);
249
250 BT_HIDDEN
251 bt_bool bt_field_common_generic_is_set(struct bt_field_common *field);
252
253 BT_HIDDEN
254 bt_bool bt_field_common_structure_is_set_recursive(
255 struct bt_field_common *field);
256
257 BT_HIDDEN
258 bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field);
259
260 BT_HIDDEN
261 bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field);
262
263 BT_HIDDEN
264 bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field);
265
266 #ifdef BT_DEV_MODE
267 # define bt_field_common_validate_recursive _bt_field_common_validate_recursive
268 # define bt_field_common_set_is_frozen_recursive _bt_field_common_set_is_frozen_recursive
269 # define bt_field_common_is_set_recursive _bt_field_common_is_set_recursive
270 # define bt_field_common_reset_recursive _bt_field_common_reset_recursive
271 # define bt_field_common_set _bt_field_common_set
272 # define bt_field_validate_recursive _bt_field_validate_recursive
273 # define bt_field_set_is_frozen_recursive _bt_field_set_is_frozen_recursive
274 # define bt_field_is_set_recursive _bt_field_is_set_recursive
275 # define bt_field_reset_recursive _bt_field_reset_recursive
276 # define bt_field_set _bt_field_set
277 #else
278 # define bt_field_common_validate_recursive(_field) (-1)
279 # define bt_field_common_set_is_frozen_recursive(_field, _is_frozen)
280 # define bt_field_common_is_set_recursive(_field) (BT_FALSE)
281 # define bt_field_common_reset_recursive(_field) (BT_TRUE)
282 # define bt_field_common_set(_field, _val)
283 # define bt_field_validate_recursive(_field) (-1)
284 # define bt_field_set_is_frozen_recursive(_field, _is_frozen)
285 # define bt_field_is_set_recursive(_field) (BT_FALSE)
286 # define bt_field_reset_recursive(_field) (BT_TRUE)
287 # define bt_field_set(_field, _val)
288 #endif
289
290 BT_ASSERT_FUNC
291 static inline bool field_type_common_has_known_id(
292 struct bt_field_type_common *ft)
293 {
294 return (int) ft->id > BT_FIELD_TYPE_ID_UNKNOWN ||
295 (int) ft->id < BT_FIELD_TYPE_ID_NR;
296 }
297
298 static inline
299 int _bt_field_common_validate_recursive(struct bt_field_common *field)
300 {
301 int ret = 0;
302
303 if (!field) {
304 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
305 ret = -1;
306 goto end;
307 }
308
309 BT_ASSERT(field_type_common_has_known_id(field->type));
310
311 if (field->methods->validate) {
312 ret = field->methods->validate(field);
313 }
314
315 end:
316 return ret;
317 }
318
319 static inline
320 void _bt_field_common_reset_recursive(struct bt_field_common *field)
321 {
322 BT_ASSERT(field);
323 BT_ASSERT(field->methods->reset);
324 field->methods->reset(field);
325 }
326
327 static inline
328 void _bt_field_common_set(struct bt_field_common *field, bool value)
329 {
330 BT_ASSERT(field);
331 field->payload_set = value;
332 }
333
334 static inline
335 bt_bool _bt_field_common_is_set_recursive(struct bt_field_common *field)
336 {
337 bt_bool is_set = BT_FALSE;
338
339 if (!field) {
340 goto end;
341 }
342
343 BT_ASSERT(field_type_common_has_known_id(field->type));
344 BT_ASSERT(field->methods->is_set);
345 is_set = field->methods->is_set(field);
346
347 end:
348 return is_set;
349 }
350
351 static inline
352 void bt_field_common_initialize(struct bt_field_common *field,
353 struct bt_field_type_common *ft,
354 bt_object_release_func release_func,
355 struct bt_field_common_methods *methods)
356 {
357 BT_ASSERT(field);
358 BT_ASSERT(ft);
359 bt_object_init(field, release_func);
360 field->methods = methods;
361 field->type = bt_get(ft);
362 }
363
364 static inline
365 struct bt_field_type_common *bt_field_common_borrow_type(
366 struct bt_field_common *field)
367 {
368 struct bt_field_type_common *ret = NULL;
369
370 BT_ASSERT_PRE_NON_NULL(field, "Field");
371 ret = field->type;
372 return ret;
373 }
374
375 static inline
376 int64_t bt_field_common_sequence_get_length(struct bt_field_common *field)
377 {
378 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
379
380 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
381 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_SEQUENCE,
382 "Field");
383 return (int64_t) sequence->length;
384 }
385
386 static inline
387 int bt_field_common_sequence_set_length(struct bt_field_common *field,
388 uint64_t length, bt_field_common_create_func field_create_func)
389 {
390 int ret = 0;
391 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
392
393 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
394 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "Sequence field");
395
396 if (length > sequence->elements->len) {
397 /* Make more room */
398 struct bt_field_type_common_sequence *sequence_ft;
399 uint64_t cur_len = sequence->elements->len;
400 uint64_t i;
401
402 g_ptr_array_set_size(sequence->elements, length);
403 sequence_ft = BT_FROM_COMMON(sequence->common.type);
404
405 for (i = cur_len; i < sequence->elements->len; i++) {
406 struct bt_field_common *elem_field =
407 field_create_func(sequence_ft->element_ft);
408
409 if (!elem_field) {
410 ret = -1;
411 goto end;
412 }
413
414 BT_ASSERT(!sequence->elements->pdata[i]);
415 sequence->elements->pdata[i] = elem_field;
416 }
417 }
418
419 sequence->length = length;
420
421 end:
422 return ret;
423 }
424
425 static inline
426 struct bt_field_common *bt_field_common_structure_borrow_field_by_name(
427 struct bt_field_common *field, const char *name)
428 {
429 struct bt_field_common *ret = NULL;
430 GQuark field_quark;
431 struct bt_field_type_common_structure *structure_ft;
432 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
433 size_t index;
434 GHashTable *field_name_to_index;
435
436 BT_ASSERT_PRE_NON_NULL(field, "Structure field");
437 BT_ASSERT_PRE_NON_NULL(name, "Field name");
438 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
439 BT_FIELD_TYPE_ID_STRUCT, "Field");
440 structure_ft = BT_FROM_COMMON(field->type);
441 field_name_to_index = structure_ft->field_name_to_index;
442 field_quark = g_quark_from_string(name);
443 if (!g_hash_table_lookup_extended(field_name_to_index,
444 GUINT_TO_POINTER(field_quark),
445 NULL, (gpointer *) &index)) {
446 BT_LOGV("Invalid parameter: no such field in structure field's type: "
447 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
448 field, field->type, name);
449 goto error;
450 }
451
452 ret = structure->fields->pdata[index];
453 BT_ASSERT(ret);
454
455 error:
456 return ret;
457 }
458
459 static inline
460 struct bt_field_common *bt_field_common_structure_borrow_field_by_index(
461 struct bt_field_common *field, uint64_t index)
462 {
463 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
464
465 BT_ASSERT_PRE_NON_NULL(field, "Structure field");
466 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
467 BT_FIELD_TYPE_ID_STRUCT, "Field");
468 BT_ASSERT_PRE(index < structure->fields->len,
469 "Index is out of bound: %![struct-field-]+_f, "
470 "index=%" PRIu64 ", count=%u", field, index,
471 structure->fields->len);
472 return structure->fields->pdata[index];
473 }
474
475 static inline
476 struct bt_field_common *bt_field_common_array_borrow_field(
477 struct bt_field_common *field, uint64_t index)
478 {
479 struct bt_field_common_array *array = BT_FROM_COMMON(field);
480
481 BT_ASSERT_PRE_NON_NULL(field, "Array field");
482 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_ARRAY,
483 "Field");
484 BT_ASSERT_PRE(index < array->elements->len,
485 "Index is out of bound: %![array-field-]+_f, "
486 "index=%" PRIu64 ", count=%u", field,
487 index, array->elements->len);
488 return array->elements->pdata[(size_t) index];
489 }
490
491 static inline
492 struct bt_field_common *bt_field_common_sequence_borrow_field(
493 struct bt_field_common *field, uint64_t index)
494 {
495 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
496
497 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
498 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_SEQUENCE,
499 "Field");
500 BT_ASSERT_PRE(index < sequence->length,
501 "Index is out of bound: %![seq-field-]+_f, "
502 "index=%" PRIu64 ", count=%u", field, index,
503 sequence->elements->len);
504 return sequence->elements->pdata[(size_t) index];
505 }
506
507 static inline
508 int bt_field_variant_common_set_tag(struct bt_field_common *variant_field,
509 uint64_t tag_uval, bool is_signed)
510 {
511 int ret = 0;
512 int64_t choice_index;
513 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
514
515 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
516 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field,
517 BT_FIELD_TYPE_ID_VARIANT, "Field");
518
519 /* Find matching index in variant field's type */
520 choice_index = bt_field_type_common_variant_find_choice_index(
521 variant_field->type, tag_uval, is_signed);
522 if (choice_index < 0) {
523 ret = -1;
524 goto end;
525 }
526
527 /* Select corresponding field */
528 BT_ASSERT(choice_index < variant->fields->len);
529 variant->current_field = variant->fields->pdata[choice_index];
530 variant->tag_value.u = tag_uval;
531
532 end:
533 return ret;
534 }
535
536 static inline
537 struct bt_field_common *bt_field_common_variant_borrow_current_field(
538 struct bt_field_common *variant_field)
539 {
540 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
541
542 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
543 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field,
544 BT_FIELD_TYPE_ID_VARIANT, "Field");
545 BT_ASSERT_PRE(variant->current_field,
546 "Variant field has no current field: %!+_f", variant_field);
547 return variant->current_field;
548 }
549
550 static inline
551 int bt_field_common_variant_get_tag_signed(struct bt_field_common *variant_field,
552 int64_t *tag)
553 {
554 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
555
556 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
557 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field,
558 BT_FIELD_TYPE_ID_VARIANT, "Field");
559 BT_ASSERT_PRE(variant->current_field,
560 "Variant field has no current field: %!+_f", variant_field);
561 *tag = variant->tag_value.i;
562 return 0;
563 }
564
565 static inline
566 int bt_field_common_variant_get_tag_unsigned(struct bt_field_common *variant_field,
567 uint64_t *tag)
568 {
569 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
570
571 BT_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
572 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field,
573 BT_FIELD_TYPE_ID_VARIANT, "Field");
574 BT_ASSERT_PRE(variant->current_field,
575 "Variant field has no current field: %!+_f", variant_field);
576 *tag = variant->tag_value.u;
577 return 0;
578 }
579
580 static inline
581 struct bt_field_type_enumeration_mapping_iterator *
582 bt_field_common_enumeration_get_mappings(struct bt_field_common *field,
583 bt_field_common_create_func field_create_func,
584 uint64_t uval)
585 {
586 struct bt_field_type_common_enumeration *enum_type = NULL;
587 struct bt_field_type_common_integer *integer_type = NULL;
588 struct bt_field_type_enumeration_mapping_iterator *iter = NULL;
589
590 BT_ASSERT(field);
591 BT_ASSERT(field->type->id == BT_FIELD_TYPE_ID_ENUM);
592 BT_ASSERT(field->payload_set);
593 enum_type = BT_FROM_COMMON(field->type);
594 integer_type = enum_type->container_ft;
595
596 if (!integer_type->is_signed) {
597 iter = bt_field_type_common_enumeration_unsigned_find_mappings_by_value(
598 field->type, uval);
599 } else {
600 iter = bt_field_type_common_enumeration_signed_find_mappings_by_value(
601 field->type, (int64_t) uval);
602 }
603
604 return iter;
605 }
606
607 static inline
608 int bt_field_common_floating_point_get_value(struct bt_field_common *field,
609 double *value)
610 {
611 struct bt_field_common_floating_point *floating_point =
612 BT_FROM_COMMON(field);
613
614 BT_ASSERT_PRE_NON_NULL(field, "Floating point number field");
615 BT_ASSERT_PRE_NON_NULL(value, "Value");
616 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Floating point number field");
617 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
618 BT_FIELD_TYPE_ID_FLOAT, "Field");
619 *value = floating_point->payload;
620 return 0;
621 }
622
623 static inline
624 int bt_field_common_floating_point_set_value(struct bt_field_common *field,
625 double value)
626 {
627 struct bt_field_common_floating_point *floating_point =
628 BT_FROM_COMMON(field);
629
630 BT_ASSERT_PRE_NON_NULL(field, "Floating point number field");
631 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "Floating point number field");
632 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
633 BT_FIELD_TYPE_ID_FLOAT, "Field");
634 floating_point->payload = value;
635 bt_field_common_set(field, true);
636 return 0;
637 }
638
639 static inline
640 const char *bt_field_common_string_get_value(struct bt_field_common *field)
641 {
642 struct bt_field_common_string *string = BT_FROM_COMMON(field);
643
644 BT_ASSERT_PRE_NON_NULL(field, "String field");
645 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "String field");
646 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
647 BT_FIELD_TYPE_ID_STRING, "Field");
648 return string->payload->str;
649 }
650
651 static inline
652 int bt_field_common_string_set_value(struct bt_field_common *field,
653 const char *value)
654 {
655 struct bt_field_common_string *string = BT_FROM_COMMON(field);
656
657 BT_ASSERT_PRE_NON_NULL(field, "String field");
658 BT_ASSERT_PRE_NON_NULL(value, "Value");
659 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "String field");
660 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
661 BT_FIELD_TYPE_ID_STRING, "Field");
662
663 if (string->payload) {
664 g_string_assign(string->payload, value);
665 } else {
666 string->payload = g_string_new(value);
667 }
668
669 bt_field_common_set(field, true);
670 return 0;
671 }
672
673 static inline
674 int bt_field_common_string_append(struct bt_field_common *field,
675 const char *value)
676 {
677 struct bt_field_common_string *string_field = BT_FROM_COMMON(field);
678
679 BT_ASSERT_PRE_NON_NULL(field, "String field");
680 BT_ASSERT_PRE_NON_NULL(value, "Value");
681 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "String field");
682 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
683 BT_FIELD_TYPE_ID_STRING, "Field");
684
685 if (string_field->payload) {
686 g_string_append(string_field->payload, value);
687 } else {
688 string_field->payload = g_string_new(value);
689 }
690
691 bt_field_common_set(field, true);
692 return 0;
693 }
694
695 static inline
696 int bt_field_common_string_append_len(struct bt_field_common *field,
697 const char *value, unsigned int length)
698 {
699 struct bt_field_common_string *string_field = BT_FROM_COMMON(field);
700
701 BT_ASSERT_PRE_NON_NULL(field, "String field");
702 BT_ASSERT_PRE_NON_NULL(value, "Value");
703 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "String field");
704 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
705 BT_FIELD_TYPE_ID_STRING, "Field");
706
707 /* make sure no null bytes are appended */
708 BT_ASSERT_PRE(memchr(value, '\0', length) == NULL,
709 "String value to append contains a null character: "
710 "partial-value=\"%.32s\", length=%u", value, length);
711
712 if (string_field->payload) {
713 g_string_append_len(string_field->payload, value, length);
714 } else {
715 string_field->payload = g_string_new_len(value, length);
716 }
717
718 bt_field_common_set(field, true);
719 return 0;
720 }
721
722 static inline
723 int bt_field_common_string_clear(struct bt_field_common *field)
724 {
725 struct bt_field_common_string *string_field = BT_FROM_COMMON(field);
726
727 BT_ASSERT_PRE_NON_NULL(field, "String field");
728 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "String field");
729 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field,
730 BT_FIELD_TYPE_ID_STRING, "Field");
731
732 if (string_field->payload) {
733 g_string_set_size(string_field->payload, 0);
734 } else {
735 string_field->payload = g_string_new(NULL);
736 }
737
738 bt_field_common_set(field, true);
739 return 0;
740 }
741
742 static inline
743 int _bt_field_validate_recursive(struct bt_field *field)
744 {
745 return _bt_field_common_validate_recursive((void *) field);
746 }
747
748 static inline
749 void _bt_field_set_is_frozen_recursive(struct bt_field *field, bool is_frozen)
750 {
751 return _bt_field_common_set_is_frozen_recursive((void *) field,
752 is_frozen);
753 }
754
755 static inline
756 bt_bool _bt_field_is_set_recursive(struct bt_field *field)
757 {
758 return _bt_field_common_is_set_recursive((void *) field);
759 }
760
761 static inline
762 void _bt_field_reset_recursive(struct bt_field *field)
763 {
764 _bt_field_common_reset_recursive((void *) field);
765 }
766
767 static inline
768 void _bt_field_set(struct bt_field *field, bool value)
769 {
770 _bt_field_common_set((void *) field, value);
771 }
772
773 static inline
774 void bt_field_common_finalize(struct bt_field_common *field)
775 {
776 BT_ASSERT(field);
777 BT_LOGD_STR("Putting field's type.");
778 bt_put(field->type);
779 }
780
781 static inline
782 void bt_field_common_integer_finalize(struct bt_field_common *field)
783 {
784 BT_ASSERT(field);
785 BT_LOGD("Finalizing common integer field object: addr=%p", field);
786 bt_field_common_finalize(field);
787 }
788
789 static inline
790 void bt_field_common_floating_point_finalize(struct bt_field_common *field)
791 {
792 BT_ASSERT(field);
793 BT_LOGD("Finalizing common floating point number field object: addr=%p", field);
794 bt_field_common_finalize(field);
795 }
796
797 static inline
798 void bt_field_common_structure_finalize_recursive(struct bt_field_common *field)
799 {
800 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
801
802 BT_ASSERT(field);
803 BT_LOGD("Finalizing common structure field object: addr=%p", field);
804 bt_field_common_finalize(field);
805
806 if (structure->fields) {
807 g_ptr_array_free(structure->fields, TRUE);
808 }
809 }
810
811 static inline
812 void bt_field_common_variant_finalize_recursive(struct bt_field_common *field)
813 {
814 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
815
816 BT_ASSERT(field);
817 BT_LOGD("Finalizing common variant field object: addr=%p", field);
818 bt_field_common_finalize(field);
819
820 if (variant->fields) {
821 g_ptr_array_free(variant->fields, TRUE);
822 }
823 }
824
825 static inline
826 void bt_field_common_array_finalize_recursive(struct bt_field_common *field)
827 {
828 struct bt_field_common_array *array = BT_FROM_COMMON(field);
829
830 BT_ASSERT(field);
831 BT_LOGD("Finalizing common array field object: addr=%p", field);
832 bt_field_common_finalize(field);
833
834 if (array->elements) {
835 g_ptr_array_free(array->elements, TRUE);
836 }
837 }
838
839 static inline
840 void bt_field_common_sequence_finalize_recursive(struct bt_field_common *field)
841 {
842 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
843
844 BT_ASSERT(field);
845 BT_LOGD("Finalizing common sequence field object: addr=%p", field);
846 bt_field_common_finalize(field);
847
848 if (sequence->elements) {
849 g_ptr_array_free(sequence->elements, TRUE);
850 }
851 }
852
853 static inline
854 void bt_field_common_string_finalize(struct bt_field_common *field)
855 {
856 struct bt_field_common_string *string = BT_FROM_COMMON(field);
857
858 BT_ASSERT(field);
859 BT_LOGD("Finalizing common string field object: addr=%p", field);
860 bt_field_common_finalize(field);
861
862 if (string->payload) {
863 g_string_free(string->payload, TRUE);
864 }
865 }
866
867 BT_ASSERT_PRE_FUNC
868 static inline bool value_is_in_range_signed(unsigned int size, int64_t value)
869 {
870 bool ret = true;
871 int64_t min_value, max_value;
872
873 min_value = -(1ULL << (size - 1));
874 max_value = (1ULL << (size - 1)) - 1;
875 if (value < min_value || value > max_value) {
876 BT_LOGF("Value is out of bounds: value=%" PRId64 ", "
877 "min-value=%" PRId64 ", max-value=%" PRId64,
878 value, min_value, max_value);
879 ret = false;
880 }
881
882 return ret;
883 }
884
885 BT_ASSERT_PRE_FUNC
886 static inline bool value_is_in_range_unsigned(unsigned int size, uint64_t value)
887 {
888 bool ret = true;
889 int64_t max_value;
890
891 max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1;
892 if (value > max_value) {
893 BT_LOGF("Value is out of bounds: value=%" PRIu64 ", "
894 "max-value=%" PRIu64,
895 value, max_value);
896 ret = false;
897 }
898
899 return ret;
900 }
901
902 BT_HIDDEN
903 struct bt_field *bt_field_create_recursive(struct bt_field_type *type);
904
905 BT_HIDDEN
906 void bt_field_destroy_recursive(struct bt_field *field);
907
908 #endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.04818 seconds and 5 git commands to generate.