Use "growing" `GArray` to store string field's payload
[babeltrace.git] / lib / ctf-ir / fields.c
1 /*
2 * fields.c
3 *
4 * Babeltrace CTF IR - Event Fields
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "FIELDS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/fields-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/compat/fcntl-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <inttypes.h>
42
43 #define BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(_field, _name) \
44 BT_ASSERT_PRE((_field)->type->id == BT_FIELD_TYPE_ID_INTEGER || \
45 (_field)->type->id == BT_FIELD_TYPE_ID_ENUM, \
46 _name " is not an integer or an enumeration field: " \
47 "%!+f", (_field))
48
49 static struct bt_field_common_methods bt_field_integer_methods = {
50 .set_is_frozen = bt_field_common_generic_set_is_frozen,
51 .validate = bt_field_common_generic_validate,
52 .copy = NULL,
53 .is_set = bt_field_common_generic_is_set,
54 .reset = bt_field_common_generic_reset,
55 };
56
57 static struct bt_field_common_methods bt_field_floating_point_methods = {
58 .set_is_frozen = bt_field_common_generic_set_is_frozen,
59 .validate = bt_field_common_generic_validate,
60 .copy = NULL,
61 .is_set = bt_field_common_generic_is_set,
62 .reset = bt_field_common_generic_reset,
63 };
64
65 static struct bt_field_common_methods bt_field_enumeration_methods = {
66 .set_is_frozen = bt_field_common_generic_set_is_frozen,
67 .validate = bt_field_common_generic_validate,
68 .copy = NULL,
69 .is_set = bt_field_common_generic_is_set,
70 .reset = bt_field_common_generic_reset,
71 };
72
73 static struct bt_field_common_methods bt_field_string_methods = {
74 .set_is_frozen = bt_field_common_generic_set_is_frozen,
75 .validate = bt_field_common_generic_validate,
76 .copy = NULL,
77 .is_set = bt_field_common_generic_is_set,
78 .reset = bt_field_common_generic_reset,
79 };
80
81 static struct bt_field_common_methods bt_field_structure_methods = {
82 .set_is_frozen = bt_field_common_structure_set_is_frozen_recursive,
83 .validate = bt_field_common_structure_validate_recursive,
84 .copy = NULL,
85 .is_set = bt_field_common_structure_is_set_recursive,
86 .reset = bt_field_common_structure_reset_recursive,
87 };
88
89 static struct bt_field_common_methods bt_field_sequence_methods = {
90 .set_is_frozen = bt_field_common_sequence_set_is_frozen_recursive,
91 .validate = bt_field_common_sequence_validate_recursive,
92 .copy = NULL,
93 .is_set = bt_field_common_sequence_is_set_recursive,
94 .reset = bt_field_common_sequence_reset_recursive,
95 };
96
97 static struct bt_field_common_methods bt_field_array_methods = {
98 .set_is_frozen = bt_field_common_array_set_is_frozen_recursive,
99 .validate = bt_field_common_array_validate_recursive,
100 .copy = NULL,
101 .is_set = bt_field_common_array_is_set_recursive,
102 .reset = bt_field_common_array_reset_recursive,
103 };
104
105 static struct bt_field_common_methods bt_field_variant_methods = {
106 .set_is_frozen = bt_field_common_variant_set_is_frozen_recursive,
107 .validate = bt_field_common_variant_validate_recursive,
108 .copy = NULL,
109 .is_set = bt_field_common_variant_is_set_recursive,
110 .reset = bt_field_common_variant_reset_recursive,
111 };
112
113 static
114 struct bt_field *bt_field_integer_create(struct bt_field_type *);
115
116 static
117 struct bt_field *bt_field_enumeration_create(struct bt_field_type *);
118
119 static
120 struct bt_field *bt_field_floating_point_create(struct bt_field_type *);
121
122 static
123 struct bt_field *bt_field_structure_create(struct bt_field_type *);
124
125 static
126 struct bt_field *bt_field_variant_create(struct bt_field_type *);
127
128 static
129 struct bt_field *bt_field_array_create(struct bt_field_type *);
130
131 static
132 struct bt_field *bt_field_sequence_create(struct bt_field_type *);
133
134 static
135 struct bt_field *bt_field_string_create(struct bt_field_type *);
136
137 static
138 struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = {
139 [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_create,
140 [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_create,
141 [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_create,
142 [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_create,
143 [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_create,
144 [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_create,
145 [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_create,
146 [BT_FIELD_TYPE_ID_STRING] = bt_field_string_create,
147 };
148
149 static
150 void bt_field_integer_destroy(struct bt_field *field);
151
152 static
153 void bt_field_enumeration_destroy(struct bt_field *field);
154
155 static
156 void bt_field_floating_point_destroy(struct bt_field *field);
157
158 static
159 void bt_field_structure_destroy_recursive(struct bt_field *field);
160
161 static
162 void bt_field_variant_destroy_recursive(struct bt_field *field);
163
164 static
165 void bt_field_array_destroy_recursive(struct bt_field *field);
166
167 static
168 void bt_field_sequence_destroy_recursive(struct bt_field *field);
169
170 static
171 void bt_field_string_destroy(struct bt_field *field);
172
173 static
174 void (* const field_destroy_funcs[])(struct bt_field *) = {
175 [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_destroy,
176 [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_destroy,
177 [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_destroy,
178 [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_destroy_recursive,
179 [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_destroy_recursive,
180 [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_destroy_recursive,
181 [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_destroy_recursive,
182 [BT_FIELD_TYPE_ID_STRING] = bt_field_string_destroy,
183 };
184
185 BT_HIDDEN
186 struct bt_field *bt_field_create_recursive(struct bt_field_type *type)
187 {
188 struct bt_field *field = NULL;
189 enum bt_field_type_id type_id;
190
191 BT_ASSERT_PRE_NON_NULL(type, "Field type");
192 BT_ASSERT(field_type_common_has_known_id((void *) type));
193 BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0,
194 "Field type is invalid: %!+F", type);
195 type_id = bt_field_type_get_type_id(type);
196 field = field_create_funcs[type_id](type);
197 if (!field) {
198 goto end;
199 }
200
201 bt_object_set_is_shared((void *) field, false);
202 bt_field_type_freeze(type);
203
204 end:
205 return field;
206 }
207
208 struct bt_field_type *bt_field_borrow_type(struct bt_field *field)
209 {
210 return (void *) bt_field_common_borrow_type((void *) field);
211 }
212
213 enum bt_field_type_id bt_field_get_type_id(struct bt_field *field)
214 {
215 struct bt_field_common *field_common = (void *) field;
216
217 BT_ASSERT_PRE_NON_NULL(field, "Field");
218 return field_common->type->id;
219 }
220
221 bt_bool bt_field_is_integer(struct bt_field *field)
222 {
223 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_INTEGER;
224 }
225
226 bt_bool bt_field_is_floating_point(struct bt_field *field)
227 {
228 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_FLOAT;
229 }
230
231 bt_bool bt_field_is_enumeration(struct bt_field *field)
232 {
233 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ENUM;
234 }
235
236 bt_bool bt_field_is_string(struct bt_field *field)
237 {
238 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING;
239 }
240
241 bt_bool bt_field_is_structure(struct bt_field *field)
242 {
243 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRUCT;
244 }
245
246 bt_bool bt_field_is_array(struct bt_field *field)
247 {
248 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ARRAY;
249 }
250
251 bt_bool bt_field_is_sequence(struct bt_field *field)
252 {
253 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SEQUENCE;
254 }
255
256 bt_bool bt_field_is_variant(struct bt_field *field)
257 {
258 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_VARIANT;
259 }
260
261 int64_t bt_field_sequence_get_length(struct bt_field *field)
262 {
263 return bt_field_common_sequence_get_length((void *) field);
264 }
265
266 int bt_field_sequence_set_length(struct bt_field *field, uint64_t length)
267 {
268 return bt_field_common_sequence_set_length((void *) field,
269 length, (bt_field_common_create_func) bt_field_create_recursive);
270 }
271
272 struct bt_field *bt_field_structure_borrow_field_by_index(
273 struct bt_field *field, uint64_t index)
274 {
275 return (void *) bt_field_common_structure_borrow_field_by_index(
276 (void *) field, index);
277 }
278
279 struct bt_field *bt_field_structure_borrow_field_by_name(
280 struct bt_field *field, const char *name)
281 {
282 return (void *) bt_field_common_structure_borrow_field_by_name(
283 (void *) field, name);
284 }
285
286 struct bt_field *bt_field_array_borrow_field(
287 struct bt_field *field, uint64_t index)
288 {
289 return (void *) bt_field_common_array_borrow_field((void *) field,
290 index);
291 }
292
293 struct bt_field *bt_field_sequence_borrow_field(
294 struct bt_field *field, uint64_t index)
295 {
296 return (void *) bt_field_common_sequence_borrow_field((void *) field,
297 index);
298 }
299
300 struct bt_field *bt_field_variant_borrow_current_field(
301 struct bt_field *variant_field)
302 {
303 return (void *) bt_field_common_variant_borrow_current_field(
304 (void *) variant_field);
305 }
306
307 int bt_field_variant_set_tag_signed(struct bt_field *variant_field,
308 int64_t tag)
309 {
310 return bt_field_variant_common_set_tag((void *) variant_field,
311 (uint64_t) tag, true);
312 }
313
314 int bt_field_variant_set_tag_unsigned(struct bt_field *variant_field,
315 uint64_t tag)
316 {
317 return bt_field_variant_common_set_tag((void *) variant_field,
318 (uint64_t) tag, false);
319 }
320
321 int bt_field_variant_get_tag_signed(struct bt_field *variant_field,
322 int64_t *tag)
323 {
324 return bt_field_common_variant_get_tag_signed((void *) variant_field, tag);
325 }
326
327 int bt_field_variant_get_tag_unsigned(struct bt_field *variant_field,
328 uint64_t *tag)
329 {
330 return bt_field_common_variant_get_tag_unsigned((void *) variant_field, tag);
331 }
332
333 struct bt_field_type_enumeration_mapping_iterator *
334 bt_field_enumeration_get_mappings(struct bt_field *field)
335 {
336 struct bt_field_enumeration *enum_field = (void *) field;
337
338 BT_ASSERT_PRE_NON_NULL(field, "Enumeration field");
339 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID((struct bt_field_common *) field,
340 BT_FIELD_TYPE_ID_ENUM, "Field");
341 BT_ASSERT_PRE_FIELD_COMMON_IS_SET((struct bt_field_common *) field,
342 "Enumeration field");
343 return bt_field_common_enumeration_get_mappings((void *) field,
344 (bt_field_common_create_func) bt_field_create_recursive,
345 enum_field->common.payload.unsignd);
346 }
347
348 BT_ASSERT_PRE_FUNC
349 static inline
350 struct bt_field_type_common_integer *get_int_enum_int_ft(
351 struct bt_field *field)
352 {
353 struct bt_field_common_integer *int_field = (void *) field;
354 struct bt_field_type_common_integer *int_ft = NULL;
355
356 if (int_field->common.type->id == BT_FIELD_TYPE_ID_INTEGER) {
357 int_ft = BT_FROM_COMMON(int_field->common.type);
358 } else if (int_field->common.type->id == BT_FIELD_TYPE_ID_ENUM) {
359 struct bt_field_type_common_enumeration *enum_ft =
360 BT_FROM_COMMON(int_field->common.type);
361 int_ft = enum_ft->container_ft;
362 }
363
364 BT_ASSERT(int_ft);
365 return int_ft;
366 }
367
368 int bt_field_integer_signed_get_value(struct bt_field *field, int64_t *value)
369 {
370 struct bt_field_common_integer *integer = (void *) field;
371
372 BT_ASSERT_PRE_NON_NULL(field, "Integer/enumeration field");
373 BT_ASSERT_PRE_NON_NULL(value, "Value");
374 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer),
375 "Integer/enumeration field");
376 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
377 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
378 BT_TO_COMMON(get_int_enum_int_ft(field))),
379 "Field's type is unsigned: %!+f", field);
380 *value = integer->payload.signd;
381 return 0;
382 }
383
384 int bt_field_integer_signed_set_value(struct bt_field *field, int64_t value)
385 {
386 int ret = 0;
387 struct bt_field_common_integer *integer = (void *) field;
388
389 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
390 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field");
391 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
392 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
393 BT_FROM_COMMON(get_int_enum_int_ft(field))),
394 "Field's type is unsigned: %!+f", field);
395 BT_ASSERT_PRE(value_is_in_range_signed(
396 get_int_enum_int_ft(field)->size, value),
397 "Value is out of bounds: value=%" PRId64 ", %![field-]+f",
398 value, field);
399 integer->payload.signd = value;
400 bt_field_set(field, true);
401 return ret;
402 }
403
404 int bt_field_integer_unsigned_get_value(struct bt_field *field, uint64_t *value)
405 {
406 struct bt_field_common_integer *integer = (void *) field;
407
408 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
409 BT_ASSERT_PRE_NON_NULL(value, "Value");
410 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer), "Integer field");
411 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
412 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
413 BT_FROM_COMMON(get_int_enum_int_ft(field))),
414 "Field's type is signed: %!+f", field);
415 *value = integer->payload.unsignd;
416 return 0;
417 }
418
419 int bt_field_integer_unsigned_set_value(struct bt_field *field,
420 uint64_t value)
421 {
422 struct bt_field_common_integer *integer = (void *) field;
423
424 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
425 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field");
426 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
427 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
428 BT_FROM_COMMON(get_int_enum_int_ft(field))),
429 "Field's type is signed: %!+f", field);
430 BT_ASSERT_PRE(value_is_in_range_unsigned(
431 get_int_enum_int_ft(field)->size, value),
432 "Value is out of bounds: value=%" PRIu64 ", %![field-]+f",
433 value, field);
434 integer->payload.unsignd = value;
435 bt_field_set(field, true);
436 return 0;
437 }
438
439 int bt_field_floating_point_get_value(struct bt_field *field,
440 double *value)
441 {
442 return bt_field_common_floating_point_get_value((void *) field, value);
443 }
444
445 int bt_field_floating_point_set_value(struct bt_field *field,
446 double value)
447 {
448 return bt_field_common_floating_point_set_value((void *) field, value);
449 }
450
451 const char *bt_field_string_get_value(struct bt_field *field)
452 {
453 return bt_field_common_string_get_value((void *) field);
454 }
455
456 int bt_field_string_set_value(struct bt_field *field, const char *value)
457 {
458 return bt_field_common_string_set_value((void *) field, value);
459 }
460
461 int bt_field_string_append(struct bt_field *field, const char *value)
462 {
463 return bt_field_common_string_append((void *) field, value);
464 }
465
466 int bt_field_string_append_len(struct bt_field *field,
467 const char *value, unsigned int length)
468 {
469 return bt_field_common_string_append_len((void *) field, value, length);
470 }
471
472 int bt_field_string_clear(struct bt_field *string_field)
473 {
474 return bt_field_common_string_clear((void *) string_field);
475 }
476
477 BT_HIDDEN
478 struct bt_field_common *bt_field_common_copy(struct bt_field_common *field)
479 {
480 struct bt_field_common *copy = NULL;
481
482 BT_ASSERT_PRE_NON_NULL(field, "Field");
483 BT_ASSERT(field_type_common_has_known_id(field->type));
484 BT_ASSERT(field->methods->copy);
485 copy = field->methods->copy(field);
486 if (!copy) {
487 BT_LOGW("Cannot create field: ft-addr=%p", field->type);
488 goto end;
489 }
490
491 bt_field_common_set(copy, field->payload_set);
492
493 end:
494 return copy;
495 }
496
497 static
498 void bt_field_integer_destroy(struct bt_field *field)
499 {
500 BT_LOGD("Destroying integer field object: addr=%p", field);
501 bt_field_common_integer_finalize((void *) field);
502 g_free(field);
503 }
504
505 static
506 void bt_field_floating_point_destroy(struct bt_field *field)
507 {
508 BT_LOGD("Destroying floating point field object: addr=%p", field);
509 bt_field_common_floating_point_finalize((void *) field);
510 g_free(field);
511 }
512
513 static
514 void bt_field_enumeration_destroy(struct bt_field *field)
515 {
516 BT_LOGD("Destroying enumeration field object: addr=%p", field);
517 bt_field_common_finalize((void *) field);
518 g_free(field);
519 }
520
521 static
522 void bt_field_structure_destroy_recursive(struct bt_field *field)
523 {
524 BT_LOGD("Destroying structure field object: addr=%p", field);
525 bt_field_common_structure_finalize_recursive((void *) field);
526 g_free(field);
527 }
528
529 static
530 void bt_field_variant_destroy_recursive(struct bt_field *field)
531 {
532 BT_LOGD("Destroying variant field object: addr=%p", field);
533 bt_field_common_variant_finalize_recursive((void *) field);
534 g_free(field);
535 }
536
537 static
538 void bt_field_array_destroy_recursive(struct bt_field *field)
539 {
540 BT_LOGD("Destroying array field object: addr=%p", field);
541 bt_field_common_array_finalize_recursive((void *) field);
542 g_free(field);
543 }
544
545 static
546 void bt_field_sequence_destroy_recursive(struct bt_field *field)
547 {
548 BT_LOGD("Destroying sequence field object: addr=%p", field);
549 bt_field_common_sequence_finalize_recursive((void *) field);
550 g_free(field);
551 }
552
553 static
554 void bt_field_string_destroy(struct bt_field *field)
555 {
556 BT_LOGD("Destroying string field object: addr=%p", field);
557 bt_field_common_string_finalize((void *) field);
558 g_free(field);
559 }
560
561 BT_HIDDEN
562 void bt_field_destroy_recursive(struct bt_field *field)
563 {
564 struct bt_field_common *field_common = (void *) field;
565
566 if (!field) {
567 return;
568 }
569
570 BT_ASSERT(field_type_common_has_known_id((void *) field_common->type));
571 field_destroy_funcs[field_common->type->id](field);
572 }
573
574 static
575 struct bt_field *bt_field_integer_create(struct bt_field_type *type)
576 {
577 struct bt_field_common_integer *integer =
578 g_new0(struct bt_field_common_integer, 1);
579
580 BT_LOGD("Creating integer field object: ft-addr=%p", type);
581
582 if (integer) {
583 bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type,
584 NULL, &bt_field_integer_methods);
585 BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
586 integer, type);
587 } else {
588 BT_LOGE_STR("Failed to allocate one integer field.");
589 }
590
591 return (void *) integer;
592 }
593
594 static
595 struct bt_field *bt_field_enumeration_create(struct bt_field_type *type)
596 {
597 struct bt_field_enumeration *enumeration = g_new0(
598 struct bt_field_enumeration, 1);
599
600 BT_LOGD("Creating enumeration field object: ft-addr=%p", type);
601
602 if (enumeration) {
603 bt_field_common_initialize(
604 BT_TO_COMMON(BT_TO_COMMON(enumeration)),
605 (void *) type, NULL, &bt_field_enumeration_methods);
606 BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
607 enumeration, type);
608 } else {
609 BT_LOGE_STR("Failed to allocate one enumeration field.");
610 }
611
612 return (void *) enumeration;
613 }
614
615 static
616 struct bt_field *bt_field_floating_point_create(struct bt_field_type *type)
617 {
618 struct bt_field_common_floating_point *floating_point;
619
620 BT_LOGD("Creating floating point number field object: ft-addr=%p", type);
621 floating_point = g_new0(struct bt_field_common_floating_point, 1);
622
623 if (floating_point) {
624 bt_field_common_initialize(BT_TO_COMMON(floating_point),
625 (void *) type, NULL, &bt_field_floating_point_methods);
626 BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
627 floating_point, type);
628 } else {
629 BT_LOGE_STR("Failed to allocate one floating point number field.");
630 }
631
632 return (void *) floating_point;
633 }
634
635 BT_HIDDEN
636 int bt_field_common_structure_initialize(struct bt_field_common *field,
637 struct bt_field_type_common *type,
638 bt_object_release_func release_func,
639 struct bt_field_common_methods *methods,
640 bt_field_common_create_func field_create_func,
641 GDestroyNotify field_release_func)
642 {
643 int ret = 0;
644 struct bt_field_type_common_structure *structure_type =
645 BT_FROM_COMMON(type);
646 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
647 size_t i;
648
649 BT_LOGD("Initializing common structure field object: ft-addr=%p", type);
650 bt_field_common_initialize(field, type, release_func, methods);
651 structure->fields = g_ptr_array_new_with_free_func(field_release_func);
652 g_ptr_array_set_size(structure->fields, structure_type->fields->len);
653
654 /* Create all fields contained in the structure field. */
655 for (i = 0; i < structure_type->fields->len; i++) {
656 struct bt_field_common *field;
657 struct bt_field_type_common_structure_field *struct_field =
658 BT_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
659 structure_type, i);
660 field = field_create_func(struct_field->type);
661 if (!field) {
662 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
663 g_quark_to_string(struct_field->name), i);
664 ret = -1;
665 goto end;
666 }
667
668 g_ptr_array_index(structure->fields, i) = field;
669 }
670
671 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
672 field, type);
673
674 end:
675 return ret;
676 }
677
678 static
679 struct bt_field *bt_field_structure_create(struct bt_field_type *type)
680 {
681 struct bt_field_common_structure *structure = g_new0(
682 struct bt_field_common_structure, 1);
683 int iret;
684
685 BT_LOGD("Creating structure field object: ft-addr=%p", type);
686
687 if (!structure) {
688 BT_LOGE_STR("Failed to allocate one structure field.");
689 goto end;
690 }
691
692 iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure),
693 (void *) type, NULL, &bt_field_structure_methods,
694 (bt_field_common_create_func) bt_field_create_recursive,
695 (GDestroyNotify) bt_field_destroy_recursive);
696 if (iret) {
697 BT_PUT(structure);
698 goto end;
699 }
700
701 BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
702 structure, type);
703
704 end:
705 return (void *) structure;
706 }
707
708 BT_HIDDEN
709 int bt_field_common_variant_initialize(struct bt_field_common *field,
710 struct bt_field_type_common *type,
711 bt_object_release_func release_func,
712 struct bt_field_common_methods *methods,
713 bt_field_common_create_func field_create_func,
714 GDestroyNotify field_release_func)
715 {
716 int ret = 0;
717 struct bt_field_type_common_variant *variant_type =
718 BT_FROM_COMMON(type);
719 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
720 size_t i;
721
722 BT_LOGD("Initializing common variant field object: ft-addr=%p", type);
723 bt_field_common_initialize(field, type, release_func, methods);
724 ret = bt_field_type_common_variant_update_choices(type);
725 if (ret) {
726 BT_LOGE("Cannot update common variant field type choices: "
727 "ret=%d", ret);
728 goto end;
729 }
730
731 variant->fields = g_ptr_array_new_with_free_func(field_release_func);
732 g_ptr_array_set_size(variant->fields, variant_type->choices->len);
733
734 /* Create all fields contained in the variant field. */
735 for (i = 0; i < variant_type->choices->len; i++) {
736 struct bt_field_common *field;
737 struct bt_field_type_common_variant_choice *var_choice =
738 BT_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
739 variant_type, i);
740
741 field = field_create_func(var_choice->type);
742 if (!field) {
743 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
744 g_quark_to_string(var_choice->name), i);
745 ret = -1;
746 goto end;
747 }
748
749 g_ptr_array_index(variant->fields, i) = field;
750 }
751
752 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
753 field, type);
754
755 end:
756 return ret;
757 }
758
759 BT_HIDDEN
760 int bt_field_common_string_initialize(struct bt_field_common *field,
761 struct bt_field_type_common *type,
762 bt_object_release_func release_func,
763 struct bt_field_common_methods *methods)
764 {
765 int ret = 0;
766 struct bt_field_common_string *string = BT_FROM_COMMON(field);
767
768 BT_LOGD("Initializing common string field object: ft-addr=%p", type);
769 bt_field_common_initialize(field, type, release_func, methods);
770 string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1);
771 if (!string->buf) {
772 ret = -1;
773 goto end;
774 }
775
776 g_array_index(string->buf, char, 0) = '\0';
777 BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
778 field, type);
779
780 end:
781 return ret;
782 }
783
784 static
785 struct bt_field *bt_field_variant_create(struct bt_field_type *type)
786 {
787 struct bt_field_common_variant *variant = g_new0(
788 struct bt_field_common_variant, 1);
789 int iret;
790
791 BT_LOGD("Creating variant field object: ft-addr=%p", type);
792
793 if (!variant) {
794 BT_LOGE_STR("Failed to allocate one variant field.");
795 goto end;
796 }
797
798 iret = bt_field_common_variant_initialize(BT_TO_COMMON(variant),
799 (void *) type, NULL, &bt_field_variant_methods,
800 (bt_field_common_create_func) bt_field_create_recursive,
801 (GDestroyNotify) bt_field_destroy_recursive);
802 if (iret) {
803 BT_PUT(variant);
804 goto end;
805 }
806
807 BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
808 variant, type);
809
810 end:
811 return (void *) variant;
812 }
813
814 BT_HIDDEN
815 int bt_field_common_array_initialize(struct bt_field_common *field,
816 struct bt_field_type_common *type,
817 bt_object_release_func release_func,
818 struct bt_field_common_methods *methods,
819 bt_field_common_create_func field_create_func,
820 GDestroyNotify field_destroy_func)
821 {
822 struct bt_field_type_common_array *array_type = BT_FROM_COMMON(type);
823 struct bt_field_common_array *array = BT_FROM_COMMON(field);
824 unsigned int array_length;
825 int ret = 0;
826 uint64_t i;
827
828 BT_LOGD("Initializing common array field object: ft-addr=%p", type);
829 BT_ASSERT(type);
830 bt_field_common_initialize(field, type, release_func, methods);
831 array_length = array_type->length;
832 array->elements = g_ptr_array_sized_new(array_length);
833 if (!array->elements) {
834 ret = -1;
835 goto end;
836 }
837
838 g_ptr_array_set_free_func(array->elements, field_destroy_func);
839 g_ptr_array_set_size(array->elements, array_length);
840
841 for (i = 0; i < array_length; i++) {
842 array->elements->pdata[i] = field_create_func(
843 array_type->element_ft);
844 if (!array->elements->pdata[i]) {
845 ret = -1;
846 goto end;
847 }
848 }
849
850 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
851 field, type);
852
853 end:
854 return ret;
855 }
856
857 static
858 struct bt_field *bt_field_array_create(struct bt_field_type *type)
859 {
860 struct bt_field_common_array *array =
861 g_new0(struct bt_field_common_array, 1);
862 int ret;
863
864 BT_LOGD("Creating array field object: ft-addr=%p", type);
865 BT_ASSERT(type);
866
867 if (!array) {
868 BT_LOGE_STR("Failed to allocate one array field.");
869 goto end;
870 }
871
872 ret = bt_field_common_array_initialize(BT_TO_COMMON(array),
873 (void *) type, NULL, &bt_field_array_methods,
874 (bt_field_common_create_func) bt_field_create_recursive,
875 (GDestroyNotify) bt_field_destroy_recursive);
876 if (ret) {
877 BT_PUT(array);
878 goto end;
879 }
880
881 BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
882 array, type);
883
884 end:
885 return (void *) array;
886 }
887
888 BT_HIDDEN
889 int bt_field_common_sequence_initialize(struct bt_field_common *field,
890 struct bt_field_type_common *type,
891 bt_object_release_func release_func,
892 struct bt_field_common_methods *methods,
893 GDestroyNotify field_destroy_func)
894 {
895 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
896 int ret = 0;
897
898 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type);
899 BT_ASSERT(type);
900 bt_field_common_initialize(field, type, release_func, methods);
901 sequence->elements = g_ptr_array_new();
902 if (!sequence->elements) {
903 ret = -1;
904 goto end;
905 }
906
907 g_ptr_array_set_free_func(sequence->elements, field_destroy_func);
908 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
909 field, type);
910
911 end:
912 return ret;
913 }
914
915 static
916 struct bt_field *bt_field_sequence_create(struct bt_field_type *type)
917 {
918 struct bt_field_common_sequence *sequence =
919 g_new0(struct bt_field_common_sequence, 1);
920 int ret;
921
922 BT_LOGD("Creating sequence field object: ft-addr=%p", type);
923 BT_ASSERT(type);
924
925 if (!sequence) {
926 BT_LOGE_STR("Failed to allocate one sequence field.");
927 goto end;
928 }
929
930 ret = bt_field_common_sequence_initialize(BT_TO_COMMON(sequence),
931 (void *) type, NULL, &bt_field_sequence_methods,
932 (GDestroyNotify) bt_field_destroy_recursive);
933 if (ret) {
934 BT_PUT(sequence);
935 goto end;
936 }
937
938 BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
939 sequence, type);
940
941 end:
942 return (void *) sequence;
943 }
944
945 static
946 struct bt_field *bt_field_string_create(struct bt_field_type *type)
947 {
948 struct bt_field_common_string *string = g_new0(
949 struct bt_field_common_string, 1);
950
951 BT_LOGD("Creating string field object: ft-addr=%p", type);
952
953 if (string) {
954 bt_field_common_string_initialize(BT_TO_COMMON(string),
955 (void *) type, NULL, &bt_field_string_methods);
956 BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
957 string, type);
958 } else {
959 BT_LOGE_STR("Failed to allocate one string field.");
960 }
961
962 return (void *) string;
963 }
964
965 BT_HIDDEN
966 int bt_field_common_generic_validate(struct bt_field_common *field)
967 {
968 return (field && field->payload_set) ? 0 : -1;
969 }
970
971 BT_HIDDEN
972 int bt_field_common_structure_validate_recursive(struct bt_field_common *field)
973 {
974 int64_t i;
975 int ret = 0;
976 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
977
978 BT_ASSERT(field);
979
980 for (i = 0; i < structure->fields->len; i++) {
981 ret = bt_field_common_validate_recursive(
982 (void *) structure->fields->pdata[i]);
983
984 if (ret) {
985 int this_ret;
986 const char *name;
987
988 this_ret = bt_field_type_common_structure_borrow_field_by_index(
989 field->type, &name, NULL, i);
990 BT_ASSERT(this_ret == 0);
991 BT_ASSERT_PRE_MSG("Invalid structure field's field: "
992 "%![struct-field-]+_f, field-name=\"%s\", "
993 "index=%" PRId64 ", %![field-]+_f",
994 field, name, i, structure->fields->pdata[i]);
995 goto end;
996 }
997 }
998
999 end:
1000 return ret;
1001 }
1002
1003 BT_HIDDEN
1004 int bt_field_common_variant_validate_recursive(struct bt_field_common *field)
1005 {
1006 int ret = 0;
1007 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
1008
1009 BT_ASSERT(field);
1010
1011 if (!variant->current_field) {
1012 ret = -1;
1013 goto end;
1014 }
1015
1016 ret = bt_field_common_validate_recursive(variant->current_field);
1017
1018 end:
1019 return ret;
1020 }
1021
1022 BT_HIDDEN
1023 int bt_field_common_array_validate_recursive(struct bt_field_common *field)
1024 {
1025 int64_t i;
1026 int ret = 0;
1027 struct bt_field_common_array *array = BT_FROM_COMMON(field);
1028
1029 BT_ASSERT(field);
1030
1031 for (i = 0; i < array->elements->len; i++) {
1032 ret = bt_field_common_validate_recursive((void *) array->elements->pdata[i]);
1033 if (ret) {
1034 BT_ASSERT_PRE_MSG("Invalid array field's element field: "
1035 "%![array-field-]+_f, " PRId64 ", "
1036 "%![elem-field-]+_f",
1037 field, i, array->elements->pdata[i]);
1038 goto end;
1039 }
1040 }
1041
1042 end:
1043 return ret;
1044 }
1045
1046 BT_HIDDEN
1047 int bt_field_common_sequence_validate_recursive(struct bt_field_common *field)
1048 {
1049 size_t i;
1050 int ret = 0;
1051 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
1052
1053 BT_ASSERT(field);
1054
1055 for (i = 0; i < sequence->elements->len; i++) {
1056 ret = bt_field_common_validate_recursive(
1057 (void *) sequence->elements->pdata[i]);
1058 if (ret) {
1059 BT_ASSERT_PRE_MSG("Invalid sequence field's element field: "
1060 "%![seq-field-]+_f, " PRId64 ", "
1061 "%![elem-field-]+_f",
1062 field, i, sequence->elements->pdata[i]);
1063 goto end;
1064 }
1065 }
1066 end:
1067 return ret;
1068 }
1069
1070 BT_HIDDEN
1071 void bt_field_common_generic_reset(struct bt_field_common *field)
1072 {
1073 BT_ASSERT(field);
1074 field->payload_set = false;
1075 }
1076
1077 BT_HIDDEN
1078 void bt_field_common_structure_reset_recursive(struct bt_field_common *field)
1079 {
1080 int64_t i;
1081 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
1082
1083 BT_ASSERT(field);
1084
1085 for (i = 0; i < structure->fields->len; i++) {
1086 struct bt_field_common *member = structure->fields->pdata[i];
1087
1088 if (!member) {
1089 /*
1090 * Structure members are lazily initialized;
1091 * skip if this member has not been allocated
1092 * yet.
1093 */
1094 continue;
1095 }
1096
1097 bt_field_common_reset_recursive(member);
1098 }
1099 }
1100
1101 BT_HIDDEN
1102 void bt_field_common_variant_reset_recursive(struct bt_field_common *field)
1103 {
1104 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
1105
1106 BT_ASSERT(field);
1107 variant->current_field = NULL;
1108 }
1109
1110 BT_HIDDEN
1111 void bt_field_common_array_reset_recursive(struct bt_field_common *field)
1112 {
1113 size_t i;
1114 struct bt_field_common_array *array = BT_FROM_COMMON(field);
1115
1116 BT_ASSERT(field);
1117
1118 for (i = 0; i < array->elements->len; i++) {
1119 struct bt_field_common *member = array->elements->pdata[i];
1120
1121 if (!member) {
1122 /*
1123 * Array elements are lazily initialized; skip
1124 * if this member has not been allocated yet.
1125 */
1126 continue;
1127 }
1128
1129 bt_field_common_reset_recursive(member);
1130 }
1131 }
1132
1133 BT_HIDDEN
1134 void bt_field_common_sequence_reset_recursive(struct bt_field_common *field)
1135 {
1136 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
1137 uint64_t i;
1138
1139 BT_ASSERT(field);
1140
1141 for (i = 0; i < sequence->elements->len; i++) {
1142 if (sequence->elements->pdata[i]) {
1143 bt_field_common_reset_recursive(
1144 sequence->elements->pdata[i]);
1145 }
1146 }
1147
1148 sequence->length = 0;
1149 }
1150
1151 BT_HIDDEN
1152 void bt_field_common_generic_set_is_frozen(struct bt_field_common *field,
1153 bool is_frozen)
1154 {
1155 field->frozen = is_frozen;
1156 }
1157
1158 BT_HIDDEN
1159 void bt_field_common_structure_set_is_frozen_recursive(
1160 struct bt_field_common *field, bool is_frozen)
1161 {
1162 uint64_t i;
1163 struct bt_field_common_structure *structure_field =
1164 BT_FROM_COMMON(field);
1165
1166 BT_LOGD("Freezing structure field object: addr=%p", field);
1167
1168 for (i = 0; i < structure_field->fields->len; i++) {
1169 struct bt_field_common *struct_field =
1170 g_ptr_array_index(structure_field->fields, i);
1171
1172 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64,
1173 struct_field, i);
1174 bt_field_common_set_is_frozen_recursive(struct_field,
1175 is_frozen);
1176 }
1177
1178 bt_field_common_generic_set_is_frozen(field, is_frozen);
1179 }
1180
1181 BT_HIDDEN
1182 void bt_field_common_variant_set_is_frozen_recursive(
1183 struct bt_field_common *field, bool is_frozen)
1184 {
1185 uint64_t i;
1186 struct bt_field_common_variant *variant_field = BT_FROM_COMMON(field);
1187
1188 BT_LOGD("Freezing variant field object: addr=%p", field);
1189
1190 for (i = 0; i < variant_field->fields->len; i++) {
1191 struct bt_field_common *var_field =
1192 g_ptr_array_index(variant_field->fields, i);
1193
1194 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64,
1195 var_field, i);
1196 bt_field_common_set_is_frozen_recursive(var_field, is_frozen);
1197 }
1198
1199 bt_field_common_generic_set_is_frozen(field, is_frozen);
1200 }
1201
1202 BT_HIDDEN
1203 void bt_field_common_array_set_is_frozen_recursive(
1204 struct bt_field_common *field, bool is_frozen)
1205 {
1206 int64_t i;
1207 struct bt_field_common_array *array_field = BT_FROM_COMMON(field);
1208
1209 BT_LOGD("Freezing array field object: addr=%p", field);
1210
1211 for (i = 0; i < array_field->elements->len; i++) {
1212 struct bt_field_common *elem_field =
1213 g_ptr_array_index(array_field->elements, i);
1214
1215 BT_LOGD("Freezing array field object's element field: "
1216 "element-field-addr=%p, index=%" PRId64,
1217 elem_field, i);
1218 bt_field_common_set_is_frozen_recursive(elem_field, is_frozen);
1219 }
1220
1221 bt_field_common_generic_set_is_frozen(field, is_frozen);
1222 }
1223
1224 BT_HIDDEN
1225 void bt_field_common_sequence_set_is_frozen_recursive(
1226 struct bt_field_common *field, bool is_frozen)
1227 {
1228 int64_t i;
1229 struct bt_field_common_sequence *sequence_field =
1230 BT_FROM_COMMON(field);
1231
1232 BT_LOGD("Freezing sequence field object: addr=%p", field);
1233
1234 for (i = 0; i < sequence_field->length; i++) {
1235 struct bt_field_common *elem_field =
1236 g_ptr_array_index(sequence_field->elements, i);
1237
1238 BT_LOGD("Freezing sequence field object's element field: "
1239 "element-field-addr=%p, index=%" PRId64,
1240 elem_field, i);
1241 bt_field_common_set_is_frozen_recursive(elem_field, is_frozen);
1242 }
1243
1244 bt_field_common_generic_set_is_frozen(field, is_frozen);
1245 }
1246
1247 BT_HIDDEN
1248 void _bt_field_common_set_is_frozen_recursive(struct bt_field_common *field,
1249 bool is_frozen)
1250 {
1251 if (!field) {
1252 goto end;
1253 }
1254
1255 if (field->frozen) {
1256 goto end;
1257 }
1258
1259 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
1260 field, is_frozen);
1261 BT_ASSERT(field_type_common_has_known_id(field->type));
1262 BT_ASSERT(field->methods->set_is_frozen);
1263 field->methods->set_is_frozen(field, is_frozen);
1264
1265 end:
1266 return;
1267 }
1268
1269 BT_HIDDEN
1270 bt_bool bt_field_common_generic_is_set(struct bt_field_common *field)
1271 {
1272 return field && field->payload_set;
1273 }
1274
1275 BT_HIDDEN
1276 bt_bool bt_field_common_structure_is_set_recursive(
1277 struct bt_field_common *field)
1278 {
1279 bt_bool is_set = BT_FALSE;
1280 size_t i;
1281 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
1282
1283 BT_ASSERT(field);
1284
1285 for (i = 0; i < structure->fields->len; i++) {
1286 is_set = bt_field_common_is_set_recursive(
1287 structure->fields->pdata[i]);
1288 if (!is_set) {
1289 goto end;
1290 }
1291 }
1292
1293 end:
1294 return is_set;
1295 }
1296
1297 BT_HIDDEN
1298 bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field)
1299 {
1300 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
1301 bt_bool is_set = BT_FALSE;
1302
1303 BT_ASSERT(field);
1304
1305 if (variant->current_field) {
1306 is_set = bt_field_common_is_set_recursive(
1307 variant->current_field);
1308 }
1309
1310 return is_set;
1311 }
1312
1313 BT_HIDDEN
1314 bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field)
1315 {
1316 size_t i;
1317 bt_bool is_set = BT_FALSE;
1318 struct bt_field_common_array *array = BT_FROM_COMMON(field);
1319
1320 BT_ASSERT(field);
1321
1322 for (i = 0; i < array->elements->len; i++) {
1323 is_set = bt_field_common_is_set_recursive(array->elements->pdata[i]);
1324 if (!is_set) {
1325 goto end;
1326 }
1327 }
1328
1329 end:
1330 return is_set;
1331 }
1332
1333 BT_HIDDEN
1334 bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field)
1335 {
1336 size_t i;
1337 bt_bool is_set = BT_FALSE;
1338 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
1339
1340 BT_ASSERT(field);
1341
1342 if (!sequence->elements) {
1343 goto end;
1344 }
1345
1346 for (i = 0; i < sequence->elements->len; i++) {
1347 is_set = bt_field_common_is_set_recursive(
1348 sequence->elements->pdata[i]);
1349 if (!is_set) {
1350 goto end;
1351 }
1352 }
1353
1354 end:
1355 return is_set;
1356 }
This page took 0.063951 seconds and 5 git commands to generate.