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