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