lib: trace IR, values: reset pointers to `NULL` on destruction
[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 = bt_object_get_ref(fc);
231 }
232
233 static
234 struct bt_field *create_integer_field(struct bt_field_class *fc)
235 {
236 struct bt_field_integer *int_field;
237
238 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc);
239 int_field = g_new0(struct bt_field_integer, 1);
240 if (!int_field) {
241 BT_LOGE_STR("Failed to allocate one integer field.");
242 goto end;
243 }
244
245 init_field((void *) int_field, fc, &integer_field_methods);
246 BT_LIB_LOGD("Created integer field object: %!+f", int_field);
247
248 end:
249 return (void *) int_field;
250 }
251
252 static
253 struct bt_field *create_real_field(struct bt_field_class *fc)
254 {
255 struct bt_field_real *real_field;
256
257 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc);
258 real_field = g_new0(struct bt_field_real, 1);
259 if (!real_field) {
260 BT_LOGE_STR("Failed to allocate one real field.");
261 goto end;
262 }
263
264 init_field((void *) real_field, fc, &real_field_methods);
265 BT_LIB_LOGD("Created real field object: %!+f", real_field);
266
267 end:
268 return (void *) real_field;
269 }
270
271 static
272 struct bt_field *create_string_field(struct bt_field_class *fc)
273 {
274 struct bt_field_string *string_field;
275
276 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc);
277 string_field = g_new0(struct bt_field_string, 1);
278 if (!string_field) {
279 BT_LOGE_STR("Failed to allocate one string field.");
280 goto end;
281 }
282
283 init_field((void *) string_field, fc, &string_field_methods);
284 string_field->buf = g_array_sized_new(FALSE, FALSE,
285 sizeof(char), 1);
286 if (!string_field->buf) {
287 BT_LOGE_STR("Failed to allocate a GArray.");
288 BT_OBJECT_PUT_REF_AND_RESET(string_field);
289 goto end;
290 }
291
292 g_array_index(string_field->buf, char, 0) = '\0';
293 BT_LIB_LOGD("Created string field object: %!+f", string_field);
294
295 end:
296 return (void *) string_field;
297 }
298
299 static inline
300 int create_fields_from_named_field_classes(
301 struct bt_field_class_named_field_class_container *fc,
302 GPtrArray **fields)
303 {
304 int ret = 0;
305 uint64_t i;
306
307 *fields = g_ptr_array_new_with_free_func(
308 (GDestroyNotify) bt_field_destroy);
309 if (!*fields) {
310 BT_LOGE_STR("Failed to allocate a GPtrArray.");
311 ret = -1;
312 goto end;
313 }
314
315 g_ptr_array_set_size(*fields, fc->named_fcs->len);
316
317 for (i = 0; i < fc->named_fcs->len; i++) {
318 struct bt_field *field;
319 struct bt_named_field_class *named_fc =
320 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, i);
321
322 field = bt_field_create(named_fc->fc);
323 if (!field) {
324 BT_LIB_LOGE("Failed to create structure member or variant option field: "
325 "name=\"%s\", %![fc-]+F",
326 named_fc->name->str, named_fc->fc);
327 ret = -1;
328 goto end;
329 }
330
331 g_ptr_array_index(*fields, i) = field;
332 }
333
334 end:
335 return ret;
336 }
337
338 static
339 struct bt_field *create_structure_field(struct bt_field_class *fc)
340 {
341 struct bt_field_structure *struct_field;
342
343 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc);
344 struct_field = g_new0(struct bt_field_structure, 1);
345 if (!struct_field) {
346 BT_LOGE_STR("Failed to allocate one structure field.");
347 goto end;
348 }
349
350 init_field((void *) struct_field, fc, &structure_field_methods);
351
352 if (create_fields_from_named_field_classes((void *) fc,
353 &struct_field->fields)) {
354 BT_LIB_LOGE("Cannot create structure member fields: "
355 "%![fc-]+F", fc);
356 BT_OBJECT_PUT_REF_AND_RESET(struct_field);
357 goto end;
358 }
359
360 BT_LIB_LOGD("Created structure field object: %!+f", struct_field);
361
362 end:
363 return (void *) struct_field;
364 }
365
366 static
367 struct bt_field *create_variant_field(struct bt_field_class *fc)
368 {
369 struct bt_field_variant *var_field;
370
371 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc);
372 var_field = g_new0(struct bt_field_variant, 1);
373 if (!var_field) {
374 BT_LOGE_STR("Failed to allocate one variant field.");
375 goto end;
376 }
377
378 init_field((void *) var_field, fc, &variant_field_methods);
379
380 if (create_fields_from_named_field_classes((void *) fc,
381 &var_field->fields)) {
382 BT_LIB_LOGE("Cannot create variant member fields: "
383 "%![fc-]+F", fc);
384 BT_OBJECT_PUT_REF_AND_RESET(var_field);
385 goto end;
386 }
387
388 BT_LIB_LOGD("Created variant field object: %!+f", var_field);
389
390 end:
391 return (void *) var_field;
392 }
393
394 static inline
395 int init_array_field_fields(struct bt_field_array *array_field)
396 {
397 int ret = 0;
398 uint64_t i;
399 struct bt_field_class_array *array_fc;
400
401 BT_ASSERT(array_field);
402 array_fc = (void *) array_field->common.class;
403 array_field->fields = g_ptr_array_sized_new(array_field->length);
404 if (!array_field->fields) {
405 BT_LOGE_STR("Failed to allocate a GPtrArray.");
406 ret = -1;
407 goto end;
408 }
409
410 g_ptr_array_set_free_func(array_field->fields,
411 (GDestroyNotify) bt_field_destroy);
412 g_ptr_array_set_size(array_field->fields, array_field->length);
413
414 for (i = 0; i < array_field->length; i++) {
415 array_field->fields->pdata[i] = bt_field_create(
416 array_fc->element_fc);
417 if (!array_field->fields->pdata[i]) {
418 BT_LIB_LOGE("Cannot create array field's element field: "
419 "index=%" PRIu64 ", %![fc-]+F", i, array_fc);
420 ret = -1;
421 goto end;
422 }
423 }
424
425 end:
426 return ret;
427 }
428
429 static
430 struct bt_field *create_static_array_field(struct bt_field_class *fc)
431 {
432 struct bt_field_class_static_array *array_fc = (void *) fc;
433 struct bt_field_array *array_field;
434
435 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc);
436 array_field = g_new0(struct bt_field_array, 1);
437 if (!array_field) {
438 BT_LOGE_STR("Failed to allocate one static array field.");
439 goto end;
440 }
441
442 init_field((void *) array_field, fc, &array_field_methods);
443 array_field->length = array_fc->length;
444
445 if (init_array_field_fields(array_field)) {
446 BT_LIB_LOGE("Cannot create static array fields: "
447 "%![fc-]+F", fc);
448 BT_OBJECT_PUT_REF_AND_RESET(array_field);
449 goto end;
450 }
451
452 BT_LIB_LOGD("Created static array field object: %!+f", array_field);
453
454 end:
455 return (void *) array_field;
456 }
457
458 static
459 struct bt_field *create_dynamic_array_field(struct bt_field_class *fc)
460 {
461 struct bt_field_array *array_field;
462
463 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc);
464 array_field = g_new0(struct bt_field_array, 1);
465 if (!array_field) {
466 BT_LOGE_STR("Failed to allocate one dynamic array field.");
467 goto end;
468 }
469
470 init_field((void *) array_field, fc, &array_field_methods);
471
472 if (init_array_field_fields(array_field)) {
473 BT_LIB_LOGE("Cannot create dynamic array fields: "
474 "%![fc-]+F", fc);
475 BT_OBJECT_PUT_REF_AND_RESET(array_field);
476 goto end;
477 }
478
479 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field);
480
481 end:
482 return (void *) array_field;
483 }
484
485 int64_t bt_field_signed_integer_get_value(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
495 void 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
512 uint64_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
522 void 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
539 double 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
549 void 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
567 int 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
583 int 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
599 const 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
610 uint64_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
621 int 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
636 int 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
643 int 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
677 int 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
691 uint64_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
700 int 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
741 end:
742 return ret;
743 }
744
745 struct 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
756 struct bt_private_field *
757 bt_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
764 struct 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
776 struct bt_private_field *
777 bt_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
784 struct 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
807 end:
808 return ret_field;
809 }
810
811 struct bt_private_field *
812 bt_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
819 struct 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
832 struct bt_private_field *
833 bt_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
840 int 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
856 uint64_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
869 static inline
870 void 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_AND_RESET(field->class);
875 }
876
877 static
878 void 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
886 static
887 void 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
895 static
896 void 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 struct_field->fields = NULL;
907 }
908
909 g_free(field);
910 }
911
912 static
913 void destroy_variant_field(struct bt_field *field)
914 {
915 struct bt_field_variant *var_field = (void *) field;
916
917 BT_ASSERT(field);
918 BT_LIB_LOGD("Destroying variant field object: %!+f", field);
919 bt_field_finalize(field);
920
921 if (var_field->fields) {
922 g_ptr_array_free(var_field->fields, TRUE);
923 var_field->fields = NULL;
924 }
925
926 g_free(field);
927 }
928
929 static
930 void destroy_array_field(struct bt_field *field)
931 {
932 struct bt_field_array *array_field = (void *) field;
933
934 BT_ASSERT(field);
935 BT_LIB_LOGD("Destroying array field object: %!+f", field);
936 bt_field_finalize(field);
937
938 if (array_field->fields) {
939 g_ptr_array_free(array_field->fields, TRUE);
940 array_field->fields = NULL;
941 }
942
943 g_free(field);
944 }
945
946 static
947 void destroy_string_field(struct bt_field *field)
948 {
949 struct bt_field_string *string_field = (void *) field;
950
951 BT_ASSERT(field);
952 BT_LIB_LOGD("Destroying string field object: %!+f", field);
953 bt_field_finalize(field);
954
955 if (string_field->buf) {
956 g_array_free(string_field->buf, TRUE);
957 string_field->buf = NULL;
958 }
959
960 g_free(field);
961 }
962
963 BT_HIDDEN
964 void bt_field_destroy(struct bt_field *field)
965 {
966 BT_ASSERT(field);
967 BT_ASSERT(bt_field_class_has_known_type(field->class));
968 field_destroy_funcs[field->class->type](field);
969 }
970
971 static
972 void reset_single_field(struct bt_field *field)
973 {
974 BT_ASSERT(field);
975 field->is_set = false;
976 }
977
978 static
979 void reset_structure_field(struct bt_field *field)
980 {
981 uint64_t i;
982 struct bt_field_structure *struct_field = (void *) field;
983
984 BT_ASSERT(field);
985
986 for (i = 0; i < struct_field->fields->len; i++) {
987 bt_field_reset(struct_field->fields->pdata[i]);
988 }
989 }
990
991 static
992 void reset_variant_field(struct bt_field *field)
993 {
994 uint64_t i;
995 struct bt_field_variant *var_field = (void *) field;
996
997 BT_ASSERT(field);
998
999 for (i = 0; i < var_field->fields->len; i++) {
1000 bt_field_reset(var_field->fields->pdata[i]);
1001 }
1002 }
1003
1004 static
1005 void reset_array_field(struct bt_field *field)
1006 {
1007 uint64_t i;
1008 struct bt_field_array *array_field = (void *) field;
1009
1010 BT_ASSERT(field);
1011
1012 for (i = 0; i < array_field->fields->len; i++) {
1013 bt_field_reset(array_field->fields->pdata[i]);
1014 }
1015 }
1016
1017 static
1018 void set_single_field_is_frozen(struct bt_field *field, bool is_frozen)
1019 {
1020 field->frozen = is_frozen;
1021 }
1022
1023 static
1024 void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen)
1025 {
1026 uint64_t i;
1027 struct bt_field_structure *struct_field = (void *) field;
1028
1029 BT_LIB_LOGD("Setting structure field's frozen state: "
1030 "%![field-]+f, is-frozen=%d", field, is_frozen);
1031
1032 for (i = 0; i < struct_field->fields->len; i++) {
1033 struct bt_field *member_field = struct_field->fields->pdata[i];
1034
1035 BT_LIB_LOGD("Setting structure field's member field's "
1036 "frozen state: %![field-]+f, index=%" PRIu64,
1037 member_field, i);
1038 bt_field_set_is_frozen(member_field, is_frozen);
1039 }
1040
1041 set_single_field_is_frozen(field, is_frozen);
1042 }
1043
1044 static
1045 void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen)
1046 {
1047 uint64_t i;
1048 struct bt_field_variant *var_field = (void *) field;
1049
1050 BT_LIB_LOGD("Setting variant field's frozen state: "
1051 "%![field-]+f, is-frozen=%d", field, is_frozen);
1052
1053 for (i = 0; i < var_field->fields->len; i++) {
1054 struct bt_field *option_field = var_field->fields->pdata[i];
1055
1056 BT_LIB_LOGD("Setting variant field's option field's "
1057 "frozen state: %![field-]+f, index=%" PRIu64,
1058 option_field, i);
1059 bt_field_set_is_frozen(option_field, is_frozen);
1060 }
1061
1062 set_single_field_is_frozen(field, is_frozen);
1063 }
1064
1065 static
1066 void set_array_field_is_frozen(struct bt_field *field, bool is_frozen)
1067 {
1068 uint64_t i;
1069 struct bt_field_array *array_field = (void *) field;
1070
1071 BT_LIB_LOGD("Setting array field's frozen state: "
1072 "%![field-]+f, is-frozen=%d", field, is_frozen);
1073
1074 for (i = 0; i < array_field->fields->len; i++) {
1075 struct bt_field *elem_field = array_field->fields->pdata[i];
1076
1077 BT_LIB_LOGD("Setting array field's element field's "
1078 "frozen state: %![field-]+f, index=%" PRIu64,
1079 elem_field, i);
1080 bt_field_set_is_frozen(elem_field, is_frozen);
1081 }
1082
1083 set_single_field_is_frozen(field, is_frozen);
1084 }
1085
1086 BT_HIDDEN
1087 void _bt_field_set_is_frozen(struct bt_field *field,
1088 bool is_frozen)
1089 {
1090 BT_ASSERT(field);
1091 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1092 field, is_frozen);
1093 BT_ASSERT(field->methods->set_is_frozen);
1094 field->methods->set_is_frozen(field, is_frozen);
1095 }
1096
1097 static
1098 bool single_field_is_set(struct bt_field *field)
1099 {
1100 BT_ASSERT(field);
1101 return field->is_set;
1102 }
1103
1104 static
1105 bool structure_field_is_set(struct bt_field *field)
1106 {
1107 bool is_set = true;
1108 uint64_t i;
1109 struct bt_field_structure *struct_field = (void *) field;
1110
1111 BT_ASSERT(field);
1112
1113 for (i = 0; i < struct_field->fields->len; i++) {
1114 is_set = bt_field_is_set(struct_field->fields->pdata[i]);
1115 if (!is_set) {
1116 goto end;
1117 }
1118 }
1119
1120 end:
1121 return is_set;
1122 }
1123
1124 static
1125 bool variant_field_is_set(struct bt_field *field)
1126 {
1127 struct bt_field_variant *var_field = (void *) field;
1128 bool is_set = false;
1129
1130 BT_ASSERT(field);
1131
1132 if (var_field->selected_field) {
1133 is_set = bt_field_is_set(var_field->selected_field);
1134 }
1135
1136 return is_set;
1137 }
1138
1139 static
1140 bool array_field_is_set(struct bt_field *field)
1141 {
1142 bool is_set = true;
1143 uint64_t i;
1144 struct bt_field_array *array_field = (void *) field;
1145
1146 BT_ASSERT(field);
1147
1148 for (i = 0; i < array_field->length; i++) {
1149 is_set = bt_field_is_set(array_field->fields->pdata[i]);
1150 if (!is_set) {
1151 goto end;
1152 }
1153 }
1154
1155 end:
1156 return is_set;
1157 }
This page took 0.058275 seconds and 4 git commands to generate.