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