bt2: add integer range set support
[babeltrace.git] / src / lib / trace-ir / field.c
... / ...
CommitLineData
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#define BT_LOG_TAG "LIB/FIELD"
25#include "lib/logging.h"
26
27#include "lib/assert-pre.h"
28#include <babeltrace2/trace-ir/field.h>
29#include <babeltrace2/trace-ir/field-const.h>
30#include "lib/object.h"
31#include "compat/compiler.h"
32#include "compat/fcntl.h"
33#include "common/align.h"
34#include "common/assert.h"
35#include <inttypes.h>
36
37#include "field.h"
38#include "field-class.h"
39#include "lib/func-status.h"
40
41static
42void reset_single_field(struct bt_field *field);
43
44static
45void reset_array_field(struct bt_field *field);
46
47static
48void reset_structure_field(struct bt_field *field);
49
50static
51void reset_variant_field(struct bt_field *field);
52
53static
54void set_single_field_is_frozen(struct bt_field *field, bool is_frozen);
55
56static
57void set_array_field_is_frozen(struct bt_field *field, bool is_frozen);
58
59static
60void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen);
61
62static
63void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen);
64
65static
66bool single_field_is_set(const struct bt_field *field);
67
68static
69bool array_field_is_set(const struct bt_field *field);
70
71static
72bool structure_field_is_set(const struct bt_field *field);
73
74static
75bool variant_field_is_set(const struct bt_field *field);
76
77static
78struct bt_field_methods integer_field_methods = {
79 .set_is_frozen = set_single_field_is_frozen,
80 .is_set = single_field_is_set,
81 .reset = reset_single_field,
82};
83
84static
85struct bt_field_methods real_field_methods = {
86 .set_is_frozen = set_single_field_is_frozen,
87 .is_set = single_field_is_set,
88 .reset = reset_single_field,
89};
90
91static
92struct bt_field_methods string_field_methods = {
93 .set_is_frozen = set_single_field_is_frozen,
94 .is_set = single_field_is_set,
95 .reset = reset_single_field,
96};
97
98static
99struct bt_field_methods structure_field_methods = {
100 .set_is_frozen = set_structure_field_is_frozen,
101 .is_set = structure_field_is_set,
102 .reset = reset_structure_field,
103};
104
105static
106struct bt_field_methods array_field_methods = {
107 .set_is_frozen = set_array_field_is_frozen,
108 .is_set = array_field_is_set,
109 .reset = reset_array_field,
110};
111
112static
113struct bt_field_methods variant_field_methods = {
114 .set_is_frozen = set_variant_field_is_frozen,
115 .is_set = variant_field_is_set,
116 .reset = reset_variant_field,
117};
118
119static
120struct bt_field *create_integer_field(struct bt_field_class *);
121
122static
123struct bt_field *create_real_field(struct bt_field_class *);
124
125static
126struct bt_field *create_string_field(struct bt_field_class *);
127
128static
129struct bt_field *create_structure_field(struct bt_field_class *);
130
131static
132struct bt_field *create_static_array_field(struct bt_field_class *);
133
134static
135struct bt_field *create_dynamic_array_field(struct bt_field_class *);
136
137static
138struct bt_field *create_variant_field(struct bt_field_class *);
139
140static
141struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = {
142 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = create_integer_field,
143 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = create_integer_field,
144 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = create_integer_field,
145 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = create_integer_field,
146 [BT_FIELD_CLASS_TYPE_REAL] = create_real_field,
147 [BT_FIELD_CLASS_TYPE_STRING] = create_string_field,
148 [BT_FIELD_CLASS_TYPE_STRUCTURE] = create_structure_field,
149 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = create_static_array_field,
150 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = create_dynamic_array_field,
151 [BT_FIELD_CLASS_TYPE_VARIANT] = create_variant_field,
152};
153
154static
155void destroy_integer_field(struct bt_field *field);
156
157static
158void destroy_real_field(struct bt_field *field);
159
160static
161void destroy_string_field(struct bt_field *field);
162
163static
164void destroy_structure_field(struct bt_field *field);
165
166static
167void destroy_array_field(struct bt_field *field);
168
169static
170void destroy_variant_field(struct bt_field *field);
171
172static
173void (* const field_destroy_funcs[])(struct bt_field *) = {
174 [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = destroy_integer_field,
175 [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = destroy_integer_field,
176 [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = destroy_integer_field,
177 [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = destroy_integer_field,
178 [BT_FIELD_CLASS_TYPE_REAL] = destroy_real_field,
179 [BT_FIELD_CLASS_TYPE_STRING] = destroy_string_field,
180 [BT_FIELD_CLASS_TYPE_STRUCTURE] = destroy_structure_field,
181 [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = destroy_array_field,
182 [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = destroy_array_field,
183 [BT_FIELD_CLASS_TYPE_VARIANT] = destroy_variant_field,
184};
185
186struct bt_field_class *bt_field_borrow_class(const struct bt_field *field)
187{
188 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
189 return field->class;
190}
191
192const struct bt_field_class *bt_field_borrow_class_const(
193 const struct bt_field *field)
194{
195 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
196 return field->class;
197}
198
199enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field)
200{
201 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
202 return field->class->type;
203}
204
205BT_HIDDEN
206struct bt_field *bt_field_create(struct bt_field_class *fc)
207{
208 struct bt_field *field = NULL;
209
210 BT_ASSERT(fc);
211 BT_ASSERT(bt_field_class_has_known_type(fc));
212 field = field_create_funcs[fc->type](fc);
213 if (!field) {
214 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
215 "%![fc-]+F", fc);
216 goto end;
217 }
218
219end:
220 return field;
221}
222
223static inline
224void init_field(struct bt_field *field, struct bt_field_class *fc,
225 struct bt_field_methods *methods)
226{
227 BT_ASSERT(field);
228 BT_ASSERT(fc);
229 bt_object_init_unique(&field->base);
230 field->methods = methods;
231 field->class = fc;
232 bt_object_get_no_null_check(fc);
233}
234
235static
236struct bt_field *create_integer_field(struct bt_field_class *fc)
237{
238 struct bt_field_integer *int_field;
239
240 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc);
241 int_field = g_new0(struct bt_field_integer, 1);
242 if (!int_field) {
243 BT_LIB_LOGE_APPEND_CAUSE(
244 "Failed to allocate one integer field.");
245 goto end;
246 }
247
248 init_field((void *) int_field, fc, &integer_field_methods);
249 BT_LIB_LOGD("Created integer field object: %!+f", int_field);
250
251end:
252 return (void *) int_field;
253}
254
255static
256struct bt_field *create_real_field(struct bt_field_class *fc)
257{
258 struct bt_field_real *real_field;
259
260 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc);
261 real_field = g_new0(struct bt_field_real, 1);
262 if (!real_field) {
263 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
264 goto end;
265 }
266
267 init_field((void *) real_field, fc, &real_field_methods);
268 BT_LIB_LOGD("Created real field object: %!+f", real_field);
269
270end:
271 return (void *) real_field;
272}
273
274static
275struct bt_field *create_string_field(struct bt_field_class *fc)
276{
277 struct bt_field_string *string_field;
278
279 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc);
280 string_field = g_new0(struct bt_field_string, 1);
281 if (!string_field) {
282 BT_LIB_LOGE_APPEND_CAUSE(
283 "Failed to allocate one string field.");
284 goto end;
285 }
286
287 init_field((void *) string_field, fc, &string_field_methods);
288 string_field->buf = g_array_sized_new(FALSE, FALSE,
289 sizeof(char), 1);
290 if (!string_field->buf) {
291 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
292 BT_OBJECT_PUT_REF_AND_RESET(string_field);
293 goto end;
294 }
295
296 g_array_index(string_field->buf, char, 0) = '\0';
297 BT_LIB_LOGD("Created string field object: %!+f", string_field);
298
299end:
300 return (void *) string_field;
301}
302
303static inline
304int create_fields_from_named_field_classes(
305 struct bt_field_class_named_field_class_container *fc,
306 GPtrArray **fields)
307{
308 int ret = 0;
309 uint64_t i;
310
311 *fields = g_ptr_array_new_with_free_func(
312 (GDestroyNotify) bt_field_destroy);
313 if (!*fields) {
314 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
315 ret = -1;
316 goto end;
317 }
318
319 g_ptr_array_set_size(*fields, fc->named_fcs->len);
320
321 for (i = 0; i < fc->named_fcs->len; i++) {
322 struct bt_field *field;
323 struct bt_named_field_class *named_fc =
324 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, i);
325
326 field = bt_field_create(named_fc->fc);
327 if (!field) {
328 BT_LIB_LOGE_APPEND_CAUSE(
329 "Failed to create structure member or variant option field: "
330 "name=\"%s\", %![fc-]+F",
331 named_fc->name->str, named_fc->fc);
332 ret = -1;
333 goto end;
334 }
335
336 g_ptr_array_index(*fields, i) = field;
337 }
338
339end:
340 return ret;
341}
342
343static
344struct bt_field *create_structure_field(struct bt_field_class *fc)
345{
346 struct bt_field_structure *struct_field;
347
348 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc);
349 struct_field = g_new0(struct bt_field_structure, 1);
350 if (!struct_field) {
351 BT_LIB_LOGE_APPEND_CAUSE(
352 "Failed to allocate one structure field.");
353 goto end;
354 }
355
356 init_field((void *) struct_field, fc, &structure_field_methods);
357
358 if (create_fields_from_named_field_classes((void *) fc,
359 &struct_field->fields)) {
360 BT_LIB_LOGE_APPEND_CAUSE(
361 "Cannot create structure member fields: %![fc-]+F", fc);
362 BT_OBJECT_PUT_REF_AND_RESET(struct_field);
363 goto end;
364 }
365
366 BT_LIB_LOGD("Created structure field object: %!+f", struct_field);
367
368end:
369 return (void *) struct_field;
370}
371
372static
373struct bt_field *create_variant_field(struct bt_field_class *fc)
374{
375 struct bt_field_variant *var_field;
376
377 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc);
378 var_field = g_new0(struct bt_field_variant, 1);
379 if (!var_field) {
380 BT_LIB_LOGE_APPEND_CAUSE(
381 "Failed to allocate one variant field.");
382 goto end;
383 }
384
385 init_field((void *) var_field, fc, &variant_field_methods);
386
387 if (create_fields_from_named_field_classes((void *) fc,
388 &var_field->fields)) {
389 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
390 "%![fc-]+F", fc);
391 BT_OBJECT_PUT_REF_AND_RESET(var_field);
392 goto end;
393 }
394
395 BT_LIB_LOGD("Created variant field object: %!+f", var_field);
396
397end:
398 return (void *) var_field;
399}
400
401static inline
402int init_array_field_fields(struct bt_field_array *array_field)
403{
404 int ret = 0;
405 uint64_t i;
406 struct bt_field_class_array *array_fc;
407
408 BT_ASSERT(array_field);
409 array_fc = (void *) array_field->common.class;
410 array_field->fields = g_ptr_array_sized_new(array_field->length);
411 if (!array_field->fields) {
412 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
413 ret = -1;
414 goto end;
415 }
416
417 g_ptr_array_set_free_func(array_field->fields,
418 (GDestroyNotify) bt_field_destroy);
419 g_ptr_array_set_size(array_field->fields, array_field->length);
420
421 for (i = 0; i < array_field->length; i++) {
422 array_field->fields->pdata[i] = bt_field_create(
423 array_fc->element_fc);
424 if (!array_field->fields->pdata[i]) {
425 BT_LIB_LOGE_APPEND_CAUSE(
426 "Cannot create array field's element field: "
427 "index=%" PRIu64 ", %![fc-]+F", i, array_fc);
428 ret = -1;
429 goto end;
430 }
431 }
432
433end:
434 return ret;
435}
436
437static
438struct bt_field *create_static_array_field(struct bt_field_class *fc)
439{
440 struct bt_field_class_static_array *array_fc = (void *) fc;
441 struct bt_field_array *array_field;
442
443 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc);
444 array_field = g_new0(struct bt_field_array, 1);
445 if (!array_field) {
446 BT_LIB_LOGE_APPEND_CAUSE(
447 "Failed to allocate one static array field.");
448 goto end;
449 }
450
451 init_field((void *) array_field, fc, &array_field_methods);
452 array_field->length = array_fc->length;
453
454 if (init_array_field_fields(array_field)) {
455 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
456 "%![fc-]+F", fc);
457 BT_OBJECT_PUT_REF_AND_RESET(array_field);
458 goto end;
459 }
460
461 BT_LIB_LOGD("Created static array field object: %!+f", array_field);
462
463end:
464 return (void *) array_field;
465}
466
467static
468struct bt_field *create_dynamic_array_field(struct bt_field_class *fc)
469{
470 struct bt_field_array *array_field;
471
472 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc);
473 array_field = g_new0(struct bt_field_array, 1);
474 if (!array_field) {
475 BT_LIB_LOGE_APPEND_CAUSE(
476 "Failed to allocate one dynamic array field.");
477 goto end;
478 }
479
480 init_field((void *) array_field, fc, &array_field_methods);
481
482 if (init_array_field_fields(array_field)) {
483 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
484 "%![fc-]+F", fc);
485 BT_OBJECT_PUT_REF_AND_RESET(array_field);
486 goto end;
487 }
488
489 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field);
490
491end:
492 return (void *) array_field;
493}
494
495int64_t bt_field_signed_integer_get_value(const struct bt_field *field)
496{
497 const struct bt_field_integer *int_field = (const void *) field;
498
499 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
500 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
501 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field");
502 return int_field->value.i;
503}
504
505void bt_field_signed_integer_set_value(struct bt_field *field, int64_t value)
506{
507 struct bt_field_integer *int_field = (void *) field;
508
509 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
510 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field");
511 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
512 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed(
513 ((struct bt_field_class_integer *) field->class)->range, value),
514 "Value is out of bounds: value=%" PRId64 ", %![field-]+f, "
515 "%![fc-]+F", value, field, field->class);
516 int_field->value.i = value;
517 bt_field_set_single(field, true);
518}
519
520uint64_t bt_field_unsigned_integer_get_value(const struct bt_field *field)
521{
522 const struct bt_field_integer *int_field = (const void *) field;
523
524 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
525 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
526 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field");
527 return int_field->value.u;
528}
529
530void bt_field_unsigned_integer_set_value(struct bt_field *field, uint64_t value)
531{
532 struct bt_field_integer *int_field = (void *) field;
533
534 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
535 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field");
536 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
537 BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned(
538 ((struct bt_field_class_integer *) field->class)->range, value),
539 "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, "
540 "%![fc-]+F", value, field, field->class);
541 int_field->value.u = value;
542 bt_field_set_single(field, true);
543}
544
545double bt_field_real_get_value(const struct bt_field *field)
546{
547 const struct bt_field_real *real_field = (const void *) field;
548
549 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
550 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
551 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field");
552 return real_field->value;
553}
554
555void bt_field_real_set_value(struct bt_field *field, double value)
556{
557 struct bt_field_real *real_field = (void *) field;
558
559 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
560 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field");
561 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
562 BT_ASSERT_PRE_DEV(
563 !((struct bt_field_class_real *) field->class)->is_single_precision ||
564 (double) (float) value == value,
565 "Invalid value for a single-precision real number: value=%f, "
566 "%![fc-]+F", value, field->class);
567 real_field->value = value;
568 bt_field_set_single(field, true);
569}
570
571enum bt_field_enumeration_get_mapping_labels_status
572bt_field_unsigned_enumeration_get_mapping_labels(
573 const struct bt_field *field,
574 bt_field_class_enumeration_mapping_label_array *label_array,
575 uint64_t *count)
576{
577 const struct bt_field_integer *int_field = (const void *) field;
578
579 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
580 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
581 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)");
582 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
583 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
584 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field");
585 return (int)
586 bt_field_class_unsigned_enumeration_get_mapping_labels_for_value(
587 field->class, int_field->value.u, label_array, count);
588}
589
590enum bt_field_enumeration_get_mapping_labels_status
591bt_field_signed_enumeration_get_mapping_labels(
592 const struct bt_field *field,
593 bt_field_class_enumeration_mapping_label_array *label_array,
594 uint64_t *count)
595{
596 const struct bt_field_integer *int_field = (const void *) field;
597
598 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
599 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
600 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)");
601 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
602 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
603 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field");
604 return (int)
605 bt_field_class_signed_enumeration_get_mapping_labels_for_value(
606 field->class, int_field->value.i, label_array, count);
607}
608
609const char *bt_field_string_get_value(const struct bt_field *field)
610{
611 const struct bt_field_string *string_field = (const void *) field;
612
613 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
614 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
615 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
616 "Field");
617 return (const char *) string_field->buf->data;
618}
619
620uint64_t bt_field_string_get_length(const struct bt_field *field)
621{
622 const struct bt_field_string *string_field = (const void *) field;
623
624 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
625 BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
626 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
627 "Field");
628 return string_field->length;
629}
630
631static inline
632void clear_string_field(struct bt_field *field)
633{
634 struct bt_field_string *string_field = (void *) field;
635
636 BT_ASSERT(field);
637 string_field->length = 0;
638 bt_field_set_single(field, true);
639}
640
641enum bt_field_string_set_value_status bt_field_string_set_value(
642 struct bt_field *field, const char *value)
643{
644 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
645 BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
646 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
647 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
648 "Field");
649 clear_string_field(field);
650 return (int) bt_field_string_append_with_length(field, value,
651 (uint64_t) strlen(value));
652}
653
654enum bt_field_string_append_status bt_field_string_append(
655 struct bt_field *field, const char *value)
656{
657 return bt_field_string_append_with_length(field,
658 value, (uint64_t) strlen(value));
659}
660
661enum bt_field_string_append_status bt_field_string_append_with_length(
662 struct bt_field *field, const char *value, uint64_t length)
663{
664 struct bt_field_string *string_field = (void *) field;
665 char *data;
666 uint64_t new_length;
667
668 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
669 BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
670 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
671 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
672 BT_FIELD_CLASS_TYPE_STRING, "Field");
673
674 /* Make sure no null bytes are appended */
675 BT_ASSERT_PRE_DEV(memchr(value, '\0', length) == NULL,
676 "String value to append contains a null character: "
677 "partial-value=\"%.32s\", length=%" PRIu64, value, length);
678
679 new_length = length + string_field->length;
680
681 if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) {
682 g_array_set_size(string_field->buf, new_length + 1);
683 }
684
685 data = string_field->buf->data;
686 memcpy(data + string_field->length, value, length);
687 ((char *) string_field->buf->data)[new_length] = '\0';
688 string_field->length = new_length;
689 bt_field_set_single(field, true);
690 return BT_FUNC_STATUS_OK;
691}
692
693void bt_field_string_clear(struct bt_field *field)
694{
695 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
696 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
697 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
698 BT_FIELD_CLASS_TYPE_STRING, "Field");
699 clear_string_field(field);
700}
701
702uint64_t bt_field_array_get_length(const struct bt_field *field)
703{
704 const struct bt_field_array *array_field = (const void *) field;
705
706 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
707 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field");
708 return array_field->length;
709}
710
711enum bt_field_dynamic_array_set_length_status bt_field_dynamic_array_set_length(
712 struct bt_field *field, uint64_t length)
713{
714 int ret = BT_FUNC_STATUS_OK;
715 struct bt_field_array *array_field = (void *) field;
716
717 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
718 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
719 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, "Field");
720 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
721
722 if (G_UNLIKELY(length > array_field->fields->len)) {
723 /* Make more room */
724 struct bt_field_class_array *array_fc;
725 uint64_t cur_len = array_field->fields->len;
726 uint64_t i;
727
728 g_ptr_array_set_size(array_field->fields, length);
729 array_fc = (void *) field->class;
730
731 for (i = cur_len; i < array_field->fields->len; i++) {
732 struct bt_field *elem_field = bt_field_create(
733 array_fc->element_fc);
734
735 if (!elem_field) {
736 BT_LIB_LOGE_APPEND_CAUSE(
737 "Cannot create element field for "
738 "dynamic array field: "
739 "index=%" PRIu64 ", "
740 "%![array-field-]+f", i, field);
741 ret = BT_FUNC_STATUS_MEMORY_ERROR;
742 goto end;
743 }
744
745 BT_ASSERT(!array_field->fields->pdata[i]);
746 array_field->fields->pdata[i] = elem_field;
747 }
748 }
749
750 array_field->length = length;
751
752end:
753 return ret;
754}
755
756static inline
757struct bt_field *borrow_array_field_element_field_by_index(
758 struct bt_field *field, uint64_t index)
759{
760 struct bt_field_array *array_field = (void *) field;
761
762 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
763 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field");
764 BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length);
765 return array_field->fields->pdata[index];
766}
767
768struct bt_field *bt_field_array_borrow_element_field_by_index(
769 struct bt_field *field, uint64_t index)
770{
771 return borrow_array_field_element_field_by_index(field, index);
772}
773
774const struct bt_field *
775bt_field_array_borrow_element_field_by_index_const(
776 const struct bt_field *field, uint64_t index)
777{
778 return borrow_array_field_element_field_by_index((void *) field, index);
779}
780
781static inline
782struct bt_field *borrow_structure_field_member_field_by_index(
783 struct bt_field *field, uint64_t index)
784{
785 struct bt_field_structure *struct_field = (void *) field;
786
787 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
788 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
789 BT_FIELD_CLASS_TYPE_STRUCTURE, "Field");
790 BT_ASSERT_PRE_DEV_VALID_INDEX(index, struct_field->fields->len);
791 return struct_field->fields->pdata[index];
792}
793
794struct bt_field *bt_field_structure_borrow_member_field_by_index(
795 struct bt_field *field, uint64_t index)
796{
797 return borrow_structure_field_member_field_by_index(field,
798 index);
799}
800
801const struct bt_field *
802bt_field_structure_borrow_member_field_by_index_const(
803 const struct bt_field *field, uint64_t index)
804{
805 return borrow_structure_field_member_field_by_index(
806 (void *) field, index);
807}
808
809static inline
810struct bt_field *borrow_structure_field_member_field_by_name(
811 struct bt_field *field, const char *name)
812{
813 struct bt_field *ret_field = NULL;
814 struct bt_field_class_structure *struct_fc;
815 struct bt_field_structure *struct_field = (void *) field;
816 gpointer orig_key;
817 gpointer index;
818
819 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
820 BT_ASSERT_PRE_DEV_NON_NULL(name, "Field name");
821 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
822 BT_FIELD_CLASS_TYPE_STRUCTURE, "Field");
823 struct_fc = (void *) field->class;
824
825 if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name,
826 &orig_key, &index)) {
827 goto end;
828 }
829
830 ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)];
831 BT_ASSERT(ret_field);
832
833end:
834 return ret_field;
835}
836
837struct bt_field *bt_field_structure_borrow_member_field_by_name(
838 struct bt_field *field, const char *name)
839{
840 return borrow_structure_field_member_field_by_name(field, name);
841}
842
843const struct bt_field *bt_field_structure_borrow_member_field_by_name_const(
844 const struct bt_field *field, const char *name)
845{
846 return borrow_structure_field_member_field_by_name(
847 (void *) field, name);
848}
849
850static inline
851struct bt_field *borrow_variant_field_selected_option_field(
852 struct bt_field *field)
853{
854 struct bt_field_variant *var_field = (void *) field;
855
856 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
857 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
858 BT_FIELD_CLASS_TYPE_VARIANT, "Field");
859 BT_ASSERT_PRE_DEV(var_field->selected_field,
860 "Variant field has no selected field: %!+f", field);
861 return var_field->selected_field;
862}
863
864struct bt_field *bt_field_variant_borrow_selected_option_field(
865 struct bt_field *field)
866{
867 return borrow_variant_field_selected_option_field(field);
868}
869
870const struct bt_field *bt_field_variant_borrow_selected_option_field_const(
871 const struct bt_field *field)
872{
873 return borrow_variant_field_selected_option_field((void *) field);
874}
875
876enum bt_field_variant_select_option_field_status
877bt_field_variant_select_option_field(
878 struct bt_field *field, uint64_t index)
879{
880 struct bt_field_variant *var_field = (void *) field;
881
882 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
883 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
884 BT_FIELD_CLASS_TYPE_VARIANT, "Field");
885 BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
886 BT_ASSERT_PRE_DEV_VALID_INDEX(index, var_field->fields->len);
887 var_field->selected_field = var_field->fields->pdata[index];
888 var_field->selected_index = index;
889 return BT_FUNC_STATUS_OK;
890}
891
892uint64_t bt_field_variant_get_selected_option_field_index(
893 const struct bt_field *field)
894{
895 const struct bt_field_variant *var_field = (const void *) field;
896
897 BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
898 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
899 BT_FIELD_CLASS_TYPE_VARIANT, "Field");
900 BT_ASSERT_PRE_DEV(var_field->selected_field,
901 "Variant field has no selected field: %!+f", field);
902 return var_field->selected_index;
903}
904
905static inline
906void bt_field_finalize(struct bt_field *field)
907{
908 BT_ASSERT(field);
909 BT_LOGD_STR("Putting field's class.");
910 BT_OBJECT_PUT_REF_AND_RESET(field->class);
911}
912
913static
914void destroy_integer_field(struct bt_field *field)
915{
916 BT_ASSERT(field);
917 BT_LIB_LOGD("Destroying integer field object: %!+f", field);
918 bt_field_finalize(field);
919 g_free(field);
920}
921
922static
923void destroy_real_field(struct bt_field *field)
924{
925 BT_ASSERT(field);
926 BT_LIB_LOGD("Destroying real field object: %!+f", field);
927 bt_field_finalize(field);
928 g_free(field);
929}
930
931static
932void destroy_structure_field(struct bt_field *field)
933{
934 struct bt_field_structure *struct_field = (void *) field;
935
936 BT_ASSERT(field);
937 BT_LIB_LOGD("Destroying structure field object: %!+f", field);
938 bt_field_finalize(field);
939
940 if (struct_field->fields) {
941 g_ptr_array_free(struct_field->fields, TRUE);
942 struct_field->fields = NULL;
943 }
944
945 g_free(field);
946}
947
948static
949void destroy_variant_field(struct bt_field *field)
950{
951 struct bt_field_variant *var_field = (void *) field;
952
953 BT_ASSERT(field);
954 BT_LIB_LOGD("Destroying variant field object: %!+f", field);
955 bt_field_finalize(field);
956
957 if (var_field->fields) {
958 g_ptr_array_free(var_field->fields, TRUE);
959 var_field->fields = NULL;
960 }
961
962 g_free(field);
963}
964
965static
966void destroy_array_field(struct bt_field *field)
967{
968 struct bt_field_array *array_field = (void *) field;
969
970 BT_ASSERT(field);
971 BT_LIB_LOGD("Destroying array field object: %!+f", field);
972 bt_field_finalize(field);
973
974 if (array_field->fields) {
975 g_ptr_array_free(array_field->fields, TRUE);
976 array_field->fields = NULL;
977 }
978
979 g_free(field);
980}
981
982static
983void destroy_string_field(struct bt_field *field)
984{
985 struct bt_field_string *string_field = (void *) field;
986
987 BT_ASSERT(field);
988 BT_LIB_LOGD("Destroying string field object: %!+f", field);
989 bt_field_finalize(field);
990
991 if (string_field->buf) {
992 g_array_free(string_field->buf, TRUE);
993 string_field->buf = NULL;
994 }
995
996 g_free(field);
997}
998
999BT_HIDDEN
1000void bt_field_destroy(struct bt_field *field)
1001{
1002 BT_ASSERT(field);
1003 BT_ASSERT(bt_field_class_has_known_type(field->class));
1004 field_destroy_funcs[field->class->type](field);
1005}
1006
1007static
1008void reset_single_field(struct bt_field *field)
1009{
1010 BT_ASSERT(field);
1011 field->is_set = false;
1012}
1013
1014static
1015void reset_structure_field(struct bt_field *field)
1016{
1017 uint64_t i;
1018 struct bt_field_structure *struct_field = (void *) field;
1019
1020 BT_ASSERT(field);
1021
1022 for (i = 0; i < struct_field->fields->len; i++) {
1023 bt_field_reset(struct_field->fields->pdata[i]);
1024 }
1025}
1026
1027static
1028void reset_variant_field(struct bt_field *field)
1029{
1030 uint64_t i;
1031 struct bt_field_variant *var_field = (void *) field;
1032
1033 BT_ASSERT(field);
1034
1035 for (i = 0; i < var_field->fields->len; i++) {
1036 bt_field_reset(var_field->fields->pdata[i]);
1037 }
1038}
1039
1040static
1041void reset_array_field(struct bt_field *field)
1042{
1043 uint64_t i;
1044 struct bt_field_array *array_field = (void *) field;
1045
1046 BT_ASSERT(field);
1047
1048 for (i = 0; i < array_field->fields->len; i++) {
1049 bt_field_reset(array_field->fields->pdata[i]);
1050 }
1051}
1052
1053static
1054void set_single_field_is_frozen(struct bt_field *field, bool is_frozen)
1055{
1056 field->frozen = is_frozen;
1057}
1058
1059static
1060void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen)
1061{
1062 uint64_t i;
1063 struct bt_field_structure *struct_field = (void *) field;
1064
1065 BT_LIB_LOGD("Setting structure field's frozen state: "
1066 "%![field-]+f, is-frozen=%d", field, is_frozen);
1067
1068 for (i = 0; i < struct_field->fields->len; i++) {
1069 struct bt_field *member_field = struct_field->fields->pdata[i];
1070
1071 BT_LIB_LOGD("Setting structure field's member field's "
1072 "frozen state: %![field-]+f, index=%" PRIu64,
1073 member_field, i);
1074 bt_field_set_is_frozen(member_field, is_frozen);
1075 }
1076
1077 set_single_field_is_frozen(field, is_frozen);
1078}
1079
1080static
1081void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen)
1082{
1083 uint64_t i;
1084 struct bt_field_variant *var_field = (void *) field;
1085
1086 BT_LIB_LOGD("Setting variant field's frozen state: "
1087 "%![field-]+f, is-frozen=%d", field, is_frozen);
1088
1089 for (i = 0; i < var_field->fields->len; i++) {
1090 struct bt_field *option_field = var_field->fields->pdata[i];
1091
1092 BT_LIB_LOGD("Setting variant field's option field's "
1093 "frozen state: %![field-]+f, index=%" PRIu64,
1094 option_field, i);
1095 bt_field_set_is_frozen(option_field, is_frozen);
1096 }
1097
1098 set_single_field_is_frozen(field, is_frozen);
1099}
1100
1101static
1102void set_array_field_is_frozen(struct bt_field *field, bool is_frozen)
1103{
1104 uint64_t i;
1105 struct bt_field_array *array_field = (void *) field;
1106
1107 BT_LIB_LOGD("Setting array field's frozen state: "
1108 "%![field-]+f, is-frozen=%d", field, is_frozen);
1109
1110 for (i = 0; i < array_field->fields->len; i++) {
1111 struct bt_field *elem_field = array_field->fields->pdata[i];
1112
1113 BT_LIB_LOGD("Setting array field's element field's "
1114 "frozen state: %![field-]+f, index=%" PRIu64,
1115 elem_field, i);
1116 bt_field_set_is_frozen(elem_field, is_frozen);
1117 }
1118
1119 set_single_field_is_frozen(field, is_frozen);
1120}
1121
1122BT_HIDDEN
1123void _bt_field_set_is_frozen(const struct bt_field *field,
1124 bool is_frozen)
1125{
1126 BT_ASSERT(field);
1127 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1128 field, is_frozen);
1129 BT_ASSERT(field->methods->set_is_frozen);
1130 field->methods->set_is_frozen((void *) field, is_frozen);
1131}
1132
1133static
1134bool single_field_is_set(const struct bt_field *field)
1135{
1136 BT_ASSERT(field);
1137 return field->is_set;
1138}
1139
1140static
1141bool structure_field_is_set(const struct bt_field *field)
1142{
1143 bool is_set = true;
1144 uint64_t i;
1145 const struct bt_field_structure *struct_field = (const void *) field;
1146
1147 BT_ASSERT(field);
1148
1149 for (i = 0; i < struct_field->fields->len; i++) {
1150 is_set = bt_field_is_set(struct_field->fields->pdata[i]);
1151 if (!is_set) {
1152 goto end;
1153 }
1154 }
1155
1156end:
1157 return is_set;
1158}
1159
1160static
1161bool variant_field_is_set(const struct bt_field *field)
1162{
1163 const struct bt_field_variant *var_field = (const void *) field;
1164 bool is_set = false;
1165
1166 BT_ASSERT(field);
1167
1168 if (var_field->selected_field) {
1169 is_set = bt_field_is_set(var_field->selected_field);
1170 }
1171
1172 return is_set;
1173}
1174
1175static
1176bool array_field_is_set(const struct bt_field *field)
1177{
1178 bool is_set = true;
1179 uint64_t i;
1180 const struct bt_field_array *array_field = (const void *) field;
1181
1182 BT_ASSERT(field);
1183
1184 for (i = 0; i < array_field->length; i++) {
1185 is_set = bt_field_is_set(array_field->fields->pdata[i]);
1186 if (!is_set) {
1187 goto end;
1188 }
1189 }
1190
1191end:
1192 return is_set;
1193}
This page took 0.025959 seconds and 4 git commands to generate.