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