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