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