lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / include / babeltrace / ctf-ir / fields-internal.h
CommitLineData
2e33ac5a
PP
1#ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
2#define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
273b65be
JG
3
4/*
2e33ac5a 5 * Babeltrace - CTF IR: Event Fields internal
273b65be 6 *
de9dd397 7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
3dca2276
PP
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>
83509119 34#include <babeltrace/object-internal.h>
273b65be 35#include <babeltrace/babeltrace-internal.h>
c55a9f58 36#include <babeltrace/types.h>
dc3fffef 37#include <stdint.h>
3dca2276 38#include <inttypes.h>
d990a4fb 39#include <stdbool.h>
273b65be
JG
40#include <glib.h>
41
3dca2276 42#define BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
312c056a 43 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
3dca2276 44 _name " has the wrong type ID: expected-type-id=%s, " \
312c056a
PP
45 "%![field-]+_f", \
46 bt_common_field_type_id_string((int) (_type_id)), (_field))
dc3fffef 47
3dca2276
PP
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
55struct bt_field;
56struct bt_field_common;
57
312c056a
PP
58typedef void (*bt_field_common_method_set_is_frozen)(struct bt_field_common *,
59 bool);
3dca2276
PP
60typedef int (*bt_field_common_method_validate)(struct bt_field_common *);
61typedef struct bt_field_common *(*bt_field_common_method_copy)(
62 struct bt_field_common *);
63typedef bt_bool (*bt_field_common_method_is_set)(struct bt_field_common *);
64typedef void (*bt_field_common_method_reset)(struct bt_field_common *);
65
66struct bt_field_common_methods {
312c056a 67 bt_field_common_method_set_is_frozen set_is_frozen;
3dca2276
PP
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
74struct bt_field_common {
83509119 75 struct bt_object base;
3dca2276
PP
76 struct bt_field_type_common *type;
77 struct bt_field_common_methods *methods;
d990a4fb
JG
78 bool payload_set;
79 bool frozen;
3dca2276
PP
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;
273b65be
JG
92};
93
3dca2276
PP
94struct bt_field_common_integer {
95 struct bt_field_common common;
dc3fffef
PP
96 union {
97 int64_t signd;
98 uint64_t unsignd;
99 } payload;
273b65be
JG
100};
101
3dca2276
PP
102struct bt_field_common_floating_point {
103 struct bt_field_common common;
dc3fffef 104 double payload;
273b65be
JG
105};
106
3dca2276
PP
107struct bt_field_common_structure {
108 struct bt_field_common common;
312c056a
PP
109
110 /* Array of `struct bt_field_common *`, owned by this */
111 GPtrArray *fields;
273b65be
JG
112};
113
3dca2276
PP
114struct bt_field_common_variant {
115 struct bt_field_common common;
312c056a
PP
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;
273b65be
JG
127};
128
3dca2276
PP
129struct bt_field_common_array {
130 struct bt_field_common common;
312c056a
PP
131
132 /* Array of `struct bt_field_common *`, owned by this */
133 GPtrArray *elements;
273b65be
JG
134};
135
3dca2276
PP
136struct bt_field_common_sequence {
137 struct bt_field_common common;
312c056a
PP
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;
273b65be
JG
148};
149
3dca2276
PP
150struct bt_field_common_string {
151 struct bt_field_common common;
273b65be
JG
152 GString *payload;
153};
154
312c056a
PP
155struct bt_field_enumeration {
156 struct bt_field_common_integer common;
157};
3dca2276
PP
158
159BT_HIDDEN
160struct bt_field_common *bt_field_common_copy(struct bt_field_common *field);
161
162BT_HIDDEN
163int 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,
312c056a
PP
167 bt_field_common_create_func field_create_func,
168 GDestroyNotify field_release_func);
3dca2276
PP
169
170BT_HIDDEN
171int bt_field_common_array_initialize(struct bt_field_common *field,
172 struct bt_field_type_common *type,
173 bt_object_release_func release_func,
312c056a
PP
174 struct bt_field_common_methods *methods,
175 bt_field_common_create_func field_create_func,
176 GDestroyNotify field_destroy_func);
3dca2276
PP
177
178BT_HIDDEN
312c056a
PP
179int 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);
3dca2276
PP
184
185BT_HIDDEN
312c056a
PP
186int 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
193BT_HIDDEN
194int bt_field_common_generic_validate(struct bt_field_common *field);
3dca2276
PP
195
196BT_HIDDEN
197int bt_field_common_structure_validate_recursive(struct bt_field_common *field);
198
199BT_HIDDEN
200int bt_field_common_variant_validate_recursive(struct bt_field_common *field);
201
202BT_HIDDEN
203int bt_field_common_array_validate_recursive(struct bt_field_common *field);
204
205BT_HIDDEN
206int bt_field_common_sequence_validate_recursive(struct bt_field_common *field);
207
208BT_HIDDEN
209void bt_field_common_generic_reset(struct bt_field_common *field);
210
3dca2276
PP
211BT_HIDDEN
212void bt_field_common_structure_reset_recursive(struct bt_field_common *field);
213
214BT_HIDDEN
215void bt_field_common_variant_reset_recursive(struct bt_field_common *field);
216
217BT_HIDDEN
218void bt_field_common_array_reset_recursive(struct bt_field_common *field);
219
220BT_HIDDEN
221void bt_field_common_sequence_reset_recursive(struct bt_field_common *field);
222
223BT_HIDDEN
224void bt_field_common_string_reset(struct bt_field_common *field);
225
226BT_HIDDEN
312c056a
PP
227void bt_field_common_generic_set_is_frozen(struct bt_field_common *field,
228 bool is_frozen);
f6ccaed9 229
273b65be 230BT_HIDDEN
312c056a
PP
231void bt_field_common_structure_set_is_frozen_recursive(
232 struct bt_field_common *field, bool is_frozen);
273b65be
JG
233
234BT_HIDDEN
312c056a
PP
235void bt_field_common_variant_set_is_frozen_recursive(
236 struct bt_field_common *field, bool is_frozen);
f6ccaed9
PP
237
238BT_HIDDEN
312c056a
PP
239void bt_field_common_array_set_is_frozen_recursive(
240 struct bt_field_common *field, bool is_frozen);
f6ccaed9
PP
241
242BT_HIDDEN
312c056a
PP
243void bt_field_common_sequence_set_is_frozen_recursive(
244 struct bt_field_common *field, bool is_frozen);
273b65be 245
918be005 246BT_HIDDEN
312c056a
PP
247void _bt_field_common_set_is_frozen_recursive(struct bt_field_common *field,
248 bool is_frozen);
3dca2276
PP
249
250BT_HIDDEN
251bt_bool bt_field_common_generic_is_set(struct bt_field_common *field);
252
3dca2276
PP
253BT_HIDDEN
254bt_bool bt_field_common_structure_is_set_recursive(
255 struct bt_field_common *field);
256
257BT_HIDDEN
258bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field);
259
260BT_HIDDEN
261bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field);
262
263BT_HIDDEN
264bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field);
265
f6ccaed9 266#ifdef BT_DEV_MODE
312c056a
PP
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
f6ccaed9 277#else
3dca2276 278# define bt_field_common_validate_recursive(_field) (-1)
312c056a 279# define bt_field_common_set_is_frozen_recursive(_field, _is_frozen)
3dca2276
PP
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)
312c056a 284# define bt_field_set_is_frozen_recursive(_field, _is_frozen)
3dca2276 285# define bt_field_is_set_recursive(_field) (BT_FALSE)
f6ccaed9
PP
286# define bt_field_reset_recursive(_field) (BT_TRUE)
287# define bt_field_set(_field, _val)
288#endif
918be005 289
3dca2276
PP
290BT_ASSERT_FUNC
291static inline bool field_type_common_has_known_id(
292 struct bt_field_type_common *ft)
293{
312c056a
PP
294 return (int) ft->id > BT_FIELD_TYPE_ID_UNKNOWN ||
295 (int) ft->id < BT_FIELD_TYPE_ID_NR;
3dca2276
PP
296}
297
298static inline
299int _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
315end:
316 return ret;
317}
318
319static inline
320void _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
327static inline
328void _bt_field_common_set(struct bt_field_common *field, bool value)
329{
330 BT_ASSERT(field);
331 field->payload_set = value;
332}
333
334static inline
335bt_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
347end:
348 return is_set;
349}
350
351static inline
352void 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
364static inline
094ff7c0 365struct bt_field_type_common *bt_field_common_borrow_type(
3dca2276
PP
366 struct bt_field_common *field)
367{
368 struct bt_field_type_common *ret = NULL;
369
370 BT_ASSERT_PRE_NON_NULL(field, "Field");
094ff7c0 371 ret = field->type;
3dca2276
PP
372 return ret;
373}
374
375static inline
312c056a 376int64_t bt_field_common_sequence_get_length(struct bt_field_common *field)
3dca2276
PP
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");
312c056a 383 return (int64_t) sequence->length;
3dca2276
PP
384}
385
386static inline
387int bt_field_common_sequence_set_length(struct bt_field_common *field,
312c056a 388 uint64_t length, bt_field_common_create_func field_create_func)
3dca2276
PP
389{
390 int ret = 0;
3dca2276 391 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
3dca2276
PP
392
393 BT_ASSERT_PRE_NON_NULL(field, "Sequence field");
3dca2276 394 BT_ASSERT_PRE_FIELD_COMMON_HOT(field, "Sequence field");
3dca2276 395
312c056a
PP
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 }
3dca2276
PP
417 }
418
312c056a 419 sequence->length = length;
3dca2276
PP
420
421end:
422 return ret;
423}
424
425static inline
094ff7c0 426struct bt_field_common *bt_field_common_structure_borrow_field_by_name(
3dca2276
PP
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
094ff7c0 452 ret = structure->fields->pdata[index];
3dca2276
PP
453 BT_ASSERT(ret);
454
455error:
456 return ret;
457}
458
459static inline
094ff7c0 460struct bt_field_common *bt_field_common_structure_borrow_field_by_index(
3dca2276
PP
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);
094ff7c0 472 return structure->fields->pdata[index];
3dca2276
PP
473}
474
3dca2276 475static inline
094ff7c0 476struct bt_field_common *bt_field_common_array_borrow_field(
312c056a 477 struct bt_field_common *field, uint64_t index)
3dca2276 478{
3dca2276
PP
479 struct bt_field_common_array *array = BT_FROM_COMMON(field);
480
481 BT_ASSERT_PRE_NON_NULL(field, "Array field");
312c056a
PP
482 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field, BT_FIELD_TYPE_ID_ARRAY,
483 "Field");
3dca2276
PP
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);
312c056a 488 return array->elements->pdata[(size_t) index];
3dca2276
PP
489}
490
491static inline
094ff7c0 492struct bt_field_common *bt_field_common_sequence_borrow_field(
312c056a 493 struct bt_field_common *field, uint64_t index)
3dca2276 494{
3dca2276
PP
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");
312c056a 500 BT_ASSERT_PRE(index < sequence->length,
3dca2276
PP
501 "Index is out of bound: %![seq-field-]+_f, "
502 "index=%" PRIu64 ", count=%u", field, index,
503 sequence->elements->len);
312c056a 504 return sequence->elements->pdata[(size_t) index];
3dca2276
PP
505}
506
507static inline
312c056a
PP
508int bt_field_variant_common_set_tag(struct bt_field_common *variant_field,
509 uint64_t tag_uval, bool is_signed)
3dca2276 510{
312c056a
PP
511 int ret = 0;
512 int64_t choice_index;
513 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
3dca2276 514
312c056a
PP
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");
3dca2276 518
312c056a
PP
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;
3dca2276
PP
524 goto end;
525 }
526
312c056a
PP
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;
3dca2276
PP
531
532end:
312c056a 533 return ret;
3dca2276
PP
534}
535
536static inline
094ff7c0 537struct bt_field_common *bt_field_common_variant_borrow_current_field(
3dca2276
PP
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");
312c056a
PP
545 BT_ASSERT_PRE(variant->current_field,
546 "Variant field has no current field: %!+_f", variant_field);
547 return variant->current_field;
3dca2276
PP
548}
549
550static inline
312c056a
PP
551int bt_field_common_variant_get_tag_signed(struct bt_field_common *variant_field,
552 int64_t *tag)
3dca2276
PP
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");
312c056a
PP
559 BT_ASSERT_PRE(variant->current_field,
560 "Variant field has no current field: %!+_f", variant_field);
561 *tag = variant->tag_value.i;
3dca2276
PP
562 return 0;
563}
564
3dca2276 565static inline
312c056a
PP
566int bt_field_common_variant_get_tag_unsigned(struct bt_field_common *variant_field,
567 uint64_t *tag)
3dca2276 568{
312c056a 569 struct bt_field_common_variant *variant = BT_FROM_COMMON(variant_field);
3dca2276 570
312c056a
PP
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;
3dca2276
PP
577 return 0;
578}
579
580static inline
581struct bt_field_type_enumeration_mapping_iterator *
582bt_field_common_enumeration_get_mappings(struct bt_field_common *field,
312c056a
PP
583 bt_field_common_create_func field_create_func,
584 uint64_t uval)
3dca2276 585{
312c056a 586 struct bt_field_type_common_enumeration *enum_type = NULL;
3dca2276
PP
587 struct bt_field_type_common_integer *integer_type = NULL;
588 struct bt_field_type_enumeration_mapping_iterator *iter = NULL;
589
312c056a
PP
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;
3dca2276
PP
595
596 if (!integer_type->is_signed) {
3dca2276 597 iter = bt_field_type_common_enumeration_unsigned_find_mappings_by_value(
312c056a 598 field->type, uval);
3dca2276 599 } else {
3dca2276 600 iter = bt_field_type_common_enumeration_signed_find_mappings_by_value(
312c056a 601 field->type, (int64_t) uval);
3dca2276
PP
602 }
603
3dca2276
PP
604 return iter;
605}
606
607static inline
608int 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
623static inline
624int 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
639static inline
640const 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
651static inline
652int 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
673static inline
674int 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
695static inline
696int 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
312c056a
PP
722static inline
723int 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
3dca2276
PP
742static inline
743int _bt_field_validate_recursive(struct bt_field *field)
744{
745 return _bt_field_common_validate_recursive((void *) field);
746}
747
748static inline
312c056a 749void _bt_field_set_is_frozen_recursive(struct bt_field *field, bool is_frozen)
3dca2276 750{
312c056a
PP
751 return _bt_field_common_set_is_frozen_recursive((void *) field,
752 is_frozen);
3dca2276
PP
753}
754
755static inline
756bt_bool _bt_field_is_set_recursive(struct bt_field *field)
757{
758 return _bt_field_common_is_set_recursive((void *) field);
759}
760
761static inline
762void _bt_field_reset_recursive(struct bt_field *field)
763{
764 _bt_field_common_reset_recursive((void *) field);
765}
766
767static inline
768void _bt_field_set(struct bt_field *field, bool value)
769{
770 _bt_field_common_set((void *) field, value);
771}
2e8876d3 772
312c056a
PP
773static inline
774void 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
781static inline
782void 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
789static inline
790void 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
797static inline
798void 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
811static inline
812void 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
825static inline
826void 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
839static inline
840void 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
853static inline
854void 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
867BT_ASSERT_PRE_FUNC
868static 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
885BT_ASSERT_PRE_FUNC
886static 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
902BT_HIDDEN
903struct bt_field *bt_field_create_recursive(struct bt_field_type *type);
904
905BT_HIDDEN
906void bt_field_destroy_recursive(struct bt_field *field);
907
2e33ac5a 908#endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.082218 seconds and 4 git commands to generate.