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