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