lib: update copyrights
[babeltrace.git] / 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 "FIELDS"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/field.h>
29 #include <babeltrace/trace-ir/field-const.h>
30 #include <babeltrace/trace-ir/field-internal.h>
31 #include <babeltrace/trace-ir/field-class-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/object.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/compat/fcntl-internal.h>
36 #include <babeltrace/align-internal.h>
37 #include <babeltrace/assert-internal.h>
38 #include <inttypes.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 int 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 bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
576 field->class, int_field->value.u, label_array, count);
577 }
578
579 int bt_field_signed_enumeration_get_mapping_labels(
580 const struct bt_field *field,
581 bt_field_class_enumeration_mapping_label_array *label_array,
582 uint64_t *count)
583 {
584 const struct bt_field_integer *int_field = (const void *) field;
585
586 BT_ASSERT_PRE_NON_NULL(field, "Field");
587 BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)");
588 BT_ASSERT_PRE_NON_NULL(label_array, "Count (output)");
589 BT_ASSERT_PRE_FIELD_IS_SET(field, "Field");
590 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
591 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field");
592 return bt_field_class_signed_enumeration_get_mapping_labels_by_value(
593 field->class, int_field->value.i, label_array, count);
594 }
595
596 const char *bt_field_string_get_value(const struct bt_field *field)
597 {
598 const struct bt_field_string *string_field = (const void *) field;
599
600 BT_ASSERT_PRE_NON_NULL(field, "Field");
601 BT_ASSERT_PRE_FIELD_IS_SET(field, "Field");
602 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
603 "Field");
604 return (const char *) string_field->buf->data;
605 }
606
607 uint64_t bt_field_string_get_length(const struct bt_field *field)
608 {
609 const struct bt_field_string *string_field = (const void *) field;
610
611 BT_ASSERT_PRE_NON_NULL(field, "Field");
612 BT_ASSERT_PRE_FIELD_IS_SET(field, "Field");
613 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
614 "Field");
615 return string_field->length;
616 }
617
618 int bt_field_string_set_value(struct bt_field *field, const char *value)
619 {
620 BT_ASSERT_PRE_NON_NULL(field, "Field");
621 BT_ASSERT_PRE_NON_NULL(value, "Value");
622 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
623 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
624 "Field");
625 bt_field_string_clear(field);
626 return bt_field_string_append_with_length(field, value,
627 (uint64_t) strlen(value));
628 }
629
630 int bt_field_string_append(struct bt_field *field, const char *value)
631 {
632 return bt_field_string_append_with_length(field,
633 value, (uint64_t) strlen(value));
634 }
635
636 int bt_field_string_append_with_length(struct bt_field *field,
637 const char *value, uint64_t length)
638 {
639 struct bt_field_string *string_field = (void *) field;
640 char *data;
641 uint64_t new_length;
642
643 BT_ASSERT_PRE_NON_NULL(field, "Field");
644 BT_ASSERT_PRE_NON_NULL(value, "Value");
645 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
646 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
647 BT_FIELD_CLASS_TYPE_STRING, "Field");
648
649 /* Make sure no null bytes are appended */
650 BT_ASSERT_PRE(memchr(value, '\0', length) == NULL,
651 "String value to append contains a null character: "
652 "partial-value=\"%.32s\", length=%" PRIu64, value, length);
653
654 new_length = length + string_field->length;
655
656 if (unlikely(new_length + 1 > string_field->buf->len)) {
657 g_array_set_size(string_field->buf, new_length + 1);
658 }
659
660 data = string_field->buf->data;
661 memcpy(data + string_field->length, value, length);
662 ((char *) string_field->buf->data)[new_length] = '\0';
663 string_field->length = new_length;
664 bt_field_set_single(field, true);
665 return 0;
666 }
667
668 int bt_field_string_clear(struct bt_field *field)
669 {
670 struct bt_field_string *string_field = (void *) field;
671
672 BT_ASSERT_PRE_NON_NULL(field, "Field");
673 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
674 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
675 BT_FIELD_CLASS_TYPE_STRING, "Field");
676 string_field->length = 0;
677 bt_field_set_single(field, true);
678 return 0;
679 }
680
681 uint64_t bt_field_array_get_length(const struct bt_field *field)
682 {
683 const struct bt_field_array *array_field = (const void *) field;
684
685 BT_ASSERT_PRE_NON_NULL(field, "Field");
686 BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field");
687 return array_field->length;
688 }
689
690 int bt_field_dynamic_array_set_length(struct bt_field *field, uint64_t length)
691 {
692 int ret = 0;
693 struct bt_field_array *array_field = (void *) field;
694
695 BT_ASSERT_PRE_NON_NULL(field, "Field");
696 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
697 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, "Field");
698 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
699
700 if (unlikely(length > array_field->fields->len)) {
701 /* Make more room */
702 struct bt_field_class_array *array_fc;
703 uint64_t cur_len = array_field->fields->len;
704 uint64_t i;
705
706 g_ptr_array_set_size(array_field->fields, length);
707 array_fc = (void *) field->class;
708
709 for (i = cur_len; i < array_field->fields->len; i++) {
710 struct bt_field *elem_field = bt_field_create(
711 array_fc->element_fc);
712
713 if (!elem_field) {
714 BT_LIB_LOGE("Cannot create element field for "
715 "dynamic array field: "
716 "index=%" PRIu64 ", "
717 "%![array-field-]+f", i, field);
718 ret = -1;
719 goto end;
720 }
721
722 BT_ASSERT(!array_field->fields->pdata[i]);
723 array_field->fields->pdata[i] = elem_field;
724 }
725 }
726
727 array_field->length = length;
728
729 end:
730 return ret;
731 }
732
733 static inline
734 struct bt_field *borrow_array_field_element_field_by_index(
735 struct bt_field *field, uint64_t index)
736 {
737 struct bt_field_array *array_field = (void *) field;
738
739 BT_ASSERT_PRE_NON_NULL(field, "Field");
740 BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field");
741 BT_ASSERT_PRE_VALID_INDEX(index, array_field->length);
742 return array_field->fields->pdata[index];
743 }
744
745 struct bt_field *bt_field_array_borrow_element_field_by_index(
746 struct bt_field *field, uint64_t index)
747 {
748 return borrow_array_field_element_field_by_index(field, index);
749 }
750
751 const struct bt_field *
752 bt_field_array_borrow_element_field_by_index_const(
753 const struct bt_field *field, uint64_t index)
754 {
755 return borrow_array_field_element_field_by_index((void *) field, index);
756 }
757
758 static inline
759 struct bt_field *borrow_structure_field_member_field_by_index(
760 struct bt_field *field, uint64_t index)
761 {
762 struct bt_field_structure *struct_field = (void *) field;
763
764 BT_ASSERT_PRE_NON_NULL(field, "Field");
765 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
766 BT_FIELD_CLASS_TYPE_STRUCTURE, "Field");
767 BT_ASSERT_PRE_VALID_INDEX(index, struct_field->fields->len);
768 return struct_field->fields->pdata[index];
769 }
770
771 struct bt_field *bt_field_structure_borrow_member_field_by_index(
772 struct bt_field *field, uint64_t index)
773 {
774 return borrow_structure_field_member_field_by_index(field,
775 index);
776 }
777
778 const struct bt_field *
779 bt_field_structure_borrow_member_field_by_index_const(
780 const struct bt_field *field, uint64_t index)
781 {
782 return borrow_structure_field_member_field_by_index(
783 (void *) field, index);
784 }
785
786 static inline
787 struct bt_field *borrow_structure_field_member_field_by_name(
788 struct bt_field *field, const char *name)
789 {
790 struct bt_field *ret_field = NULL;
791 struct bt_field_class_structure *struct_fc;
792 struct bt_field_structure *struct_field = (void *) field;
793 gpointer orig_key;
794 gpointer index;
795
796 BT_ASSERT_PRE_NON_NULL(field, "Field");
797 BT_ASSERT_PRE_NON_NULL(name, "Field name");
798 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
799 BT_FIELD_CLASS_TYPE_STRUCTURE, "Field");
800 struct_fc = (void *) field->class;
801
802 if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name,
803 &orig_key, &index)) {
804 goto end;
805 }
806
807 ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)];
808 BT_ASSERT(ret_field);
809
810 end:
811 return ret_field;
812 }
813
814 struct bt_field *bt_field_structure_borrow_member_field_by_name(
815 struct bt_field *field, const char *name)
816 {
817 return borrow_structure_field_member_field_by_name(field, name);
818 }
819
820 const struct bt_field *bt_field_structure_borrow_member_field_by_name_const(
821 const struct bt_field *field, const char *name)
822 {
823 return borrow_structure_field_member_field_by_name(
824 (void *) field, name);
825 }
826
827 static inline
828 struct bt_field *borrow_variant_field_selected_option_field(
829 struct bt_field *field)
830 {
831 struct bt_field_variant *var_field = (void *) field;
832
833 BT_ASSERT_PRE_NON_NULL(field, "Field");
834 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
835 BT_FIELD_CLASS_TYPE_VARIANT, "Field");
836 BT_ASSERT_PRE(var_field->selected_field,
837 "Variant field has no selected field: %!+f", field);
838 return var_field->selected_field;
839 }
840
841 struct bt_field *bt_field_variant_borrow_selected_option_field(
842 struct bt_field *field)
843 {
844 return borrow_variant_field_selected_option_field(field);
845 }
846
847 const struct bt_field *bt_field_variant_borrow_selected_option_field_const(
848 const struct bt_field *field)
849 {
850 return borrow_variant_field_selected_option_field((void *) field);
851 }
852
853 int bt_field_variant_select_option_field(
854 struct bt_field *field, uint64_t index)
855 {
856 struct bt_field_variant *var_field = (void *) field;
857
858 BT_ASSERT_PRE_NON_NULL(field, "Field");
859 BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field,
860 BT_FIELD_CLASS_TYPE_VARIANT, "Field");
861 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
862 BT_ASSERT_PRE_VALID_INDEX(index, var_field->fields->len);
863 var_field->selected_field = var_field->fields->pdata[index];
864 var_field->selected_index = index;
865 return 0;
866 }
867
868 uint64_t bt_field_variant_get_selected_option_field_index(
869 const struct bt_field *field)
870 {
871 const struct bt_field_variant *var_field = (const 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(var_field->selected_field,
877 "Variant field has no selected field: %!+f", field);
878 return var_field->selected_index;
879 }
880
881 static inline
882 void bt_field_finalize(struct bt_field *field)
883 {
884 BT_ASSERT(field);
885 BT_LOGD_STR("Putting field's class.");
886 BT_OBJECT_PUT_REF_AND_RESET(field->class);
887 }
888
889 static
890 void destroy_integer_field(struct bt_field *field)
891 {
892 BT_ASSERT(field);
893 BT_LIB_LOGD("Destroying integer field object: %!+f", field);
894 bt_field_finalize(field);
895 g_free(field);
896 }
897
898 static
899 void destroy_real_field(struct bt_field *field)
900 {
901 BT_ASSERT(field);
902 BT_LIB_LOGD("Destroying real field object: %!+f", field);
903 bt_field_finalize(field);
904 g_free(field);
905 }
906
907 static
908 void destroy_structure_field(struct bt_field *field)
909 {
910 struct bt_field_structure *struct_field = (void *) field;
911
912 BT_ASSERT(field);
913 BT_LIB_LOGD("Destroying structure field object: %!+f", field);
914 bt_field_finalize(field);
915
916 if (struct_field->fields) {
917 g_ptr_array_free(struct_field->fields, TRUE);
918 struct_field->fields = NULL;
919 }
920
921 g_free(field);
922 }
923
924 static
925 void destroy_variant_field(struct bt_field *field)
926 {
927 struct bt_field_variant *var_field = (void *) field;
928
929 BT_ASSERT(field);
930 BT_LIB_LOGD("Destroying variant field object: %!+f", field);
931 bt_field_finalize(field);
932
933 if (var_field->fields) {
934 g_ptr_array_free(var_field->fields, TRUE);
935 var_field->fields = NULL;
936 }
937
938 g_free(field);
939 }
940
941 static
942 void destroy_array_field(struct bt_field *field)
943 {
944 struct bt_field_array *array_field = (void *) field;
945
946 BT_ASSERT(field);
947 BT_LIB_LOGD("Destroying array field object: %!+f", field);
948 bt_field_finalize(field);
949
950 if (array_field->fields) {
951 g_ptr_array_free(array_field->fields, TRUE);
952 array_field->fields = NULL;
953 }
954
955 g_free(field);
956 }
957
958 static
959 void destroy_string_field(struct bt_field *field)
960 {
961 struct bt_field_string *string_field = (void *) field;
962
963 BT_ASSERT(field);
964 BT_LIB_LOGD("Destroying string field object: %!+f", field);
965 bt_field_finalize(field);
966
967 if (string_field->buf) {
968 g_array_free(string_field->buf, TRUE);
969 string_field->buf = NULL;
970 }
971
972 g_free(field);
973 }
974
975 BT_HIDDEN
976 void bt_field_destroy(struct bt_field *field)
977 {
978 BT_ASSERT(field);
979 BT_ASSERT(bt_field_class_has_known_type(field->class));
980 field_destroy_funcs[field->class->type](field);
981 }
982
983 static
984 void reset_single_field(struct bt_field *field)
985 {
986 BT_ASSERT(field);
987 field->is_set = false;
988 }
989
990 static
991 void reset_structure_field(struct bt_field *field)
992 {
993 uint64_t i;
994 struct bt_field_structure *struct_field = (void *) field;
995
996 BT_ASSERT(field);
997
998 for (i = 0; i < struct_field->fields->len; i++) {
999 bt_field_reset(struct_field->fields->pdata[i]);
1000 }
1001 }
1002
1003 static
1004 void reset_variant_field(struct bt_field *field)
1005 {
1006 uint64_t i;
1007 struct bt_field_variant *var_field = (void *) field;
1008
1009 BT_ASSERT(field);
1010
1011 for (i = 0; i < var_field->fields->len; i++) {
1012 bt_field_reset(var_field->fields->pdata[i]);
1013 }
1014 }
1015
1016 static
1017 void reset_array_field(struct bt_field *field)
1018 {
1019 uint64_t i;
1020 struct bt_field_array *array_field = (void *) field;
1021
1022 BT_ASSERT(field);
1023
1024 for (i = 0; i < array_field->fields->len; i++) {
1025 bt_field_reset(array_field->fields->pdata[i]);
1026 }
1027 }
1028
1029 static
1030 void set_single_field_is_frozen(struct bt_field *field, bool is_frozen)
1031 {
1032 field->frozen = is_frozen;
1033 }
1034
1035 static
1036 void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen)
1037 {
1038 uint64_t i;
1039 struct bt_field_structure *struct_field = (void *) field;
1040
1041 BT_LIB_LOGD("Setting structure field's frozen state: "
1042 "%![field-]+f, is-frozen=%d", field, is_frozen);
1043
1044 for (i = 0; i < struct_field->fields->len; i++) {
1045 struct bt_field *member_field = struct_field->fields->pdata[i];
1046
1047 BT_LIB_LOGD("Setting structure field's member field's "
1048 "frozen state: %![field-]+f, index=%" PRIu64,
1049 member_field, i);
1050 bt_field_set_is_frozen(member_field, is_frozen);
1051 }
1052
1053 set_single_field_is_frozen(field, is_frozen);
1054 }
1055
1056 static
1057 void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen)
1058 {
1059 uint64_t i;
1060 struct bt_field_variant *var_field = (void *) field;
1061
1062 BT_LIB_LOGD("Setting variant field's frozen state: "
1063 "%![field-]+f, is-frozen=%d", field, is_frozen);
1064
1065 for (i = 0; i < var_field->fields->len; i++) {
1066 struct bt_field *option_field = var_field->fields->pdata[i];
1067
1068 BT_LIB_LOGD("Setting variant field's option field's "
1069 "frozen state: %![field-]+f, index=%" PRIu64,
1070 option_field, i);
1071 bt_field_set_is_frozen(option_field, is_frozen);
1072 }
1073
1074 set_single_field_is_frozen(field, is_frozen);
1075 }
1076
1077 static
1078 void set_array_field_is_frozen(struct bt_field *field, bool is_frozen)
1079 {
1080 uint64_t i;
1081 struct bt_field_array *array_field = (void *) field;
1082
1083 BT_LIB_LOGD("Setting array field's frozen state: "
1084 "%![field-]+f, is-frozen=%d", field, is_frozen);
1085
1086 for (i = 0; i < array_field->fields->len; i++) {
1087 struct bt_field *elem_field = array_field->fields->pdata[i];
1088
1089 BT_LIB_LOGD("Setting array field's element field's "
1090 "frozen state: %![field-]+f, index=%" PRIu64,
1091 elem_field, i);
1092 bt_field_set_is_frozen(elem_field, is_frozen);
1093 }
1094
1095 set_single_field_is_frozen(field, is_frozen);
1096 }
1097
1098 BT_HIDDEN
1099 void _bt_field_set_is_frozen(const struct bt_field *field,
1100 bool is_frozen)
1101 {
1102 BT_ASSERT(field);
1103 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1104 field, is_frozen);
1105 BT_ASSERT(field->methods->set_is_frozen);
1106 field->methods->set_is_frozen((void *) field, is_frozen);
1107 }
1108
1109 static
1110 bool single_field_is_set(const struct bt_field *field)
1111 {
1112 BT_ASSERT(field);
1113 return field->is_set;
1114 }
1115
1116 static
1117 bool structure_field_is_set(const struct bt_field *field)
1118 {
1119 bool is_set = true;
1120 uint64_t i;
1121 const struct bt_field_structure *struct_field = (const void *) field;
1122
1123 BT_ASSERT(field);
1124
1125 for (i = 0; i < struct_field->fields->len; i++) {
1126 is_set = bt_field_is_set(struct_field->fields->pdata[i]);
1127 if (!is_set) {
1128 goto end;
1129 }
1130 }
1131
1132 end:
1133 return is_set;
1134 }
1135
1136 static
1137 bool variant_field_is_set(const struct bt_field *field)
1138 {
1139 const struct bt_field_variant *var_field = (const void *) field;
1140 bool is_set = false;
1141
1142 BT_ASSERT(field);
1143
1144 if (var_field->selected_field) {
1145 is_set = bt_field_is_set(var_field->selected_field);
1146 }
1147
1148 return is_set;
1149 }
1150
1151 static
1152 bool array_field_is_set(const struct bt_field *field)
1153 {
1154 bool is_set = true;
1155 uint64_t i;
1156 const struct bt_field_array *array_field = (const void *) field;
1157
1158 BT_ASSERT(field);
1159
1160 for (i = 0; i < array_field->length; i++) {
1161 is_set = bt_field_is_set(array_field->fields->pdata[i]);
1162 if (!is_set) {
1163 goto end;
1164 }
1165 }
1166
1167 end:
1168 return is_set;
1169 }
This page took 0.056766 seconds and 4 git commands to generate.