-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathfield.c
1601 lines (1315 loc) · 42.7 KB
/
field.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
* Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#define BT_LOG_TAG "LIB/FIELD"
#include "lib/logging.h"
#include "lib/assert-pre.h"
#include <babeltrace2/trace-ir/field.h>
#include "lib/object.h"
#include "compat/compiler.h"
#include "compat/fcntl.h"
#include "common/align.h"
#include "common/assert.h"
#include <inttypes.h>
#include <stdbool.h>
#include "field.h"
#include "field-class.h"
#include "lib/func-status.h"
static
void reset_single_field(struct bt_field *field);
static
void reset_array_field(struct bt_field *field);
static
void reset_structure_field(struct bt_field *field);
static
void reset_option_field(struct bt_field *field);
static
void reset_variant_field(struct bt_field *field);
static
void set_single_field_is_frozen(struct bt_field *field, bool is_frozen);
static
void set_array_field_is_frozen(struct bt_field *field, bool is_frozen);
static
void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen);
static
void set_option_field_is_frozen(struct bt_field *field, bool is_frozen);
static
void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen);
static
bool single_field_is_set(const struct bt_field *field);
static
bool array_field_is_set(const struct bt_field *field);
static
bool structure_field_is_set(const struct bt_field *field);
static
bool option_field_is_set(const struct bt_field *field);
static
bool variant_field_is_set(const struct bt_field *field);
static
struct bt_field_methods bool_field_methods = {
.set_is_frozen = set_single_field_is_frozen,
.is_set = single_field_is_set,
.reset = reset_single_field,
};
static
struct bt_field_methods bit_array_field_methods = {
.set_is_frozen = set_single_field_is_frozen,
.is_set = single_field_is_set,
.reset = reset_single_field,
};
static
struct bt_field_methods integer_field_methods = {
.set_is_frozen = set_single_field_is_frozen,
.is_set = single_field_is_set,
.reset = reset_single_field,
};
static
struct bt_field_methods real_field_methods = {
.set_is_frozen = set_single_field_is_frozen,
.is_set = single_field_is_set,
.reset = reset_single_field,
};
static
struct bt_field_methods string_field_methods = {
.set_is_frozen = set_single_field_is_frozen,
.is_set = single_field_is_set,
.reset = reset_single_field,
};
static
struct bt_field_methods structure_field_methods = {
.set_is_frozen = set_structure_field_is_frozen,
.is_set = structure_field_is_set,
.reset = reset_structure_field,
};
static
struct bt_field_methods array_field_methods = {
.set_is_frozen = set_array_field_is_frozen,
.is_set = array_field_is_set,
.reset = reset_array_field,
};
static
struct bt_field_methods option_field_methods = {
.set_is_frozen = set_option_field_is_frozen,
.is_set = option_field_is_set,
.reset = reset_option_field,
};
static
struct bt_field_methods variant_field_methods = {
.set_is_frozen = set_variant_field_is_frozen,
.is_set = variant_field_is_set,
.reset = reset_variant_field,
};
static
struct bt_field *create_bool_field(struct bt_field_class *);
static
struct bt_field *create_bit_array_field(struct bt_field_class *);
static
struct bt_field *create_integer_field(struct bt_field_class *);
static
struct bt_field *create_real_field(struct bt_field_class *);
static
struct bt_field *create_string_field(struct bt_field_class *);
static
struct bt_field *create_structure_field(struct bt_field_class *);
static
struct bt_field *create_static_array_field(struct bt_field_class *);
static
struct bt_field *create_dynamic_array_field(struct bt_field_class *);
static
struct bt_field *create_option_field(struct bt_field_class *);
static
struct bt_field *create_variant_field(struct bt_field_class *);
static
void destroy_bool_field(struct bt_field *field);
static
void destroy_bit_array_field(struct bt_field *field);
static
void destroy_integer_field(struct bt_field *field);
static
void destroy_real_field(struct bt_field *field);
static
void destroy_string_field(struct bt_field *field);
static
void destroy_structure_field(struct bt_field *field);
static
void destroy_array_field(struct bt_field *field);
static
void destroy_option_field(struct bt_field *field);
static
void destroy_variant_field(struct bt_field *field);
struct bt_field_class *bt_field_borrow_class(struct bt_field *field)
{
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
return field->class;
}
const struct bt_field_class *bt_field_borrow_class_const(
const struct bt_field *field)
{
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
return field->class;
}
enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field)
{
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
return field->class->type;
}
BT_HIDDEN
struct bt_field *bt_field_create(struct bt_field_class *fc)
{
struct bt_field *field = NULL;
BT_ASSERT(fc);
switch (fc->type) {
case BT_FIELD_CLASS_TYPE_BOOL:
field = create_bool_field(fc);
break;
case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
field = create_bit_array_field(fc);
break;
case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
field = create_integer_field(fc);
break;
case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
field = create_real_field(fc);
break;
case BT_FIELD_CLASS_TYPE_STRING:
field = create_string_field(fc);
break;
case BT_FIELD_CLASS_TYPE_STRUCTURE:
field = create_structure_field(fc);
break;
case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
field = create_static_array_field(fc);
break;
case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
field = create_dynamic_array_field(fc);
break;
case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
field = create_option_field(fc);
break;
case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
field = create_variant_field(fc);
break;
default:
bt_common_abort();
}
if (!field) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
"%![fc-]+F", fc);
goto end;
}
end:
return field;
}
static inline
void init_field(struct bt_field *field, struct bt_field_class *fc,
struct bt_field_methods *methods)
{
BT_ASSERT(field);
BT_ASSERT(fc);
bt_object_init_unique(&field->base);
field->methods = methods;
field->class = fc;
bt_object_get_ref_no_null_check(fc);
}
static
struct bt_field *create_bool_field(struct bt_field_class *fc)
{
struct bt_field_bool *bool_field;
BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc);
bool_field = g_new0(struct bt_field_bool, 1);
if (!bool_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one boolean field.");
goto end;
}
init_field((void *) bool_field, fc, &bool_field_methods);
BT_LIB_LOGD("Created boolean field object: %!+f", bool_field);
end:
return (void *) bool_field;
}
static
struct bt_field *create_bit_array_field(struct bt_field_class *fc)
{
struct bt_field_bit_array *ba_field;
BT_LIB_LOGD("Creating bit array field object: %![fc-]+F", fc);
ba_field = g_new0(struct bt_field_bit_array, 1);
if (!ba_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one bit array field.");
goto end;
}
init_field((void *) ba_field, fc, &bit_array_field_methods);
BT_LIB_LOGD("Created bit array field object: %!+f", ba_field);
end:
return (void *) ba_field;
}
static
struct bt_field *create_integer_field(struct bt_field_class *fc)
{
struct bt_field_integer *int_field;
BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc);
int_field = g_new0(struct bt_field_integer, 1);
if (!int_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one integer field.");
goto end;
}
init_field((void *) int_field, fc, &integer_field_methods);
BT_LIB_LOGD("Created integer field object: %!+f", int_field);
end:
return (void *) int_field;
}
static
struct bt_field *create_real_field(struct bt_field_class *fc)
{
struct bt_field_real *real_field;
BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc);
real_field = g_new0(struct bt_field_real, 1);
if (!real_field) {
BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
goto end;
}
init_field((void *) real_field, fc, &real_field_methods);
BT_LIB_LOGD("Created real field object: %!+f", real_field);
end:
return (void *) real_field;
}
static
struct bt_field *create_string_field(struct bt_field_class *fc)
{
struct bt_field_string *string_field;
BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc);
string_field = g_new0(struct bt_field_string, 1);
if (!string_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one string field.");
goto end;
}
init_field((void *) string_field, fc, &string_field_methods);
string_field->buf = g_array_sized_new(FALSE, FALSE,
sizeof(char), 1);
if (!string_field->buf) {
BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
bt_field_destroy((void *) string_field);
string_field = NULL;
goto end;
}
g_array_index(string_field->buf, char, 0) = '\0';
BT_LIB_LOGD("Created string field object: %!+f", string_field);
end:
return (void *) string_field;
}
static inline
int create_fields_from_named_field_classes(
struct bt_field_class_named_field_class_container *fc,
GPtrArray **fields)
{
int ret = 0;
uint64_t i;
*fields = g_ptr_array_new_with_free_func(
(GDestroyNotify) bt_field_destroy);
if (!*fields) {
BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
ret = -1;
goto end;
}
g_ptr_array_set_size(*fields, fc->named_fcs->len);
for (i = 0; i < fc->named_fcs->len; i++) {
struct bt_field *field;
struct bt_named_field_class *named_fc = fc->named_fcs->pdata[i];
field = bt_field_create(named_fc->fc);
if (!field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to create structure member or variant option field: "
"name=\"%s\", %![fc-]+F",
named_fc->name->str, named_fc->fc);
ret = -1;
goto end;
}
g_ptr_array_index(*fields, i) = field;
}
end:
return ret;
}
static
struct bt_field *create_structure_field(struct bt_field_class *fc)
{
struct bt_field_structure *struct_field;
BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc);
struct_field = g_new0(struct bt_field_structure, 1);
if (!struct_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one structure field.");
goto end;
}
init_field((void *) struct_field, fc, &structure_field_methods);
if (create_fields_from_named_field_classes((void *) fc,
&struct_field->fields)) {
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot create structure member fields: %![fc-]+F", fc);
bt_field_destroy((void *) struct_field);
struct_field = NULL;
goto end;
}
BT_LIB_LOGD("Created structure field object: %!+f", struct_field);
end:
return (void *) struct_field;
}
static
struct bt_field *create_option_field(struct bt_field_class *fc)
{
struct bt_field_option *opt_field;
struct bt_field_class_option *opt_fc = (void *) fc;
BT_LIB_LOGD("Creating option field object: %![fc-]+F", fc);
opt_field = g_new0(struct bt_field_option, 1);
if (!opt_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one option field.");
goto end;
}
init_field((void *) opt_field, fc, &option_field_methods);
opt_field->content_field = bt_field_create(opt_fc->content_fc);
if (!opt_field->content_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to create option field's content field: "
"%![opt-fc-]+F, %![content-fc-]+F",
opt_fc, opt_fc->content_fc);
bt_field_destroy((void *) opt_field);
opt_field = NULL;
goto end;
}
BT_LIB_LOGD("Created option field object: %!+f", opt_field);
end:
return (void *) opt_field;
}
static
struct bt_field *create_variant_field(struct bt_field_class *fc)
{
struct bt_field_variant *var_field;
BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc);
var_field = g_new0(struct bt_field_variant, 1);
if (!var_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one variant field.");
goto end;
}
init_field((void *) var_field, fc, &variant_field_methods);
if (create_fields_from_named_field_classes((void *) fc,
&var_field->fields)) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
"%![fc-]+F", fc);
bt_field_destroy((void *) var_field);
var_field = NULL;
goto end;
}
BT_LIB_LOGD("Created variant field object: %!+f", var_field);
end:
return (void *) var_field;
}
static inline
int init_array_field_fields(struct bt_field_array *array_field)
{
int ret = 0;
uint64_t i;
struct bt_field_class_array *array_fc;
BT_ASSERT(array_field);
array_fc = (void *) array_field->common.class;
array_field->fields = g_ptr_array_sized_new(array_field->length);
if (!array_field->fields) {
BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
ret = -1;
goto end;
}
g_ptr_array_set_free_func(array_field->fields,
(GDestroyNotify) bt_field_destroy);
g_ptr_array_set_size(array_field->fields, array_field->length);
for (i = 0; i < array_field->length; i++) {
array_field->fields->pdata[i] = bt_field_create(
array_fc->element_fc);
if (!array_field->fields->pdata[i]) {
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot create array field's element field: "
"index=%" PRIu64 ", %![fc-]+F", i, array_fc);
ret = -1;
goto end;
}
}
end:
return ret;
}
static
struct bt_field *create_static_array_field(struct bt_field_class *fc)
{
struct bt_field_class_array_static *array_fc = (void *) fc;
struct bt_field_array *array_field;
BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc);
array_field = g_new0(struct bt_field_array, 1);
if (!array_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one static array field.");
goto end;
}
init_field((void *) array_field, fc, &array_field_methods);
array_field->length = array_fc->length;
if (init_array_field_fields(array_field)) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
"%![fc-]+F", fc);
bt_field_destroy((void *) array_field);
array_field = NULL;
goto end;
}
BT_LIB_LOGD("Created static array field object: %!+f", array_field);
end:
return (void *) array_field;
}
static
struct bt_field *create_dynamic_array_field(struct bt_field_class *fc)
{
struct bt_field_array *array_field;
BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc);
array_field = g_new0(struct bt_field_array, 1);
if (!array_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Failed to allocate one dynamic array field.");
goto end;
}
init_field((void *) array_field, fc, &array_field_methods);
if (init_array_field_fields(array_field)) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
"%![fc-]+F", fc);
bt_field_destroy((void *) array_field);
array_field = NULL;
goto end;
}
BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field);
end:
return (void *) array_field;
}
bt_bool bt_field_bool_get_value(const struct bt_field *field)
{
const struct bt_field_bool *bool_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL,
"Field");
return (bt_bool) bool_field->value;
}
void bt_field_bool_set_value(struct bt_field *field, bt_bool value)
{
struct bt_field_bool *bool_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_BOOL,
"Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
bool_field->value = (bool) value;
bt_field_set_single(field, true);
}
uint64_t bt_field_bit_array_get_value_as_integer(const struct bt_field *field)
{
const struct bt_field_bit_array *ba_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field");
return ba_field->value_as_int;
}
void bt_field_bit_array_set_value_as_integer(struct bt_field *field,
uint64_t value)
{
struct bt_field_bit_array *ba_field = (void *) field;
struct bt_field_class_bit_array *ba_fc;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
ba_fc = (void *) field->class;
ba_field->value_as_int = value;
if (ba_fc->length < 64) {
/* Apply mask */
ba_field->value_as_int &= ((UINT64_C(1) << ba_fc->length) - 1);
}
bt_field_set_single(field, true);
}
int64_t bt_field_integer_signed_get_value(const struct bt_field *field)
{
const struct bt_field_integer *int_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field");
return int_field->value.i;
}
void bt_field_integer_signed_set_value(struct bt_field *field, int64_t value)
{
struct bt_field_integer *int_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_signed(
((struct bt_field_class_integer *) field->class)->range, value),
"Value is out of bounds: value=%" PRId64 ", %![field-]+f, "
"%![fc-]+F", value, field, field->class);
int_field->value.i = value;
bt_field_set_single(field, true);
}
uint64_t bt_field_integer_unsigned_get_value(const struct bt_field *field)
{
const struct bt_field_integer *int_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field");
return int_field->value.u;
}
void bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value)
{
struct bt_field_integer *int_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
BT_ASSERT_PRE_DEV(bt_util_value_is_in_range_unsigned(
((struct bt_field_class_integer *) field->class)->range, value),
"Value is out of bounds: value=%" PRIu64 ", %![field-]+f, "
"%![fc-]+F", value, field, field->class);
int_field->value.u = value;
bt_field_set_single(field, true);
}
float bt_field_real_single_precision_get_value(const struct bt_field *field)
{
const struct bt_field_real *real_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field");
return (float) real_field->value;
}
double bt_field_real_double_precision_get_value(const struct bt_field *field)
{
const struct bt_field_real *real_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field");
return real_field->value;
}
void bt_field_real_single_precision_set_value(struct bt_field *field,
float value)
{
struct bt_field_real *real_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
real_field->value = (double) value;
bt_field_set_single(field, true);
}
void bt_field_real_double_precision_set_value(struct bt_field *field,
double value)
{
struct bt_field_real *real_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
real_field->value = value;
bt_field_set_single(field, true);
}
enum bt_field_enumeration_get_mapping_labels_status
bt_field_enumeration_unsigned_get_mapping_labels(
const struct bt_field *field,
bt_field_class_enumeration_mapping_label_array *label_array,
uint64_t *count)
{
const struct bt_field_integer *int_field = (const void *) field;
BT_ASSERT_PRE_DEV_NO_ERROR();
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field");
return (int)
bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
field->class, int_field->value.u, label_array, count);
}
enum bt_field_enumeration_get_mapping_labels_status
bt_field_enumeration_signed_get_mapping_labels(
const struct bt_field *field,
bt_field_class_enumeration_mapping_label_array *label_array,
uint64_t *count)
{
const struct bt_field_integer *int_field = (const void *) field;
BT_ASSERT_PRE_DEV_NO_ERROR();
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Count (output)");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field");
return (int)
bt_field_class_enumeration_signed_get_mapping_labels_for_value(
field->class, int_field->value.i, label_array, count);
}
const char *bt_field_string_get_value(const struct bt_field *field)
{
const struct bt_field_string *string_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
"Field");
return (const char *) string_field->buf->data;
}
uint64_t bt_field_string_get_length(const struct bt_field *field)
{
const struct bt_field_string *string_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_SET(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
"Field");
return string_field->length;
}
static inline
void clear_string_field(struct bt_field *field)
{
struct bt_field_string *string_field = (void *) field;
BT_ASSERT_DBG(field);
string_field->length = 0;
bt_field_set_single(field, true);
}
enum bt_field_string_set_value_status bt_field_string_set_value(
struct bt_field *field, const char *value)
{
BT_ASSERT_PRE_DEV_NO_ERROR();
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING,
"Field");
clear_string_field(field);
return (int) bt_field_string_append_with_length(field, value,
(uint64_t) strlen(value));
}
enum bt_field_string_append_status bt_field_string_append(
struct bt_field *field, const char *value)
{
BT_ASSERT_PRE_DEV_NO_ERROR();
return bt_field_string_append_with_length(field,
value, (uint64_t) strlen(value));
}
enum bt_field_string_append_status bt_field_string_append_with_length(
struct bt_field *field, const char *value, uint64_t length)
{
struct bt_field_string *string_field = (void *) field;
char *data;
uint64_t new_length;
BT_ASSERT_PRE_DEV_NO_ERROR();
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_NON_NULL(value, "Value");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_STRING, "Field");
/* Make sure no null bytes are appended */
BT_ASSERT_PRE_DEV(!memchr(value, '\0', length),
"String value to append contains a null character: "
"partial-value=\"%.32s\", length=%" PRIu64, value, length);
new_length = length + string_field->length;
if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) {
g_array_set_size(string_field->buf, new_length + 1);
}
data = string_field->buf->data;
memcpy(data + string_field->length, value, length);
((char *) string_field->buf->data)[new_length] = '\0';
string_field->length = new_length;
bt_field_set_single(field, true);
return BT_FUNC_STATUS_OK;
}
void bt_field_string_clear(struct bt_field *field)
{
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(field,
BT_FIELD_CLASS_TYPE_STRING, "Field");
clear_string_field(field);
}
uint64_t bt_field_array_get_length(const struct bt_field *field)
{
const struct bt_field_array *array_field = (const void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field");
return array_field->length;
}
enum bt_field_array_dynamic_set_length_status bt_field_array_dynamic_set_length(
struct bt_field *field, uint64_t length)
{
int ret = BT_FUNC_STATUS_OK;
struct bt_field_array *array_field = (void *) field;
BT_ASSERT_PRE_DEV_NO_ERROR();
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_HOT(field, "Field");
if (G_UNLIKELY(length > array_field->fields->len)) {
/* Make more room */
struct bt_field_class_array *array_fc;
uint64_t cur_len = array_field->fields->len;
uint64_t i;
g_ptr_array_set_size(array_field->fields, length);
array_fc = (void *) field->class;
for (i = cur_len; i < array_field->fields->len; i++) {
struct bt_field *elem_field = bt_field_create(
array_fc->element_fc);
if (!elem_field) {
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot create element field for "
"dynamic array field: "
"index=%" PRIu64 ", "
"%![array-field-]+f", i, field);
ret = BT_FUNC_STATUS_MEMORY_ERROR;
goto end;
}
BT_ASSERT_DBG(!array_field->fields->pdata[i]);
array_field->fields->pdata[i] = elem_field;
}
}
array_field->length = length;
end:
return ret;
}
static inline
struct bt_field *borrow_array_field_element_field_by_index(
struct bt_field *field, uint64_t index)
{
struct bt_field_array *array_field = (void *) field;
BT_ASSERT_PRE_DEV_NON_NULL(field, "Field");
BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(field, "Field");
BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length);
return array_field->fields->pdata[index];
}
struct bt_field *bt_field_array_borrow_element_field_by_index(
struct bt_field *field, uint64_t index)
{
return borrow_array_field_element_field_by_index(field, index);
}
const struct bt_field *