Make API CTF-agnostic
[babeltrace.git] / lib / ctf-ir / fields.c
1 /*
2 * fields.c
3 *
4 * Babeltrace CTF 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/ctf-ir/fields.h>
34 #include <babeltrace/ctf-ir/fields-internal.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/ref.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
44 static
45 void reset_single_field(struct bt_field *field);
46
47 static
48 void reset_array_field(struct bt_field *field);
49
50 static
51 void reset_structure_field(struct bt_field *field);
52
53 static
54 void reset_variant_field(struct bt_field *field);
55
56 static
57 void set_single_field_is_frozen(struct bt_field *field, bool is_frozen);
58
59 static
60 void set_array_field_is_frozen(struct bt_field *field, bool is_frozen);
61
62 static
63 void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen);
64
65 static
66 void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen);
67
68 static
69 bool single_field_is_set(struct bt_field *field);
70
71 static
72 bool array_field_is_set(struct bt_field *field);
73
74 static
75 bool structure_field_is_set(struct bt_field *field);
76
77 static
78 bool variant_field_is_set(struct bt_field *field);
79
80 static
81 struct 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
87 static
88 struct 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
94 static
95 struct 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
101 static
102 struct 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
108 static
109 struct 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
115 static
116 struct 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
122 static
123 struct bt_field *create_integer_field(struct bt_field_type *);
124
125 static
126 struct bt_field *create_real_field(struct bt_field_type *);
127
128 static
129 struct bt_field *create_string_field(struct bt_field_type *);
130
131 static
132 struct bt_field *create_structure_field(struct bt_field_type *);
133
134 static
135 struct bt_field *create_static_array_field(struct bt_field_type *);
136
137 static
138 struct bt_field *create_dynamic_array_field(struct bt_field_type *);
139
140 static
141 struct bt_field *create_variant_field(struct bt_field_type *);
142
143 static
144 struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = {
145 [BT_FIELD_TYPE_ID_UNSIGNED_INTEGER] = create_integer_field,
146 [BT_FIELD_TYPE_ID_SIGNED_INTEGER] = create_integer_field,
147 [BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION] = create_integer_field,
148 [BT_FIELD_TYPE_ID_SIGNED_ENUMERATION] = create_integer_field,
149 [BT_FIELD_TYPE_ID_REAL] = create_real_field,
150 [BT_FIELD_TYPE_ID_STRING] = create_string_field,
151 [BT_FIELD_TYPE_ID_STRUCTURE] = create_structure_field,
152 [BT_FIELD_TYPE_ID_STATIC_ARRAY] = create_static_array_field,
153 [BT_FIELD_TYPE_ID_DYNAMIC_ARRAY] = create_dynamic_array_field,
154 [BT_FIELD_TYPE_ID_VARIANT] = create_variant_field,
155 };
156
157 static
158 void destroy_integer_field(struct bt_field *field);
159
160 static
161 void destroy_real_field(struct bt_field *field);
162
163 static
164 void destroy_string_field(struct bt_field *field);
165
166 static
167 void destroy_structure_field(struct bt_field *field);
168
169 static
170 void destroy_array_field(struct bt_field *field);
171
172 static
173 void destroy_variant_field(struct bt_field *field);
174
175 static
176 void (* const field_destroy_funcs[])(struct bt_field *) = {
177 [BT_FIELD_TYPE_ID_UNSIGNED_INTEGER] = destroy_integer_field,
178 [BT_FIELD_TYPE_ID_SIGNED_INTEGER] = destroy_integer_field,
179 [BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION] = destroy_integer_field,
180 [BT_FIELD_TYPE_ID_SIGNED_ENUMERATION] = destroy_integer_field,
181 [BT_FIELD_TYPE_ID_REAL] = destroy_real_field,
182 [BT_FIELD_TYPE_ID_STRING] = destroy_string_field,
183 [BT_FIELD_TYPE_ID_STRUCTURE] = destroy_structure_field,
184 [BT_FIELD_TYPE_ID_STATIC_ARRAY] = destroy_array_field,
185 [BT_FIELD_TYPE_ID_DYNAMIC_ARRAY] = destroy_array_field,
186 [BT_FIELD_TYPE_ID_VARIANT] = destroy_variant_field,
187 };
188
189 struct bt_field_type *bt_field_borrow_type(struct bt_field *field)
190 {
191 BT_ASSERT_PRE_NON_NULL(field, "Field");
192 return field->type;
193 }
194
195 enum bt_field_type_id bt_field_get_type_id(struct bt_field *field)
196 {
197 BT_ASSERT_PRE_NON_NULL(field, "Field");
198 return field->type->id;
199 }
200
201 BT_HIDDEN
202 struct bt_field *bt_field_create(struct bt_field_type *ft)
203 {
204 struct bt_field *field = NULL;
205
206 BT_ASSERT_PRE_NON_NULL(ft, "Field type");
207 BT_ASSERT(bt_field_type_has_known_id(ft));
208 field = field_create_funcs[ft->id](ft);
209 if (!field) {
210 BT_LIB_LOGE("Cannot create field object from field type: "
211 "%![ft-]+F", ft);
212 goto end;
213 }
214
215 end:
216 return field;
217 }
218
219 static inline
220 void init_field(struct bt_field *field, struct bt_field_type *ft,
221 struct bt_field_methods *methods)
222 {
223 BT_ASSERT(field);
224 BT_ASSERT(ft);
225 bt_object_init_unique(&field->base);
226 field->methods = methods;
227 field->type = bt_get(ft);
228 }
229
230 static
231 struct bt_field *create_integer_field(struct bt_field_type *ft)
232 {
233 struct bt_field_integer *int_field;
234
235 BT_LIB_LOGD("Creating integer field object: %![ft-]+F", ft);
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, ft, &integer_field_methods);
243 BT_LIB_LOGD("Created integer field object: %!+f", int_field);
244
245 end:
246 return (void *) int_field;
247 }
248
249 static
250 struct bt_field *create_real_field(struct bt_field_type *ft)
251 {
252 struct bt_field_real *real_field;
253
254 BT_LIB_LOGD("Creating real field object: %![ft-]+F", ft);
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, ft, &real_field_methods);
262 BT_LIB_LOGD("Created real field object: %!+f", real_field);
263
264 end:
265 return (void *) real_field;
266 }
267
268 static
269 struct bt_field *create_string_field(struct bt_field_type *ft)
270 {
271 struct bt_field_string *string_field;
272
273 BT_LIB_LOGD("Creating string field object: %![ft-]+F", ft);
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, ft, &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_PUT(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
292 end:
293 return (void *) string_field;
294 }
295
296 static inline
297 int create_fields_from_named_field_types(
298 struct bt_field_type_named_field_types_container *ft,
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, ft->named_fts->len);
313
314 for (i = 0; i < ft->named_fts->len; i++) {
315 struct bt_field *field;
316 struct bt_named_field_type *named_ft =
317 BT_FIELD_TYPE_NAMED_FT_AT_INDEX(ft, i);
318
319 field = bt_field_create(named_ft->ft);
320 if (!field) {
321 BT_LIB_LOGE("Failed to create structure member or variant option field: "
322 "name=\"%s\", %![ft-]+F",
323 named_ft->name->str, named_ft->ft);
324 ret = -1;
325 goto end;
326 }
327
328 g_ptr_array_index(*fields, i) = field;
329 }
330
331 end:
332 return ret;
333 }
334
335 static
336 struct bt_field *create_structure_field(struct bt_field_type *ft)
337 {
338 struct bt_field_structure *struct_field;
339
340 BT_LIB_LOGD("Creating structure field object: %![ft-]+F", ft);
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, ft, &structure_field_methods);
348
349 if (create_fields_from_named_field_types((void *) ft,
350 &struct_field->fields)) {
351 BT_LIB_LOGE("Cannot create structure member fields: "
352 "%![ft-]+F", ft);
353 BT_PUT(struct_field);
354 goto end;
355 }
356
357 BT_LIB_LOGD("Created structure field object: %!+f", struct_field);
358
359 end:
360 return (void *) struct_field;
361 }
362
363 static
364 struct bt_field *create_variant_field(struct bt_field_type *ft)
365 {
366 struct bt_field_variant *var_field;
367
368 BT_LIB_LOGD("Creating variant field object: %![ft-]+F", ft);
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, ft, &variant_field_methods);
376
377 if (create_fields_from_named_field_types((void *) ft,
378 &var_field->fields)) {
379 BT_LIB_LOGE("Cannot create variant member fields: "
380 "%![ft-]+F", ft);
381 BT_PUT(var_field);
382 goto end;
383 }
384
385 BT_LIB_LOGD("Created variant field object: %!+f", var_field);
386
387 end:
388 return (void *) var_field;
389 }
390
391 static inline
392 int init_array_field_fields(struct bt_field_array *array_field)
393 {
394 int ret = 0;
395 uint64_t i;
396 struct bt_field_type_array *array_ft;
397
398 BT_ASSERT(array_field);
399 array_ft = (void *) array_field->common.type;
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_ft->element_ft);
414 if (!array_field->fields->pdata[i]) {
415 BT_LIB_LOGE("Cannot create array field's element field: "
416 "index=%" PRIu64 ", %![ft-]+F", i, array_ft);
417 ret = -1;
418 goto end;
419 }
420 }
421
422 end:
423 return ret;
424 }
425
426 static
427 struct bt_field *create_static_array_field(struct bt_field_type *ft)
428 {
429 struct bt_field_type_static_array *array_ft = (void *) ft;
430 struct bt_field_array *array_field;
431
432 BT_LIB_LOGD("Creating static array field object: %![ft-]+F", ft);
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, ft, &array_field_methods);
440 array_field->length = array_ft->length;
441
442 if (init_array_field_fields(array_field)) {
443 BT_LIB_LOGE("Cannot create static array fields: "
444 "%![ft-]+F", ft);
445 BT_PUT(array_field);
446 goto end;
447 }
448
449 BT_LIB_LOGD("Created static array field object: %!+f", array_field);
450
451 end:
452 return (void *) array_field;
453 }
454
455 static
456 struct bt_field *create_dynamic_array_field(struct bt_field_type *ft)
457 {
458 struct bt_field_array *array_field;
459
460 BT_LIB_LOGD("Creating dynamic array field object: %![ft-]+F", ft);
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, ft, &array_field_methods);
468
469 if (init_array_field_fields(array_field)) {
470 BT_LIB_LOGE("Cannot create dynamic array fields: "
471 "%![ft-]+F", ft);
472 BT_PUT(array_field);
473 goto end;
474 }
475
476 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field);
477
478 end:
479 return (void *) array_field;
480 }
481
482 int64_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
492 void 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_type_integer *) field->type)->range, value),
501 "Value is out of bounds: value=%" PRId64 ", %![field-]+f, "
502 "%![ft-]+F", value, field, field->type);
503 int_field->value.i = value;
504 bt_field_set_single(field, true);
505 }
506
507 uint64_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
517 void 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_type_integer *) field->type)->range, value),
527 "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, "
528 "%![ft-]+F", value, field, field->type);
529 int_field->value.u = value;
530 bt_field_set_single(field, true);
531 }
532
533 double 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_TYPE_ID(field, BT_FIELD_TYPE_ID_REAL, "Field");
540 return real_field->value;
541 }
542
543 void 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_TYPE_ID(field, BT_FIELD_TYPE_ID_REAL, "Field");
549 BT_ASSERT_PRE_FIELD_HOT(field, "Field");
550 BT_ASSERT_PRE(
551 !((struct bt_field_type_real *) field->type)->is_single_precision ||
552 (double) (float) value == value,
553 "Invalid value for a single-precision real number: value=%f, "
554 "%![ft-]+F", value, field->type);
555 real_field->value = value;
556 bt_field_set_single(field, true);
557 }
558
559 int bt_field_unsigned_enumeration_get_mapping_labels(struct bt_field *field,
560 bt_field_type_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_TYPE_ID(field,
570 BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, "Field");
571 return bt_field_type_unsigned_enumeration_get_mapping_labels_by_value(
572 field->type, int_field->value.u, label_array, count);
573 }
574
575 int bt_field_signed_enumeration_get_mapping_labels(struct bt_field *field,
576 bt_field_type_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_TYPE_ID(field,
586 BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, "Field");
587 return bt_field_type_signed_enumeration_get_mapping_labels_by_value(
588 field->type, int_field->value.i, label_array, count);
589 }
590
591 const 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_TYPE_ID(field, BT_FIELD_TYPE_ID_STRING,
598 "Field");
599 return (const char *) string_field->buf->data;
600 }
601
602 uint64_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_TYPE_ID(field, BT_FIELD_TYPE_ID_STRING,
609 "Field");
610 return string_field->length;
611 }
612
613 int 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_TYPE_ID(field, BT_FIELD_TYPE_ID_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
625 int 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
631 int 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_TYPE_ID(field,
642 BT_FIELD_TYPE_ID_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
663 int 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_TYPE_ID(field,
670 BT_FIELD_TYPE_ID_STRING, "Field");
671 string_field->length = 0;
672 bt_field_set_single(field, true);
673 return 0;
674 }
675
676 uint64_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
685 int 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_TYPE_ID(field,
693 BT_FIELD_TYPE_ID_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_type_array *array_ft;
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_ft = (void *) field->type;
704
705 for (i = cur_len; i < array_field->fields->len; i++) {
706 struct bt_field *elem_field = bt_field_create(
707 array_ft->element_ft);
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
725 end:
726 return ret;
727 }
728
729 struct 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
740 struct 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_TYPE_ID(field,
747 BT_FIELD_TYPE_ID_STRUCTURE, "Field");
748 BT_ASSERT_PRE_VALID_INDEX(index, struct_field->fields->len);
749 return struct_field->fields->pdata[index];
750 }
751
752 struct 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_type_structure *struct_ft;
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_TYPE_ID(field,
764 BT_FIELD_TYPE_ID_STRUCTURE, "Field");
765 struct_ft = (void *) field->type;
766
767 if (!g_hash_table_lookup_extended(struct_ft->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
775 end:
776 return ret_field;
777 }
778
779 struct 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_TYPE_ID(field,
786 BT_FIELD_TYPE_ID_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
792 int 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_TYPE_ID(field,
799 BT_FIELD_TYPE_ID_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
807 uint64_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_TYPE_ID(field,
814 BT_FIELD_TYPE_ID_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
820 static inline
821 void bt_field_finalize(struct bt_field *field)
822 {
823 BT_ASSERT(field);
824 BT_LOGD_STR("Putting field's type.");
825 bt_put(field->type);
826 }
827
828 static
829 void 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
837 static
838 void 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
846 static
847 void 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
862 static
863 void 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
878 static
879 void 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
894 static
895 void 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
910 BT_HIDDEN
911 void bt_field_destroy(struct bt_field *field)
912 {
913 BT_ASSERT(field);
914 BT_ASSERT(bt_field_type_has_known_id(field->type));
915 field_destroy_funcs[field->type->id](field);
916 }
917
918 static
919 void reset_single_field(struct bt_field *field)
920 {
921 BT_ASSERT(field);
922 field->is_set = false;
923 }
924
925 static
926 void 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
938 static
939 void 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
951 static
952 void 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
964 static
965 void set_single_field_is_frozen(struct bt_field *field, bool is_frozen)
966 {
967 field->frozen = is_frozen;
968 }
969
970 static
971 void 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
991 static
992 void 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
1012 static
1013 void 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
1033 BT_HIDDEN
1034 void _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
1044 static
1045 bool single_field_is_set(struct bt_field *field)
1046 {
1047 BT_ASSERT(field);
1048 return field->is_set;
1049 }
1050
1051 static
1052 bool 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
1067 end:
1068 return is_set;
1069 }
1070
1071 static
1072 bool 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
1086 static
1087 bool 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
1102 end:
1103 return is_set;
1104 }
This page took 0.090049 seconds and 4 git commands to generate.