Commit | Line | Data |
---|---|---|
15fe47e0 PP |
1 | #ifndef BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H |
2 | #define BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H | |
3 | ||
4 | /* | |
5 | * Copyright 2018-2019 - Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | */ | |
17 | ||
3fadfbc0 | 18 | #include <babeltrace2/babeltrace.h> |
578e048b MJ |
19 | #include "common/common.h" |
20 | #include "common/assert.h" | |
21 | #include "compat/uuid.h" | |
15fe47e0 PP |
22 | #include <glib.h> |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
25 | #include <stdbool.h> | |
26 | #include <ctype.h> | |
27 | ||
28 | enum fs_sink_ctf_field_class_type { | |
29 | FS_SINK_CTF_FIELD_CLASS_TYPE_INT, | |
30 | FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT, | |
31 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, | |
32 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, | |
33 | FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, | |
34 | FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE, | |
35 | FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, | |
36 | }; | |
37 | ||
38 | struct fs_sink_ctf_field_class { | |
39 | enum fs_sink_ctf_field_class_type type; | |
40 | ||
41 | /* Weak */ | |
42 | const bt_field_class *ir_fc; | |
43 | ||
44 | unsigned int alignment; | |
45 | ||
46 | /* Index of the field class within its own parent */ | |
47 | uint64_t index_in_parent; | |
48 | }; | |
49 | ||
50 | struct fs_sink_ctf_field_class_bit_array { | |
51 | struct fs_sink_ctf_field_class base; | |
52 | unsigned int size; | |
53 | }; | |
54 | ||
55 | struct fs_sink_ctf_field_class_int { | |
56 | struct fs_sink_ctf_field_class_bit_array base; | |
57 | bool is_signed; | |
58 | }; | |
59 | ||
60 | struct fs_sink_ctf_field_class_float { | |
61 | struct fs_sink_ctf_field_class_bit_array base; | |
62 | }; | |
63 | ||
64 | struct fs_sink_ctf_field_class_string { | |
65 | struct fs_sink_ctf_field_class base; | |
66 | }; | |
67 | ||
68 | struct fs_sink_ctf_named_field_class { | |
69 | GString *name; | |
70 | ||
71 | /* Owned by this */ | |
72 | struct fs_sink_ctf_field_class *fc; | |
73 | }; | |
74 | ||
75 | struct fs_sink_ctf_field_class_struct { | |
76 | struct fs_sink_ctf_field_class base; | |
77 | ||
78 | /* Array of `struct fs_sink_ctf_named_field_class` */ | |
79 | GArray *members; | |
80 | }; | |
81 | ||
82 | struct fs_sink_ctf_field_class_variant { | |
83 | struct fs_sink_ctf_field_class base; | |
84 | GString *tag_ref; | |
85 | bool tag_is_before; | |
86 | ||
87 | /* Array of `struct fs_sink_ctf_named_field_class` */ | |
88 | GArray *options; | |
89 | }; | |
90 | ||
91 | struct fs_sink_ctf_field_class_array_base { | |
92 | struct fs_sink_ctf_field_class base; | |
93 | struct fs_sink_ctf_field_class *elem_fc; | |
94 | }; | |
95 | ||
96 | struct fs_sink_ctf_field_class_array { | |
97 | struct fs_sink_ctf_field_class_array_base base; | |
98 | uint64_t length; | |
99 | }; | |
100 | ||
101 | struct fs_sink_ctf_field_class_sequence { | |
102 | struct fs_sink_ctf_field_class_array_base base; | |
103 | GString *length_ref; | |
104 | bool length_is_before; | |
105 | }; | |
106 | ||
107 | struct fs_sink_ctf_stream_class; | |
108 | ||
109 | struct fs_sink_ctf_event_class { | |
110 | /* Weak */ | |
111 | const bt_event_class *ir_ec; | |
112 | ||
113 | /* Weak */ | |
114 | struct fs_sink_ctf_stream_class *sc; | |
115 | ||
116 | /* Owned by this */ | |
117 | struct fs_sink_ctf_field_class *spec_context_fc; | |
118 | ||
119 | /* Owned by this */ | |
120 | struct fs_sink_ctf_field_class *payload_fc; | |
121 | }; | |
122 | ||
123 | struct fs_sink_ctf_trace_class; | |
124 | ||
125 | struct fs_sink_ctf_stream_class { | |
126 | /* Weak */ | |
127 | struct fs_sink_ctf_trace_class *tc; | |
128 | ||
129 | /* Weak */ | |
130 | const bt_stream_class *ir_sc; | |
131 | ||
132 | /* Weak */ | |
133 | const bt_clock_class *default_clock_class; | |
134 | ||
135 | GString *default_clock_class_name; | |
ffb5c13c PP |
136 | bool packets_have_ts_begin; |
137 | bool packets_have_ts_end; | |
138 | bool has_discarded_events; | |
139 | bool discarded_events_has_ts; | |
140 | bool discarded_packets_has_ts; | |
15fe47e0 PP |
141 | |
142 | /* Owned by this */ | |
143 | struct fs_sink_ctf_field_class *packet_context_fc; | |
144 | ||
145 | /* Owned by this */ | |
146 | struct fs_sink_ctf_field_class *event_common_context_fc; | |
147 | ||
148 | /* Array of `struct fs_sink_ctf_event_class *` (owned by this) */ | |
149 | GPtrArray *event_classes; | |
150 | ||
151 | /* | |
152 | * `const bt_event_class *` (weak) -> | |
153 | * `struct fs_sink_ctf_event_class *` (weak) | |
154 | */ | |
155 | GHashTable *event_classes_from_ir; | |
156 | }; | |
157 | ||
158 | struct fs_sink_ctf_trace_class { | |
159 | /* Weak */ | |
160 | const bt_trace_class *ir_tc; | |
161 | ||
162 | unsigned char uuid[BABELTRACE_UUID_LEN]; | |
163 | ||
164 | /* Array of `struct fs_sink_ctf_stream_class *` (owned by this) */ | |
165 | GPtrArray *stream_classes; | |
166 | }; | |
167 | ||
168 | static inline | |
169 | void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc); | |
170 | ||
171 | static inline | |
172 | void _fs_sink_ctf_field_class_init(struct fs_sink_ctf_field_class *fc, | |
173 | enum fs_sink_ctf_field_class_type type, | |
174 | const bt_field_class *ir_fc, unsigned int alignment, | |
175 | uint64_t index_in_parent) | |
176 | { | |
177 | BT_ASSERT(fc); | |
178 | fc->type = type; | |
179 | fc->ir_fc = ir_fc; | |
180 | fc->alignment = alignment; | |
181 | fc->index_in_parent = index_in_parent; | |
182 | } | |
183 | ||
184 | static inline | |
185 | void _fs_sink_ctf_field_class_bit_array_init( | |
186 | struct fs_sink_ctf_field_class_bit_array *fc, | |
187 | enum fs_sink_ctf_field_class_type type, | |
188 | const bt_field_class *ir_fc, unsigned int size, | |
189 | uint64_t index_in_parent) | |
190 | { | |
191 | _fs_sink_ctf_field_class_init((void *) fc, type, ir_fc, | |
192 | size % 8 == 0 ? 8 : 1, index_in_parent); | |
193 | fc->size = size; | |
194 | } | |
195 | ||
196 | static inline | |
197 | void _fs_sink_ctf_field_class_int_init(struct fs_sink_ctf_field_class_int *fc, | |
198 | enum fs_sink_ctf_field_class_type type, | |
199 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
200 | { | |
201 | bt_field_class_type ir_fc_type = bt_field_class_get_type(ir_fc); | |
202 | ||
203 | _fs_sink_ctf_field_class_bit_array_init((void *) fc, type, ir_fc, | |
204 | (unsigned int) bt_field_class_integer_get_field_value_range( | |
205 | ir_fc), | |
206 | index_in_parent); | |
207 | fc->is_signed = (ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || | |
208 | ir_fc_type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); | |
209 | } | |
210 | ||
211 | static inline | |
212 | void _fs_sink_ctf_named_field_class_init( | |
213 | struct fs_sink_ctf_named_field_class *named_fc) | |
214 | { | |
215 | BT_ASSERT(named_fc); | |
216 | named_fc->name = g_string_new(NULL); | |
217 | BT_ASSERT(named_fc->name); | |
218 | } | |
219 | ||
220 | static inline | |
221 | void _fs_sink_ctf_named_field_class_fini( | |
222 | struct fs_sink_ctf_named_field_class *named_fc) | |
223 | { | |
224 | BT_ASSERT(named_fc); | |
225 | ||
226 | if (named_fc->name) { | |
227 | g_string_free(named_fc->name, TRUE); | |
228 | named_fc->name = NULL; | |
229 | } | |
230 | ||
231 | fs_sink_ctf_field_class_destroy(named_fc->fc); | |
232 | named_fc->fc = NULL; | |
233 | } | |
234 | ||
235 | static inline | |
236 | struct fs_sink_ctf_field_class_int *fs_sink_ctf_field_class_int_create( | |
237 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
238 | { | |
239 | struct fs_sink_ctf_field_class_int *fc = | |
240 | g_new0(struct fs_sink_ctf_field_class_int, 1); | |
241 | ||
242 | BT_ASSERT(fc); | |
243 | _fs_sink_ctf_field_class_int_init(fc, FS_SINK_CTF_FIELD_CLASS_TYPE_INT, | |
244 | ir_fc, index_in_parent); | |
245 | return fc; | |
246 | } | |
247 | ||
248 | static inline | |
249 | struct fs_sink_ctf_field_class_float *fs_sink_ctf_field_class_float_create( | |
250 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
251 | { | |
252 | struct fs_sink_ctf_field_class_float *fc = | |
253 | g_new0(struct fs_sink_ctf_field_class_float, 1); | |
254 | ||
255 | BT_ASSERT(fc); | |
256 | _fs_sink_ctf_field_class_bit_array_init((void *) fc, | |
257 | FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT, | |
258 | ir_fc, bt_field_class_real_is_single_precision(ir_fc) ? 32 : 64, | |
259 | index_in_parent); | |
260 | return fc; | |
261 | } | |
262 | ||
263 | static inline | |
264 | struct fs_sink_ctf_field_class_string *fs_sink_ctf_field_class_string_create( | |
265 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
266 | { | |
267 | struct fs_sink_ctf_field_class_string *fc = | |
268 | g_new0(struct fs_sink_ctf_field_class_string, 1); | |
269 | ||
270 | BT_ASSERT(fc); | |
271 | _fs_sink_ctf_field_class_init((void *) fc, | |
272 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRING, ir_fc, | |
273 | 8, index_in_parent); | |
274 | return fc; | |
275 | } | |
276 | ||
277 | static inline | |
278 | struct fs_sink_ctf_field_class_struct *fs_sink_ctf_field_class_struct_create_empty( | |
279 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
280 | { | |
281 | struct fs_sink_ctf_field_class_struct *fc = | |
282 | g_new0(struct fs_sink_ctf_field_class_struct, 1); | |
283 | ||
284 | BT_ASSERT(fc); | |
285 | _fs_sink_ctf_field_class_init((void *) fc, | |
286 | FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT, ir_fc, 1, index_in_parent); | |
287 | fc->members = g_array_new(FALSE, TRUE, | |
288 | sizeof(struct fs_sink_ctf_named_field_class)); | |
289 | BT_ASSERT(fc->members); | |
290 | return fc; | |
291 | } | |
292 | ||
293 | static inline | |
294 | struct fs_sink_ctf_field_class_variant *fs_sink_ctf_field_class_variant_create_empty( | |
295 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
296 | { | |
297 | struct fs_sink_ctf_field_class_variant *fc = | |
298 | g_new0(struct fs_sink_ctf_field_class_variant, 1); | |
299 | ||
300 | BT_ASSERT(fc); | |
301 | _fs_sink_ctf_field_class_init((void *) fc, | |
302 | FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT, ir_fc, | |
303 | 1, index_in_parent); | |
304 | fc->options = g_array_new(FALSE, TRUE, | |
305 | sizeof(struct fs_sink_ctf_named_field_class)); | |
306 | BT_ASSERT(fc->options); | |
307 | fc->tag_ref = g_string_new(NULL); | |
308 | BT_ASSERT(fc->tag_ref); | |
309 | fc->tag_is_before = | |
310 | bt_field_class_variant_borrow_selector_field_path_const(ir_fc) == | |
311 | NULL; | |
312 | return fc; | |
313 | } | |
314 | ||
315 | static inline | |
316 | struct fs_sink_ctf_field_class_array *fs_sink_ctf_field_class_array_create_empty( | |
317 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
318 | { | |
319 | struct fs_sink_ctf_field_class_array *fc = | |
320 | g_new0(struct fs_sink_ctf_field_class_array, 1); | |
321 | ||
322 | BT_ASSERT(fc); | |
323 | _fs_sink_ctf_field_class_init((void *) fc, | |
324 | FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY, ir_fc, | |
325 | 1, index_in_parent); | |
326 | fc->length = bt_field_class_static_array_get_length(ir_fc); | |
327 | return fc; | |
328 | } | |
329 | ||
330 | static inline | |
331 | struct fs_sink_ctf_field_class_sequence *fs_sink_ctf_field_class_sequence_create_empty( | |
332 | const bt_field_class *ir_fc, uint64_t index_in_parent) | |
333 | { | |
334 | struct fs_sink_ctf_field_class_sequence *fc = | |
335 | g_new0(struct fs_sink_ctf_field_class_sequence, 1); | |
336 | ||
337 | BT_ASSERT(fc); | |
338 | _fs_sink_ctf_field_class_init((void *) fc, | |
339 | FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE, | |
340 | ir_fc, 1, index_in_parent); | |
341 | fc->length_ref = g_string_new(NULL); | |
342 | BT_ASSERT(fc->length_ref); | |
343 | fc->length_is_before = | |
344 | bt_field_class_dynamic_array_borrow_length_field_path_const(ir_fc) == | |
345 | NULL; | |
346 | return fc; | |
347 | } | |
348 | ||
349 | static inline | |
350 | struct fs_sink_ctf_named_field_class * | |
351 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
352 | struct fs_sink_ctf_field_class_struct *fc, uint64_t index); | |
353 | ||
354 | static inline | |
355 | struct fs_sink_ctf_named_field_class * | |
356 | fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
357 | struct fs_sink_ctf_field_class_variant *fc, uint64_t index); | |
358 | ||
359 | static inline | |
360 | void _fs_sink_ctf_field_class_fini(struct fs_sink_ctf_field_class *fc) | |
361 | { | |
362 | BT_ASSERT(fc); | |
363 | } | |
364 | ||
365 | static inline | |
366 | void _fs_sink_ctf_field_class_int_destroy( | |
367 | struct fs_sink_ctf_field_class_int *fc) | |
368 | { | |
369 | BT_ASSERT(fc); | |
370 | _fs_sink_ctf_field_class_fini((void *) fc); | |
371 | g_free(fc); | |
372 | } | |
373 | ||
374 | static inline | |
375 | void _fs_sink_ctf_field_class_float_destroy( | |
376 | struct fs_sink_ctf_field_class_float *fc) | |
377 | { | |
378 | BT_ASSERT(fc); | |
379 | _fs_sink_ctf_field_class_fini((void *) fc); | |
380 | g_free(fc); | |
381 | } | |
382 | ||
383 | static inline | |
384 | void _fs_sink_ctf_field_class_string_destroy( | |
385 | struct fs_sink_ctf_field_class_string *fc) | |
386 | { | |
387 | BT_ASSERT(fc); | |
388 | _fs_sink_ctf_field_class_fini((void *) fc); | |
389 | g_free(fc); | |
390 | } | |
391 | ||
392 | static inline | |
393 | void _fs_sink_ctf_field_class_struct_destroy( | |
394 | struct fs_sink_ctf_field_class_struct *fc) | |
395 | { | |
396 | BT_ASSERT(fc); | |
397 | _fs_sink_ctf_field_class_fini((void *) fc); | |
398 | ||
399 | if (fc->members) { | |
400 | uint64_t i; | |
401 | ||
402 | for (i = 0; i < fc->members->len; i++) { | |
403 | struct fs_sink_ctf_named_field_class *named_fc = | |
404 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
405 | fc, i); | |
406 | ||
407 | _fs_sink_ctf_named_field_class_fini(named_fc); | |
408 | } | |
409 | ||
410 | g_array_free(fc->members, TRUE); | |
411 | fc->members = NULL; | |
412 | } | |
413 | ||
414 | g_free(fc); | |
415 | } | |
416 | ||
417 | static inline | |
418 | void _fs_sink_ctf_field_class_array_base_fini( | |
419 | struct fs_sink_ctf_field_class_array_base *fc) | |
420 | { | |
421 | BT_ASSERT(fc); | |
422 | _fs_sink_ctf_field_class_fini((void *) fc); | |
423 | fs_sink_ctf_field_class_destroy(fc->elem_fc); | |
424 | fc->elem_fc = NULL; | |
425 | } | |
426 | ||
427 | static inline | |
428 | void _fs_sink_ctf_field_class_array_destroy( | |
429 | struct fs_sink_ctf_field_class_array *fc) | |
430 | { | |
431 | BT_ASSERT(fc); | |
432 | _fs_sink_ctf_field_class_array_base_fini((void *) fc); | |
433 | g_free(fc); | |
434 | } | |
435 | ||
436 | static inline | |
437 | void _fs_sink_ctf_field_class_sequence_destroy( | |
438 | struct fs_sink_ctf_field_class_sequence *fc) | |
439 | { | |
440 | BT_ASSERT(fc); | |
441 | _fs_sink_ctf_field_class_array_base_fini((void *) fc); | |
442 | ||
443 | if (fc->length_ref) { | |
444 | g_string_free(fc->length_ref, TRUE); | |
445 | fc->length_ref = NULL; | |
446 | } | |
447 | ||
448 | g_free(fc); | |
449 | } | |
450 | ||
451 | static inline | |
452 | void _fs_sink_ctf_field_class_variant_destroy( | |
453 | struct fs_sink_ctf_field_class_variant *fc) | |
454 | { | |
455 | BT_ASSERT(fc); | |
456 | _fs_sink_ctf_field_class_fini((void *) fc); | |
457 | ||
458 | if (fc->options) { | |
459 | uint64_t i; | |
460 | ||
461 | for (i = 0; i < fc->options->len; i++) { | |
462 | struct fs_sink_ctf_named_field_class *named_fc = | |
463 | fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
464 | fc, i); | |
465 | ||
466 | _fs_sink_ctf_named_field_class_fini(named_fc); | |
467 | } | |
468 | ||
469 | g_array_free(fc->options, TRUE); | |
470 | fc->options = NULL; | |
471 | } | |
472 | ||
473 | if (fc->tag_ref) { | |
474 | g_string_free(fc->tag_ref, TRUE); | |
475 | fc->tag_ref = NULL; | |
476 | } | |
477 | ||
478 | g_free(fc); | |
479 | } | |
480 | ||
481 | static inline | |
482 | void fs_sink_ctf_field_class_destroy(struct fs_sink_ctf_field_class *fc) | |
483 | { | |
484 | if (!fc) { | |
485 | return; | |
486 | } | |
487 | ||
488 | switch (fc->type) { | |
489 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: | |
490 | _fs_sink_ctf_field_class_int_destroy((void *) fc); | |
491 | break; | |
492 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: | |
493 | _fs_sink_ctf_field_class_float_destroy((void *) fc); | |
494 | break; | |
495 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: | |
496 | _fs_sink_ctf_field_class_string_destroy((void *) fc); | |
497 | break; | |
498 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
499 | _fs_sink_ctf_field_class_struct_destroy((void *) fc); | |
500 | break; | |
501 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
502 | _fs_sink_ctf_field_class_array_destroy((void *) fc); | |
503 | break; | |
504 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
505 | _fs_sink_ctf_field_class_sequence_destroy((void *) fc); | |
506 | break; | |
507 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
508 | _fs_sink_ctf_field_class_variant_destroy((void *) fc); | |
509 | break; | |
510 | default: | |
511 | abort(); | |
512 | } | |
513 | } | |
514 | ||
515 | static inline | |
516 | struct fs_sink_ctf_named_field_class * | |
517 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
518 | struct fs_sink_ctf_field_class_struct *fc, uint64_t index) | |
519 | { | |
520 | BT_ASSERT(fc); | |
521 | BT_ASSERT(index < fc->members->len); | |
522 | return &g_array_index(fc->members, struct fs_sink_ctf_named_field_class, | |
523 | index); | |
524 | } | |
525 | ||
526 | static inline | |
527 | struct fs_sink_ctf_named_field_class * | |
528 | fs_sink_ctf_field_class_struct_borrow_member_by_name( | |
529 | struct fs_sink_ctf_field_class_struct *fc, const char *name) | |
530 | { | |
531 | uint64_t i; | |
532 | struct fs_sink_ctf_named_field_class *ret_named_fc = NULL; | |
533 | ||
534 | BT_ASSERT(fc); | |
535 | BT_ASSERT(name); | |
536 | ||
537 | for (i = 0; i < fc->members->len; i++) { | |
538 | struct fs_sink_ctf_named_field_class *named_fc = | |
539 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
540 | fc, i); | |
541 | ||
542 | if (strcmp(name, named_fc->name->str) == 0) { | |
543 | ret_named_fc = named_fc; | |
544 | goto end; | |
545 | } | |
546 | } | |
547 | ||
548 | end: | |
549 | return ret_named_fc; | |
550 | } | |
551 | ||
552 | static inline | |
553 | struct fs_sink_ctf_field_class * | |
554 | fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name( | |
555 | struct fs_sink_ctf_field_class_struct *struct_fc, const char *name) | |
556 | { | |
557 | struct fs_sink_ctf_named_field_class *named_fc = NULL; | |
558 | struct fs_sink_ctf_field_class *fc = NULL; | |
559 | ||
560 | if (!struct_fc) { | |
561 | goto end; | |
562 | } | |
563 | ||
564 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_name( | |
565 | struct_fc, name); | |
566 | if (!named_fc) { | |
567 | goto end; | |
568 | } | |
569 | ||
570 | fc = named_fc->fc; | |
571 | ||
572 | end: | |
573 | return fc; | |
574 | } | |
575 | ||
576 | static inline | |
577 | struct fs_sink_ctf_field_class_int * | |
578 | fs_sink_ctf_field_class_struct_borrow_member_int_field_class_by_name( | |
579 | struct fs_sink_ctf_field_class_struct *struct_fc, | |
580 | const char *name) | |
581 | { | |
582 | struct fs_sink_ctf_field_class_int *int_fc = NULL; | |
583 | ||
584 | int_fc = (void *) | |
585 | fs_sink_ctf_field_class_struct_borrow_member_field_class_by_name( | |
586 | struct_fc, name); | |
587 | if (!int_fc) { | |
588 | goto end; | |
589 | } | |
590 | ||
591 | if (int_fc->base.base.type != FS_SINK_CTF_FIELD_CLASS_TYPE_INT) { | |
592 | int_fc = NULL; | |
593 | goto end; | |
594 | } | |
595 | ||
596 | end: | |
597 | return int_fc; | |
598 | } | |
599 | ||
600 | static inline | |
601 | void fs_sink_ctf_field_class_struct_align_at_least( | |
602 | struct fs_sink_ctf_field_class_struct *fc, | |
603 | unsigned int alignment) | |
604 | { | |
605 | if (alignment > fc->base.alignment) { | |
606 | fc->base.alignment = alignment; | |
607 | } | |
608 | } | |
609 | ||
610 | static inline | |
611 | void fs_sink_ctf_field_class_struct_append_member( | |
612 | struct fs_sink_ctf_field_class_struct *fc, | |
613 | const char *name, struct fs_sink_ctf_field_class *member_fc) | |
614 | { | |
615 | struct fs_sink_ctf_named_field_class *named_fc; | |
616 | ||
617 | BT_ASSERT(fc); | |
618 | BT_ASSERT(name); | |
619 | g_array_set_size(fc->members, fc->members->len + 1); | |
620 | ||
621 | named_fc = &g_array_index(fc->members, | |
622 | struct fs_sink_ctf_named_field_class, fc->members->len - 1); | |
623 | _fs_sink_ctf_named_field_class_init(named_fc); | |
624 | g_string_assign(named_fc->name, name); | |
625 | named_fc->fc = member_fc; | |
626 | fs_sink_ctf_field_class_struct_align_at_least(fc, member_fc->alignment); | |
627 | } | |
628 | ||
629 | static inline | |
630 | struct fs_sink_ctf_named_field_class * | |
631 | fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
632 | struct fs_sink_ctf_field_class_variant *fc, uint64_t index) | |
633 | { | |
634 | BT_ASSERT(fc); | |
635 | BT_ASSERT(index < fc->options->len); | |
636 | return &g_array_index(fc->options, struct fs_sink_ctf_named_field_class, | |
637 | index); | |
638 | } | |
639 | ||
640 | static inline | |
641 | struct fs_sink_ctf_named_field_class * | |
642 | fs_sink_ctf_field_class_variant_borrow_option_by_name( | |
643 | struct fs_sink_ctf_field_class_variant *fc, const char *name) | |
644 | { | |
645 | uint64_t i; | |
646 | struct fs_sink_ctf_named_field_class *ret_named_fc = NULL; | |
647 | ||
648 | BT_ASSERT(fc); | |
649 | BT_ASSERT(name); | |
650 | ||
651 | for (i = 0; i < fc->options->len; i++) { | |
652 | struct fs_sink_ctf_named_field_class *named_fc = | |
653 | fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
654 | fc, i); | |
655 | ||
656 | if (strcmp(name, named_fc->name->str) == 0) { | |
657 | ret_named_fc = named_fc; | |
658 | goto end; | |
659 | } | |
660 | } | |
661 | ||
662 | end: | |
663 | return ret_named_fc; | |
664 | } | |
665 | ||
666 | static inline | |
667 | void fs_sink_ctf_field_class_variant_append_option( | |
668 | struct fs_sink_ctf_field_class_variant *fc, | |
669 | const char *name, struct fs_sink_ctf_field_class *option_fc) | |
670 | { | |
671 | struct fs_sink_ctf_named_field_class *named_fc; | |
672 | ||
673 | BT_ASSERT(fc); | |
674 | BT_ASSERT(name); | |
675 | g_array_set_size(fc->options, fc->options->len + 1); | |
676 | ||
677 | named_fc = &g_array_index(fc->options, | |
678 | struct fs_sink_ctf_named_field_class, fc->options->len - 1); | |
679 | _fs_sink_ctf_named_field_class_init(named_fc); | |
680 | g_string_assign(named_fc->name, name); | |
681 | named_fc->fc = option_fc; | |
682 | } | |
683 | ||
684 | static inline | |
685 | struct fs_sink_ctf_event_class *fs_sink_ctf_event_class_create( | |
686 | struct fs_sink_ctf_stream_class *sc, | |
687 | const bt_event_class *ir_ec) | |
688 | { | |
689 | struct fs_sink_ctf_event_class *ec = | |
690 | g_new0(struct fs_sink_ctf_event_class, 1); | |
691 | ||
692 | BT_ASSERT(sc); | |
693 | BT_ASSERT(ir_ec); | |
694 | BT_ASSERT(ec); | |
695 | ec->ir_ec = ir_ec; | |
696 | ec->sc = sc; | |
697 | g_ptr_array_add(sc->event_classes, ec); | |
698 | g_hash_table_insert(sc->event_classes_from_ir, (gpointer) ir_ec, ec); | |
699 | return ec; | |
700 | } | |
701 | ||
702 | static inline | |
703 | void fs_sink_ctf_event_class_destroy(struct fs_sink_ctf_event_class *ec) | |
704 | { | |
705 | if (!ec) { | |
706 | return; | |
707 | } | |
708 | ||
709 | fs_sink_ctf_field_class_destroy(ec->spec_context_fc); | |
710 | ec->spec_context_fc = NULL; | |
711 | fs_sink_ctf_field_class_destroy(ec->payload_fc); | |
712 | ec->payload_fc = NULL; | |
713 | g_free(ec); | |
714 | } | |
715 | ||
716 | static inline | |
717 | struct fs_sink_ctf_stream_class *fs_sink_ctf_stream_class_create( | |
718 | struct fs_sink_ctf_trace_class *tc, | |
719 | const bt_stream_class *ir_sc) | |
720 | { | |
721 | struct fs_sink_ctf_stream_class *sc = | |
722 | g_new0(struct fs_sink_ctf_stream_class, 1); | |
723 | ||
724 | BT_ASSERT(tc); | |
725 | BT_ASSERT(ir_sc); | |
726 | BT_ASSERT(sc); | |
727 | sc->tc = tc; | |
728 | sc->ir_sc = ir_sc; | |
729 | sc->default_clock_class = | |
730 | bt_stream_class_borrow_default_clock_class_const(ir_sc); | |
731 | sc->default_clock_class_name = g_string_new(NULL); | |
732 | BT_ASSERT(sc->default_clock_class_name); | |
733 | sc->event_classes = g_ptr_array_new_with_free_func( | |
734 | (GDestroyNotify) fs_sink_ctf_event_class_destroy); | |
735 | BT_ASSERT(sc->event_classes); | |
736 | sc->event_classes_from_ir = g_hash_table_new(g_direct_hash, | |
737 | g_direct_equal); | |
738 | BT_ASSERT(sc->event_classes_from_ir); | |
ffb5c13c | 739 | sc->packets_have_ts_begin = |
9b24b6aa | 740 | bt_stream_class_packets_have_beginning_default_clock_snapshot( |
ffb5c13c PP |
741 | ir_sc); |
742 | sc->packets_have_ts_end = | |
9b24b6aa | 743 | bt_stream_class_packets_have_end_default_clock_snapshot(ir_sc); |
ffb5c13c PP |
744 | sc->has_discarded_events = |
745 | bt_stream_class_supports_discarded_events(ir_sc); | |
746 | ||
747 | if (sc->has_discarded_events) { | |
748 | sc->discarded_events_has_ts = | |
749 | bt_stream_class_discarded_events_have_default_clock_snapshots( | |
750 | ir_sc); | |
751 | } | |
752 | ||
753 | if (bt_stream_class_supports_discarded_packets(ir_sc)) { | |
754 | sc->discarded_packets_has_ts = | |
755 | bt_stream_class_discarded_packets_have_default_clock_snapshots( | |
756 | ir_sc); | |
757 | } | |
758 | ||
15fe47e0 PP |
759 | g_ptr_array_add(tc->stream_classes, sc); |
760 | return sc; | |
761 | } | |
762 | ||
763 | static inline | |
764 | void fs_sink_ctf_stream_class_destroy(struct fs_sink_ctf_stream_class *sc) | |
765 | { | |
766 | if (!sc) { | |
767 | return; | |
768 | } | |
769 | ||
770 | if (sc->default_clock_class_name) { | |
771 | g_string_free(sc->default_clock_class_name, TRUE); | |
772 | sc->default_clock_class_name = NULL; | |
773 | } | |
774 | ||
775 | if (sc->event_classes) { | |
776 | g_ptr_array_free(sc->event_classes, TRUE); | |
777 | sc->event_classes = NULL; | |
778 | } | |
779 | ||
780 | if (sc->event_classes_from_ir) { | |
781 | g_hash_table_destroy(sc->event_classes_from_ir); | |
782 | sc->event_classes_from_ir = NULL; | |
783 | } | |
784 | ||
785 | fs_sink_ctf_field_class_destroy(sc->packet_context_fc); | |
786 | sc->packet_context_fc = NULL; | |
787 | fs_sink_ctf_field_class_destroy(sc->event_common_context_fc); | |
788 | sc->event_common_context_fc = NULL; | |
789 | g_free(sc); | |
790 | } | |
791 | ||
792 | static inline | |
793 | void fs_sink_ctf_stream_class_append_event_class( | |
794 | struct fs_sink_ctf_stream_class *sc, | |
795 | struct fs_sink_ctf_event_class *ec) | |
796 | { | |
797 | g_ptr_array_add(sc->event_classes, ec); | |
798 | } | |
799 | ||
800 | static inline | |
801 | void fs_sink_ctf_trace_class_destroy(struct fs_sink_ctf_trace_class *tc) | |
802 | { | |
803 | if (!tc) { | |
804 | return; | |
805 | } | |
806 | ||
807 | if (tc->stream_classes) { | |
808 | g_ptr_array_free(tc->stream_classes, TRUE); | |
809 | tc->stream_classes = NULL; | |
810 | } | |
811 | ||
812 | g_free(tc); | |
813 | } | |
814 | ||
815 | static inline | |
816 | struct fs_sink_ctf_trace_class *fs_sink_ctf_trace_class_create( | |
817 | const bt_trace_class *ir_tc) | |
818 | { | |
819 | struct fs_sink_ctf_trace_class *tc = | |
820 | g_new0(struct fs_sink_ctf_trace_class, 1); | |
821 | ||
822 | BT_ASSERT(tc); | |
823 | ||
824 | if (bt_uuid_generate(tc->uuid)) { | |
825 | fs_sink_ctf_trace_class_destroy(tc); | |
826 | tc = NULL; | |
827 | goto end; | |
828 | } | |
829 | ||
830 | tc->ir_tc = ir_tc; | |
831 | tc->stream_classes = g_ptr_array_new_with_free_func( | |
832 | (GDestroyNotify) fs_sink_ctf_stream_class_destroy); | |
833 | BT_ASSERT(tc->stream_classes); | |
834 | ||
835 | end: | |
836 | return tc; | |
837 | } | |
838 | ||
839 | static inline | |
840 | bool fs_sink_ctf_ist_valid_identifier(const char *name) | |
841 | { | |
842 | const char *at; | |
843 | uint64_t i; | |
844 | bool ist_valid = true; | |
845 | static const char *reserved_keywords[] = { | |
846 | "align", | |
847 | "callsite", | |
848 | "const", | |
849 | "char", | |
850 | "clock", | |
851 | "double", | |
852 | "enum", | |
853 | "env", | |
854 | "event", | |
855 | "floating_point", | |
856 | "float", | |
857 | "integer", | |
858 | "int", | |
859 | "long", | |
860 | "short", | |
861 | "signed", | |
862 | "stream", | |
863 | "string", | |
864 | "struct", | |
865 | "trace", | |
866 | "typealias", | |
867 | "typedef", | |
868 | "unsigned", | |
869 | "variant", | |
870 | "void", | |
871 | "_Bool", | |
872 | "_Complex", | |
873 | "_Imaginary", | |
874 | }; | |
875 | ||
876 | /* Make sure the name is not a reserved keyword */ | |
877 | for (i = 0; i < sizeof(reserved_keywords) / sizeof(*reserved_keywords); | |
878 | i++) { | |
879 | if (strcmp(name, reserved_keywords[i]) == 0) { | |
880 | ist_valid = false; | |
881 | goto end; | |
882 | } | |
883 | } | |
884 | ||
885 | /* Make sure the name is not an empty string */ | |
886 | if (strlen(name) == 0) { | |
887 | ist_valid = false; | |
888 | goto end; | |
889 | } | |
890 | ||
891 | /* Make sure the name starts with a letter or `_` */ | |
892 | if (!isalpha(name[0]) && name[0] != '_') { | |
893 | ist_valid = false; | |
894 | goto end; | |
895 | } | |
896 | ||
897 | /* Make sure the name only contains letters, digits, and `_` */ | |
898 | for (at = name; *at != '\0'; at++) { | |
899 | if (!isalnum(*at) && *at != '_') { | |
900 | ist_valid = false; | |
901 | goto end; | |
902 | } | |
903 | } | |
904 | ||
905 | end: | |
906 | return ist_valid; | |
907 | } | |
908 | ||
909 | static inline | |
910 | int fs_sink_ctf_protect_name(GString *name) | |
911 | { | |
912 | int ret = 0; | |
913 | ||
914 | if (!fs_sink_ctf_ist_valid_identifier(name->str)) { | |
915 | ret = -1; | |
916 | goto end; | |
917 | } | |
918 | ||
919 | /* Prepend `_` to protect it */ | |
920 | g_string_prepend_c(name, '_'); | |
921 | ||
922 | end: | |
923 | return ret; | |
924 | } | |
925 | ||
926 | #endif /* BABELTRACE_PLUGIN_CTF_FS_SINK_FS_SINK_CTF_META_H */ |