Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * event-types.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Event Types |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
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 | #include <babeltrace/ctf-writer/event-types.h> | |
adc315b8 | 30 | #include <babeltrace/ctf-ir/event-types-internal.h> |
654c1444 | 31 | #include <babeltrace/ctf-ir/utils.h> |
6cfb906f | 32 | #include <babeltrace/ctf-ir/clock.h> |
273b65be JG |
33 | #include <babeltrace/ctf-writer/writer-internal.h> |
34 | #include <babeltrace/compiler.h> | |
35 | #include <babeltrace/endian.h> | |
36 | #include <float.h> | |
37 | #include <inttypes.h> | |
a39fa057 | 38 | #include <stdlib.h> |
273b65be JG |
39 | |
40 | struct range_overlap_query { | |
b92ddaaa JG |
41 | union { |
42 | uint64_t _unsigned; | |
43 | int64_t _signed; | |
44 | } range_start; | |
45 | ||
46 | union { | |
47 | uint64_t _unsigned; | |
48 | int64_t _signed; | |
49 | } range_end; | |
273b65be JG |
50 | int overlaps; |
51 | GQuark mapping_name; | |
52 | }; | |
53 | ||
2f2d8e05 JG |
54 | static |
55 | void bt_ctf_field_type_destroy(struct bt_ctf_ref *); | |
273b65be JG |
56 | static |
57 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *); | |
58 | static | |
59 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *); | |
60 | static | |
61 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *); | |
62 | static | |
63 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *); | |
64 | static | |
65 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *); | |
66 | static | |
67 | void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *); | |
68 | static | |
69 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *); | |
70 | static | |
71 | void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *); | |
72 | ||
73 | static | |
74 | void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = { | |
75 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy, | |
76 | [CTF_TYPE_ENUM] = | |
77 | bt_ctf_field_type_enumeration_destroy, | |
78 | [CTF_TYPE_FLOAT] = | |
79 | bt_ctf_field_type_floating_point_destroy, | |
80 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy, | |
81 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy, | |
82 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy, | |
83 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy, | |
84 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_destroy, | |
85 | }; | |
86 | ||
87 | static | |
88 | void generic_field_type_freeze(struct bt_ctf_field_type *); | |
89 | static | |
90 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *); | |
91 | static | |
92 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *); | |
93 | static | |
94 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *); | |
95 | static | |
96 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *); | |
97 | static | |
98 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *); | |
99 | ||
100 | static | |
101 | type_freeze_func const type_freeze_funcs[] = { | |
102 | [CTF_TYPE_INTEGER] = generic_field_type_freeze, | |
103 | [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze, | |
104 | [CTF_TYPE_FLOAT] = generic_field_type_freeze, | |
105 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze, | |
106 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze, | |
107 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze, | |
108 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze, | |
109 | [CTF_TYPE_STRING] = generic_field_type_freeze, | |
110 | }; | |
111 | ||
112 | static | |
113 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *, | |
114 | struct metadata_context *); | |
115 | static | |
116 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *, | |
117 | struct metadata_context *); | |
118 | static | |
119 | int bt_ctf_field_type_floating_point_serialize( | |
120 | struct bt_ctf_field_type *, struct metadata_context *); | |
121 | static | |
122 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *, | |
123 | struct metadata_context *); | |
124 | static | |
125 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *, | |
126 | struct metadata_context *); | |
127 | static | |
128 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *, | |
129 | struct metadata_context *); | |
130 | static | |
131 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *, | |
132 | struct metadata_context *); | |
133 | static | |
134 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *, | |
135 | struct metadata_context *); | |
136 | ||
137 | static | |
138 | type_serialize_func const type_serialize_funcs[] = { | |
139 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize, | |
140 | [CTF_TYPE_ENUM] = | |
141 | bt_ctf_field_type_enumeration_serialize, | |
142 | [CTF_TYPE_FLOAT] = | |
143 | bt_ctf_field_type_floating_point_serialize, | |
144 | [CTF_TYPE_STRUCT] = | |
145 | bt_ctf_field_type_structure_serialize, | |
146 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize, | |
147 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize, | |
148 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize, | |
149 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_serialize, | |
150 | }; | |
151 | ||
152 | static | |
153 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *, | |
c35a1669 JG |
154 | int byte_order, int set_native); |
155 | static | |
156 | void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *, | |
157 | int byte_order, int set_native); | |
273b65be JG |
158 | static |
159 | void bt_ctf_field_type_floating_point_set_byte_order( | |
c35a1669 JG |
160 | struct bt_ctf_field_type *, int byte_order, int set_native); |
161 | static | |
162 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *, | |
163 | int byte_order, int set_native); | |
164 | static | |
165 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *, | |
166 | int byte_order, int set_native); | |
167 | static | |
168 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *, | |
169 | int byte_order, int set_native); | |
170 | static | |
171 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *, | |
172 | int byte_order, int set_native); | |
273b65be | 173 | |
c35a1669 | 174 | /* The set_native flag only set the byte order if it is set to native */ |
273b65be JG |
175 | static |
176 | void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *, | |
c35a1669 JG |
177 | int byte_order, int set_native) = { |
178 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_set_byte_order, | |
179 | [CTF_TYPE_ENUM] = | |
180 | bt_ctf_field_type_enumeration_set_byte_order, | |
273b65be JG |
181 | [CTF_TYPE_FLOAT] = |
182 | bt_ctf_field_type_floating_point_set_byte_order, | |
c35a1669 JG |
183 | [CTF_TYPE_STRUCT] = |
184 | bt_ctf_field_type_structure_set_byte_order, | |
185 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_set_byte_order, | |
186 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_set_byte_order, | |
187 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order, | |
188 | [CTF_TYPE_STRING] = NULL, | |
273b65be JG |
189 | }; |
190 | ||
24724933 JG |
191 | static |
192 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( | |
193 | struct bt_ctf_field_type *); | |
194 | static | |
195 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( | |
196 | struct bt_ctf_field_type *); | |
197 | static | |
198 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( | |
199 | struct bt_ctf_field_type *); | |
200 | static | |
201 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( | |
202 | struct bt_ctf_field_type *); | |
203 | static | |
204 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( | |
205 | struct bt_ctf_field_type *); | |
206 | static | |
207 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( | |
208 | struct bt_ctf_field_type *); | |
209 | static | |
210 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( | |
211 | struct bt_ctf_field_type *); | |
212 | static | |
213 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( | |
214 | struct bt_ctf_field_type *); | |
215 | ||
216 | static | |
217 | struct bt_ctf_field_type *(* const type_copy_funcs[])( | |
218 | struct bt_ctf_field_type *) = { | |
219 | [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_copy, | |
220 | [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_copy, | |
221 | [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_copy, | |
222 | [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_copy, | |
223 | [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_copy, | |
224 | [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_copy, | |
225 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_copy, | |
226 | [CTF_TYPE_STRING] = bt_ctf_field_type_string_copy, | |
227 | }; | |
228 | ||
273b65be JG |
229 | static |
230 | void destroy_enumeration_mapping(struct enumeration_mapping *mapping) | |
231 | { | |
232 | g_free(mapping); | |
233 | } | |
234 | ||
235 | static | |
236 | void destroy_structure_field(struct structure_field *field) | |
237 | { | |
238 | if (field->type) { | |
239 | bt_ctf_field_type_put(field->type); | |
240 | } | |
241 | ||
242 | g_free(field); | |
243 | } | |
244 | ||
245 | static | |
246 | void check_ranges_overlap(gpointer element, gpointer query) | |
247 | { | |
248 | struct enumeration_mapping *mapping = element; | |
249 | struct range_overlap_query *overlap_query = query; | |
250 | ||
b92ddaaa JG |
251 | if (mapping->range_start._signed <= overlap_query->range_end._signed |
252 | && overlap_query->range_start._signed <= | |
253 | mapping->range_end._signed) { | |
254 | overlap_query->overlaps = 1; | |
255 | overlap_query->mapping_name = mapping->string; | |
256 | } | |
257 | ||
258 | overlap_query->overlaps |= | |
259 | mapping->string == overlap_query->mapping_name; | |
260 | } | |
261 | ||
262 | static | |
263 | void check_ranges_overlap_unsigned(gpointer element, gpointer query) | |
264 | { | |
265 | struct enumeration_mapping *mapping = element; | |
266 | struct range_overlap_query *overlap_query = query; | |
267 | ||
268 | if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned | |
269 | && overlap_query->range_start._unsigned <= | |
270 | mapping->range_end._unsigned) { | |
273b65be JG |
271 | overlap_query->overlaps = 1; |
272 | overlap_query->mapping_name = mapping->string; | |
273 | } | |
274 | ||
275 | overlap_query->overlaps |= | |
276 | mapping->string == overlap_query->mapping_name; | |
277 | } | |
278 | ||
b92ddaaa JG |
279 | static |
280 | gint compare_enumeration_mappings_signed(struct enumeration_mapping **a, | |
281 | struct enumeration_mapping **b) | |
282 | { | |
283 | return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1; | |
284 | } | |
285 | ||
286 | static | |
287 | gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a, | |
288 | struct enumeration_mapping **b) | |
289 | { | |
290 | return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1; | |
291 | } | |
292 | ||
273b65be JG |
293 | static |
294 | void bt_ctf_field_type_init(struct bt_ctf_field_type *type) | |
295 | { | |
296 | enum ctf_type_id type_id = type->declaration->id; | |
c4e511df | 297 | int ret; |
273b65be JG |
298 | |
299 | assert(type && (type_id > CTF_TYPE_UNKNOWN) && | |
300 | (type_id < NR_CTF_TYPES)); | |
301 | ||
302 | bt_ctf_ref_init(&type->ref_count); | |
303 | type->freeze = type_freeze_funcs[type_id]; | |
304 | type->serialize = type_serialize_funcs[type_id]; | |
c4e511df MD |
305 | ret = bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_NATIVE); |
306 | assert(!ret); | |
273b65be JG |
307 | type->declaration->alignment = 1; |
308 | } | |
309 | ||
310 | static | |
311 | int add_structure_field(GPtrArray *fields, | |
312 | GHashTable *field_name_to_index, | |
313 | struct bt_ctf_field_type *field_type, | |
314 | const char *field_name) | |
315 | { | |
316 | int ret = 0; | |
317 | GQuark name_quark = g_quark_from_string(field_name); | |
318 | struct structure_field *field; | |
319 | ||
320 | /* Make sure structure does not contain a field of the same name */ | |
fe0fe95c JG |
321 | if (g_hash_table_lookup_extended(field_name_to_index, |
322 | GUINT_TO_POINTER(name_quark), NULL, NULL)) { | |
273b65be JG |
323 | ret = -1; |
324 | goto end; | |
325 | } | |
326 | ||
327 | field = g_new0(struct structure_field, 1); | |
328 | if (!field) { | |
329 | ret = -1; | |
330 | goto end; | |
331 | } | |
332 | ||
333 | bt_ctf_field_type_get(field_type); | |
334 | field->name = name_quark; | |
335 | field->type = field_type; | |
336 | g_hash_table_insert(field_name_to_index, | |
337 | (gpointer) (unsigned long) name_quark, | |
338 | (gpointer) (unsigned long) fields->len); | |
339 | g_ptr_array_add(fields, field); | |
273b65be JG |
340 | end: |
341 | return ret; | |
342 | } | |
343 | ||
2f2d8e05 JG |
344 | static |
345 | void bt_ctf_field_type_destroy(struct bt_ctf_ref *ref) | |
346 | { | |
347 | struct bt_ctf_field_type *type; | |
348 | enum ctf_type_id type_id; | |
349 | ||
350 | if (!ref) { | |
351 | return; | |
352 | } | |
353 | ||
354 | type = container_of(ref, struct bt_ctf_field_type, ref_count); | |
355 | type_id = type->declaration->id; | |
356 | if (type_id <= CTF_TYPE_UNKNOWN || | |
357 | type_id >= NR_CTF_TYPES) { | |
358 | return; | |
359 | } | |
360 | ||
2f2d8e05 JG |
361 | type_destroy_funcs[type_id](ref); |
362 | } | |
363 | ||
9ce21c30 JG |
364 | BT_HIDDEN |
365 | int bt_ctf_field_type_validate(struct bt_ctf_field_type *type) | |
366 | { | |
367 | int ret = 0; | |
368 | ||
369 | if (!type) { | |
370 | ret = -1; | |
371 | goto end; | |
372 | } | |
373 | ||
5d161ecc JG |
374 | switch (type->declaration->id) { |
375 | case CTF_TYPE_ENUM: | |
376 | { | |
9ce21c30 JG |
377 | struct bt_ctf_field_type_enumeration *enumeration = |
378 | container_of(type, struct bt_ctf_field_type_enumeration, | |
379 | parent); | |
380 | ||
5d161ecc | 381 | /* Ensure enum has entries */ |
9ce21c30 | 382 | ret = enumeration->entries->len ? 0 : -1; |
5d161ecc JG |
383 | break; |
384 | } | |
c6c0ca42 JG |
385 | case CTF_TYPE_SEQUENCE: |
386 | { | |
387 | struct bt_ctf_field_type_sequence *sequence = | |
388 | container_of(type, struct bt_ctf_field_type_sequence, | |
389 | parent); | |
390 | ||
391 | /* length field name should be set at this point */ | |
392 | ret = sequence->length_field_name->len ? 0 : -1; | |
393 | break; | |
394 | } | |
395 | case CTF_TYPE_VARIANT: | |
396 | { | |
397 | struct bt_ctf_field_type_variant *variant = | |
398 | container_of(type, struct bt_ctf_field_type_variant, | |
399 | parent); | |
400 | ||
401 | if (variant->tag_name->len == 0 || !variant->tag) { | |
402 | ret = -1; | |
403 | } | |
404 | break; | |
405 | } | |
5d161ecc JG |
406 | default: |
407 | break; | |
9ce21c30 JG |
408 | } |
409 | end: | |
410 | return ret; | |
411 | } | |
412 | ||
273b65be JG |
413 | struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) |
414 | { | |
415 | struct bt_ctf_field_type_integer *integer = | |
416 | g_new0(struct bt_ctf_field_type_integer, 1); | |
417 | ||
1f02e293 | 418 | if (!integer || size == 0 || size > 64) { |
273b65be JG |
419 | return NULL; |
420 | } | |
421 | ||
422 | integer->parent.declaration = &integer->declaration.p; | |
423 | integer->parent.declaration->id = CTF_TYPE_INTEGER; | |
424 | integer->declaration.len = size; | |
425 | integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL; | |
426 | integer->declaration.encoding = CTF_STRING_NONE; | |
427 | bt_ctf_field_type_init(&integer->parent); | |
428 | return &integer->parent; | |
429 | } | |
430 | ||
b92ddaaa JG |
431 | int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type) |
432 | { | |
433 | int ret = 0; | |
434 | struct bt_ctf_field_type_integer *integer; | |
435 | ||
436 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { | |
437 | ret = -1; | |
438 | goto end; | |
439 | } | |
440 | ||
441 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
442 | ret = (int) integer->declaration.len; | |
443 | end: | |
444 | return ret; | |
445 | } | |
446 | ||
447 | int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type) | |
448 | { | |
449 | int ret = 0; | |
450 | struct bt_ctf_field_type_integer *integer; | |
451 | ||
452 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { | |
453 | ret = -1; | |
454 | goto end; | |
455 | } | |
456 | ||
457 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
458 | ret = integer->declaration.signedness; | |
459 | end: | |
460 | return ret; | |
461 | } | |
462 | ||
273b65be JG |
463 | int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type, |
464 | int is_signed) | |
465 | { | |
466 | int ret = 0; | |
467 | struct bt_ctf_field_type_integer *integer; | |
468 | ||
469 | if (!type || type->frozen || | |
470 | type->declaration->id != CTF_TYPE_INTEGER) { | |
471 | ret = -1; | |
472 | goto end; | |
473 | } | |
474 | ||
475 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
273b65be JG |
476 | integer->declaration.signedness = !!is_signed; |
477 | end: | |
478 | return ret; | |
479 | } | |
480 | ||
b92ddaaa JG |
481 | enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( |
482 | struct bt_ctf_field_type *type) | |
483 | { | |
484 | enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN; | |
485 | struct bt_ctf_field_type_integer *integer; | |
486 | ||
487 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { | |
488 | goto end; | |
489 | } | |
490 | ||
491 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
492 | ret = integer->declaration.base; | |
493 | end: | |
494 | return ret; | |
495 | } | |
496 | ||
273b65be JG |
497 | int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type, |
498 | enum bt_ctf_integer_base base) | |
499 | { | |
500 | int ret = 0; | |
501 | ||
502 | if (!type || type->frozen || | |
503 | type->declaration->id != CTF_TYPE_INTEGER) { | |
504 | ret = -1; | |
505 | goto end; | |
506 | } | |
507 | ||
508 | switch (base) { | |
509 | case BT_CTF_INTEGER_BASE_BINARY: | |
510 | case BT_CTF_INTEGER_BASE_OCTAL: | |
511 | case BT_CTF_INTEGER_BASE_DECIMAL: | |
512 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: | |
513 | { | |
514 | struct bt_ctf_field_type_integer *integer = container_of(type, | |
515 | struct bt_ctf_field_type_integer, parent); | |
516 | integer->declaration.base = base; | |
517 | break; | |
518 | } | |
519 | default: | |
520 | ret = -1; | |
521 | } | |
522 | end: | |
523 | return ret; | |
524 | } | |
525 | ||
b92ddaaa JG |
526 | enum ctf_string_encoding bt_ctf_field_type_integer_get_encoding( |
527 | struct bt_ctf_field_type *type) | |
528 | { | |
529 | enum ctf_string_encoding ret = CTF_STRING_UNKNOWN; | |
530 | struct bt_ctf_field_type_integer *integer; | |
531 | ||
532 | if (!type || type->declaration->id != CTF_TYPE_INTEGER) { | |
533 | goto end; | |
534 | } | |
535 | ||
536 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
537 | ret = integer->declaration.encoding; | |
538 | end: | |
539 | return ret; | |
540 | } | |
541 | ||
273b65be JG |
542 | int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type, |
543 | enum ctf_string_encoding encoding) | |
544 | { | |
545 | int ret = 0; | |
546 | struct bt_ctf_field_type_integer *integer; | |
547 | ||
548 | if (!type || type->frozen || | |
549 | (type->declaration->id != CTF_TYPE_INTEGER) || | |
550 | (encoding < CTF_STRING_NONE) || | |
551 | (encoding >= CTF_STRING_UNKNOWN)) { | |
552 | ret = -1; | |
553 | goto end; | |
554 | } | |
555 | ||
556 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
557 | integer->declaration.encoding = encoding; | |
558 | end: | |
559 | return ret; | |
560 | } | |
561 | ||
6cfb906f JG |
562 | struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock( |
563 | struct bt_ctf_field_type *type) | |
564 | { | |
565 | struct bt_ctf_field_type_integer *integer; | |
566 | struct bt_ctf_clock *clock = NULL; | |
567 | ||
568 | if (!type) { | |
569 | goto end; | |
570 | } | |
571 | ||
572 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
573 | clock = integer->mapped_clock; | |
574 | if (clock) { | |
575 | bt_ctf_clock_get(clock); | |
576 | } | |
577 | end: | |
578 | return clock; | |
579 | } | |
580 | ||
581 | int bt_ctf_field_type_integer_set_mapped_clock( | |
582 | struct bt_ctf_field_type *type, | |
583 | struct bt_ctf_clock *clock) | |
584 | { | |
585 | struct bt_ctf_field_type_integer *integer; | |
586 | int ret = 0; | |
587 | ||
588 | if (!type || type->frozen) { | |
589 | ret = -1; | |
590 | goto end; | |
591 | } | |
592 | ||
593 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
594 | if (integer->mapped_clock) { | |
595 | bt_ctf_clock_put(integer->mapped_clock); | |
596 | } | |
597 | ||
598 | if (clock) { | |
599 | bt_ctf_clock_get(clock); | |
600 | } | |
601 | ||
602 | integer->mapped_clock = clock; | |
603 | end: | |
604 | return ret; | |
605 | } | |
606 | ||
273b65be JG |
607 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( |
608 | struct bt_ctf_field_type *integer_container_type) | |
609 | { | |
610 | struct bt_ctf_field_type_enumeration *enumeration = NULL; | |
611 | ||
612 | if (!integer_container_type) { | |
613 | goto error; | |
614 | } | |
615 | ||
2a610bb7 JG |
616 | if (integer_container_type->declaration->id != CTF_TYPE_INTEGER) { |
617 | goto error; | |
618 | } | |
619 | ||
273b65be JG |
620 | enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1); |
621 | if (!enumeration) { | |
622 | goto error; | |
623 | } | |
624 | ||
625 | enumeration->parent.declaration = &enumeration->declaration.p; | |
626 | enumeration->parent.declaration->id = CTF_TYPE_ENUM; | |
627 | bt_ctf_field_type_get(integer_container_type); | |
628 | enumeration->container = integer_container_type; | |
629 | enumeration->entries = g_ptr_array_new_with_free_func( | |
630 | (GDestroyNotify)destroy_enumeration_mapping); | |
631 | bt_ctf_field_type_init(&enumeration->parent); | |
632 | return &enumeration->parent; | |
633 | error: | |
634 | g_free(enumeration); | |
635 | return NULL; | |
636 | } | |
637 | ||
b92ddaaa JG |
638 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type( |
639 | struct bt_ctf_field_type *type) | |
640 | { | |
641 | struct bt_ctf_field_type *container_type = NULL; | |
642 | struct bt_ctf_field_type_enumeration *enumeration_type; | |
643 | ||
644 | if (!type) { | |
645 | goto end; | |
646 | } | |
647 | ||
648 | if (type->declaration->id != CTF_TYPE_ENUM) { | |
649 | goto end; | |
650 | } | |
651 | ||
652 | enumeration_type = container_of(type, | |
653 | struct bt_ctf_field_type_enumeration, parent); | |
654 | container_type = enumeration_type->container; | |
655 | bt_ctf_field_type_get(container_type); | |
656 | end: | |
657 | return container_type; | |
658 | } | |
659 | ||
273b65be JG |
660 | int bt_ctf_field_type_enumeration_add_mapping( |
661 | struct bt_ctf_field_type *type, const char *string, | |
662 | int64_t range_start, int64_t range_end) | |
663 | { | |
664 | int ret = 0; | |
665 | GQuark mapping_name; | |
666 | struct enumeration_mapping *mapping; | |
667 | struct bt_ctf_field_type_enumeration *enumeration; | |
668 | struct range_overlap_query query; | |
a39fa057 | 669 | char *escaped_string; |
273b65be JG |
670 | |
671 | if (!type || (type->declaration->id != CTF_TYPE_ENUM) || | |
672 | type->frozen || | |
673 | (range_end < range_start)) { | |
674 | ret = -1; | |
675 | goto end; | |
676 | } | |
677 | ||
a39fa057 | 678 | if (!string || strlen(string) == 0) { |
273b65be JG |
679 | ret = -1; |
680 | goto end; | |
681 | } | |
682 | ||
a39fa057 JG |
683 | escaped_string = g_strescape(string, NULL); |
684 | if (!escaped_string) { | |
685 | ret = -1; | |
686 | goto end; | |
687 | } | |
688 | ||
689 | mapping_name = g_quark_from_string(escaped_string); | |
b92ddaaa JG |
690 | query = (struct range_overlap_query) { |
691 | .range_start._signed = range_start, | |
692 | .range_end._signed = range_end, | |
273b65be JG |
693 | .mapping_name = mapping_name, |
694 | .overlaps = 0 }; | |
695 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, | |
696 | parent); | |
697 | ||
698 | /* Check that the range does not overlap with one already present */ | |
699 | g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query); | |
700 | if (query.overlaps) { | |
701 | ret = -1; | |
a39fa057 | 702 | goto error_free; |
273b65be JG |
703 | } |
704 | ||
705 | mapping = g_new(struct enumeration_mapping, 1); | |
706 | if (!mapping) { | |
707 | ret = -1; | |
a39fa057 | 708 | goto error_free; |
273b65be JG |
709 | } |
710 | ||
b92ddaaa JG |
711 | *mapping = (struct enumeration_mapping) { |
712 | .range_start._signed = range_start, | |
713 | .range_end._signed = range_end, .string = mapping_name}; | |
273b65be | 714 | g_ptr_array_add(enumeration->entries, mapping); |
b92ddaaa JG |
715 | g_ptr_array_sort(enumeration->entries, |
716 | (GCompareFunc)compare_enumeration_mappings_signed); | |
717 | error_free: | |
718 | free(escaped_string); | |
719 | end: | |
720 | return ret; | |
721 | } | |
722 | ||
723 | int bt_ctf_field_type_enumeration_add_mapping_unsigned( | |
724 | struct bt_ctf_field_type *type, const char *string, | |
725 | uint64_t range_start, uint64_t range_end) | |
726 | { | |
727 | int ret = 0; | |
728 | GQuark mapping_name; | |
729 | struct enumeration_mapping *mapping; | |
730 | struct bt_ctf_field_type_enumeration *enumeration; | |
731 | struct range_overlap_query query; | |
732 | char *escaped_string; | |
733 | ||
734 | if (!type || (type->declaration->id != CTF_TYPE_ENUM) || | |
735 | type->frozen || | |
736 | (range_end < range_start)) { | |
737 | ret = -1; | |
738 | goto end; | |
739 | } | |
740 | ||
741 | if (!string || strlen(string) == 0) { | |
742 | ret = -1; | |
743 | goto end; | |
744 | } | |
745 | ||
746 | escaped_string = g_strescape(string, NULL); | |
747 | if (!escaped_string) { | |
748 | ret = -1; | |
749 | goto end; | |
750 | } | |
751 | ||
752 | mapping_name = g_quark_from_string(escaped_string); | |
753 | query = (struct range_overlap_query) { | |
754 | .range_start._unsigned = range_start, | |
755 | .range_end._unsigned = range_end, | |
756 | .mapping_name = mapping_name, | |
757 | .overlaps = 0 }; | |
758 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, | |
759 | parent); | |
760 | ||
761 | /* Check that the range does not overlap with one already present */ | |
762 | g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned, | |
763 | &query); | |
764 | if (query.overlaps) { | |
765 | ret = -1; | |
766 | goto error_free; | |
767 | } | |
768 | ||
769 | mapping = g_new(struct enumeration_mapping, 1); | |
770 | if (!mapping) { | |
771 | ret = -1; | |
772 | goto error_free; | |
773 | } | |
774 | ||
775 | *mapping = (struct enumeration_mapping) { | |
776 | .range_start._unsigned = range_start, | |
777 | .range_end._unsigned = range_end, .string = mapping_name}; | |
778 | g_ptr_array_add(enumeration->entries, mapping); | |
779 | g_ptr_array_sort(enumeration->entries, | |
780 | (GCompareFunc)compare_enumeration_mappings_unsigned); | |
a39fa057 JG |
781 | error_free: |
782 | free(escaped_string); | |
273b65be JG |
783 | end: |
784 | return ret; | |
785 | } | |
786 | ||
e5958c30 JG |
787 | const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned( |
788 | struct bt_ctf_field_type_enumeration *enumeration_type, | |
789 | uint64_t value) | |
790 | { | |
791 | const char *name = NULL; | |
792 | struct range_overlap_query query = | |
793 | (struct range_overlap_query) { | |
b92ddaaa JG |
794 | .range_start._unsigned = value, |
795 | .range_end._unsigned = value, | |
e5958c30 JG |
796 | .overlaps = 0 }; |
797 | ||
b92ddaaa JG |
798 | g_ptr_array_foreach(enumeration_type->entries, |
799 | check_ranges_overlap_unsigned, | |
e5958c30 JG |
800 | &query); |
801 | if (!query.overlaps) { | |
802 | goto end; | |
803 | } | |
804 | ||
805 | name = g_quark_to_string(query.mapping_name); | |
806 | end: | |
807 | return name; | |
808 | } | |
809 | ||
810 | const char *bt_ctf_field_type_enumeration_get_mapping_name_signed( | |
811 | struct bt_ctf_field_type_enumeration *enumeration_type, | |
812 | int64_t value) | |
813 | { | |
814 | const char *name = NULL; | |
815 | struct range_overlap_query query = | |
816 | (struct range_overlap_query) { | |
b92ddaaa JG |
817 | .range_start._signed = value, |
818 | .range_end._signed = value, | |
e5958c30 JG |
819 | .overlaps = 0 }; |
820 | ||
821 | g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap, | |
822 | &query); | |
823 | if (!query.overlaps) { | |
824 | goto end; | |
825 | } | |
826 | ||
827 | name = g_quark_to_string(query.mapping_name); | |
828 | end: | |
829 | return name; | |
830 | } | |
831 | ||
074ee56d | 832 | int bt_ctf_field_type_enumeration_get_mapping_count( |
b92ddaaa JG |
833 | struct bt_ctf_field_type *type) |
834 | { | |
074ee56d | 835 | int ret = 0; |
b92ddaaa JG |
836 | struct bt_ctf_field_type_enumeration *enumeration; |
837 | ||
838 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { | |
839 | ret = -1; | |
840 | goto end; | |
841 | } | |
842 | ||
843 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, | |
844 | parent); | |
074ee56d | 845 | ret = (int) enumeration->entries->len; |
b92ddaaa JG |
846 | end: |
847 | return ret; | |
848 | } | |
849 | ||
850 | static inline | |
851 | struct enumeration_mapping *get_enumeration_mapping( | |
074ee56d | 852 | struct bt_ctf_field_type *type, int index) |
b92ddaaa JG |
853 | { |
854 | struct enumeration_mapping *mapping = NULL; | |
855 | struct bt_ctf_field_type_enumeration *enumeration; | |
856 | ||
857 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, | |
858 | parent); | |
859 | if (index >= enumeration->entries->len) { | |
860 | goto end; | |
861 | } | |
862 | ||
863 | mapping = g_ptr_array_index(enumeration->entries, index); | |
864 | end: | |
865 | return mapping; | |
866 | } | |
867 | ||
868 | int bt_ctf_field_type_enumeration_get_mapping( | |
074ee56d | 869 | struct bt_ctf_field_type *type, int index, |
b92ddaaa JG |
870 | const char **string, int64_t *range_start, int64_t *range_end) |
871 | { | |
872 | struct enumeration_mapping *mapping; | |
873 | int ret = 0; | |
874 | ||
074ee56d | 875 | if (!type || index < 0 || !string || !range_start || !range_end || |
b92ddaaa JG |
876 | (type->declaration->id != CTF_TYPE_ENUM)) { |
877 | ret = -1; | |
878 | goto end; | |
879 | } | |
880 | ||
881 | mapping = get_enumeration_mapping(type, index); | |
882 | if (!mapping) { | |
883 | ret = -1; | |
884 | goto end; | |
885 | } | |
886 | ||
887 | *string = g_quark_to_string(mapping->string); | |
888 | *range_start = mapping->range_start._signed; | |
889 | *range_end = mapping->range_end._signed; | |
890 | end: | |
891 | return ret; | |
892 | } | |
893 | ||
894 | int bt_ctf_field_type_enumeration_get_mapping_unsigned( | |
074ee56d | 895 | struct bt_ctf_field_type *type, int index, |
b92ddaaa JG |
896 | const char **string, uint64_t *range_start, uint64_t *range_end) |
897 | { | |
898 | struct enumeration_mapping *mapping; | |
899 | int ret = 0; | |
900 | ||
074ee56d | 901 | if (!type || index < 0 || !string || !range_start || !range_end || |
b92ddaaa JG |
902 | (type->declaration->id != CTF_TYPE_ENUM)) { |
903 | ret = -1; | |
904 | goto end; | |
905 | } | |
906 | ||
907 | mapping = get_enumeration_mapping(type, index); | |
908 | if (!mapping) { | |
909 | ret = -1; | |
910 | goto end; | |
911 | } | |
912 | ||
913 | *string = g_quark_to_string(mapping->string); | |
914 | *range_start = mapping->range_start._unsigned; | |
915 | *range_end = mapping->range_end._unsigned; | |
916 | end: | |
917 | return ret; | |
918 | } | |
919 | ||
920 | int bt_ctf_field_type_enumeration_get_mapping_index_by_name( | |
074ee56d | 921 | struct bt_ctf_field_type *type, const char *name) |
b92ddaaa | 922 | { |
b92ddaaa JG |
923 | GQuark name_quark; |
924 | struct bt_ctf_field_type_enumeration *enumeration; | |
074ee56d | 925 | int i, ret = 0; |
b92ddaaa | 926 | |
074ee56d | 927 | if (!type || !name || |
b92ddaaa JG |
928 | (type->declaration->id != CTF_TYPE_ENUM)) { |
929 | ret = -1; | |
930 | goto end; | |
931 | } | |
932 | ||
933 | name_quark = g_quark_try_string(name); | |
934 | if (!name_quark) { | |
935 | ret = -1; | |
936 | goto end; | |
937 | } | |
938 | ||
939 | enumeration = container_of(type, | |
940 | struct bt_ctf_field_type_enumeration, parent); | |
941 | for (i = 0; i < enumeration->entries->len; i++) { | |
942 | struct enumeration_mapping *mapping = | |
943 | get_enumeration_mapping(type, i); | |
944 | ||
945 | if (mapping->string == name_quark) { | |
074ee56d | 946 | ret = i; |
b92ddaaa JG |
947 | goto end; |
948 | } | |
949 | } | |
950 | ||
951 | ret = -1; | |
952 | end: | |
953 | return ret; | |
954 | } | |
955 | ||
956 | int bt_ctf_field_type_enumeration_get_mapping_index_by_value( | |
074ee56d | 957 | struct bt_ctf_field_type *type, int64_t value) |
b92ddaaa JG |
958 | { |
959 | struct bt_ctf_field_type_enumeration *enumeration; | |
074ee56d | 960 | int i, ret = 0; |
b92ddaaa | 961 | |
074ee56d | 962 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { |
b92ddaaa JG |
963 | ret = -1; |
964 | goto end; | |
965 | } | |
966 | ||
967 | enumeration = container_of(type, | |
968 | struct bt_ctf_field_type_enumeration, parent); | |
969 | for (i = 0; i < enumeration->entries->len; i++) { | |
970 | struct enumeration_mapping *mapping = | |
971 | get_enumeration_mapping(type, i); | |
972 | ||
973 | if (value >= mapping->range_start._signed && | |
974 | value <= mapping->range_end._signed) { | |
074ee56d | 975 | ret = i; |
b92ddaaa JG |
976 | goto end; |
977 | } | |
978 | } | |
979 | ||
980 | ret = -1; | |
981 | end: | |
982 | return ret; | |
983 | } | |
984 | ||
985 | int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value( | |
074ee56d | 986 | struct bt_ctf_field_type *type, uint64_t value) |
b92ddaaa JG |
987 | { |
988 | struct bt_ctf_field_type_enumeration *enumeration; | |
074ee56d | 989 | int i, ret = 0; |
b92ddaaa | 990 | |
074ee56d | 991 | if (!type || (type->declaration->id != CTF_TYPE_ENUM)) { |
b92ddaaa JG |
992 | ret = -1; |
993 | goto end; | |
994 | } | |
995 | ||
996 | enumeration = container_of(type, | |
997 | struct bt_ctf_field_type_enumeration, parent); | |
998 | for (i = 0; i < enumeration->entries->len; i++) { | |
999 | struct enumeration_mapping *mapping = | |
1000 | get_enumeration_mapping(type, i); | |
1001 | ||
1002 | if (value >= mapping->range_start._unsigned && | |
1003 | value <= mapping->range_end._unsigned) { | |
074ee56d | 1004 | ret = i; |
b92ddaaa JG |
1005 | goto end; |
1006 | } | |
1007 | } | |
1008 | ||
1009 | ret = -1; | |
1010 | end: | |
1011 | return ret; | |
1012 | } | |
1013 | ||
273b65be JG |
1014 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) |
1015 | { | |
1016 | struct bt_ctf_field_type_floating_point *floating_point = | |
1017 | g_new0(struct bt_ctf_field_type_floating_point, 1); | |
1018 | ||
1019 | if (!floating_point) { | |
1020 | goto end; | |
1021 | } | |
1022 | ||
1023 | floating_point->declaration.sign = &floating_point->sign; | |
1024 | floating_point->declaration.mantissa = &floating_point->mantissa; | |
1025 | floating_point->declaration.exp = &floating_point->exp; | |
1026 | floating_point->sign.len = 1; | |
1027 | floating_point->parent.declaration = &floating_point->declaration.p; | |
1028 | floating_point->parent.declaration->id = CTF_TYPE_FLOAT; | |
1029 | floating_point->declaration.exp->len = | |
1030 | sizeof(float) * CHAR_BIT - FLT_MANT_DIG; | |
1031 | floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1; | |
1032 | floating_point->sign.p.alignment = 1; | |
1033 | floating_point->mantissa.p.alignment = 1; | |
1034 | floating_point->exp.p.alignment = 1; | |
1035 | ||
1036 | bt_ctf_field_type_init(&floating_point->parent); | |
1037 | end: | |
1038 | return floating_point ? &floating_point->parent : NULL; | |
1039 | } | |
1040 | ||
b92ddaaa JG |
1041 | int bt_ctf_field_type_floating_point_get_exponent_digits( |
1042 | struct bt_ctf_field_type *type) | |
1043 | { | |
1044 | int ret = 0; | |
1045 | struct bt_ctf_field_type_floating_point *floating_point; | |
1046 | ||
1047 | if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) { | |
1048 | ret = -1; | |
1049 | goto end; | |
1050 | } | |
1051 | ||
1052 | floating_point = container_of(type, | |
1053 | struct bt_ctf_field_type_floating_point, parent); | |
1054 | ret = (int) floating_point->declaration.exp->len; | |
1055 | end: | |
1056 | return ret; | |
1057 | } | |
1058 | ||
273b65be JG |
1059 | int bt_ctf_field_type_floating_point_set_exponent_digits( |
1060 | struct bt_ctf_field_type *type, | |
1061 | unsigned int exponent_digits) | |
1062 | { | |
1063 | int ret = 0; | |
1064 | struct bt_ctf_field_type_floating_point *floating_point; | |
1065 | ||
1066 | if (!type || type->frozen || | |
1067 | (type->declaration->id != CTF_TYPE_FLOAT)) { | |
1068 | ret = -1; | |
1069 | goto end; | |
1070 | } | |
1071 | ||
1072 | floating_point = container_of(type, | |
1073 | struct bt_ctf_field_type_floating_point, parent); | |
1074 | if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) && | |
1075 | (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) && | |
1076 | (exponent_digits != | |
1077 | sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) { | |
1078 | ret = -1; | |
1079 | goto end; | |
1080 | } | |
1081 | ||
1082 | floating_point->declaration.exp->len = exponent_digits; | |
1083 | end: | |
1084 | return ret; | |
1085 | } | |
1086 | ||
b92ddaaa JG |
1087 | int bt_ctf_field_type_floating_point_get_mantissa_digits( |
1088 | struct bt_ctf_field_type *type) | |
1089 | { | |
1090 | int ret = 0; | |
1091 | struct bt_ctf_field_type_floating_point *floating_point; | |
1092 | ||
1093 | if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) { | |
1094 | ret = -1; | |
1095 | goto end; | |
1096 | } | |
1097 | ||
1098 | floating_point = container_of(type, | |
1099 | struct bt_ctf_field_type_floating_point, parent); | |
1100 | ret = (int) floating_point->mantissa.len + 1; | |
1101 | end: | |
1102 | return ret; | |
1103 | } | |
1104 | ||
273b65be JG |
1105 | int bt_ctf_field_type_floating_point_set_mantissa_digits( |
1106 | struct bt_ctf_field_type *type, | |
1107 | unsigned int mantissa_digits) | |
1108 | { | |
1109 | int ret = 0; | |
1110 | struct bt_ctf_field_type_floating_point *floating_point; | |
1111 | ||
1112 | if (!type || type->frozen || | |
1113 | (type->declaration->id != CTF_TYPE_FLOAT)) { | |
1114 | ret = -1; | |
1115 | goto end; | |
1116 | } | |
1117 | ||
1118 | floating_point = container_of(type, | |
1119 | struct bt_ctf_field_type_floating_point, parent); | |
1120 | ||
1121 | if ((mantissa_digits != FLT_MANT_DIG) && | |
1122 | (mantissa_digits != DBL_MANT_DIG) && | |
1123 | (mantissa_digits != LDBL_MANT_DIG)) { | |
1124 | ret = -1; | |
1125 | goto end; | |
1126 | } | |
1127 | ||
1128 | floating_point->declaration.mantissa->len = mantissa_digits - 1; | |
1129 | end: | |
1130 | return ret; | |
1131 | } | |
1132 | ||
1133 | struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) | |
1134 | { | |
1135 | struct bt_ctf_field_type_structure *structure = | |
1136 | g_new0(struct bt_ctf_field_type_structure, 1); | |
1137 | ||
1138 | if (!structure) { | |
1139 | goto error; | |
1140 | } | |
1141 | ||
1142 | structure->parent.declaration = &structure->declaration.p; | |
1143 | structure->parent.declaration->id = CTF_TYPE_STRUCT; | |
273b65be JG |
1144 | structure->fields = g_ptr_array_new_with_free_func( |
1145 | (GDestroyNotify)destroy_structure_field); | |
1146 | structure->field_name_to_index = g_hash_table_new(NULL, NULL); | |
c35a1669 | 1147 | bt_ctf_field_type_init(&structure->parent); |
273b65be JG |
1148 | return &structure->parent; |
1149 | error: | |
1150 | return NULL; | |
1151 | } | |
1152 | ||
1153 | int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type, | |
1154 | struct bt_ctf_field_type *field_type, | |
1155 | const char *field_name) | |
1156 | { | |
1157 | int ret = 0; | |
1158 | struct bt_ctf_field_type_structure *structure; | |
1159 | ||
1160 | if (!type || !field_type || type->frozen || | |
654c1444 | 1161 | bt_ctf_validate_identifier(field_name) || |
9ce21c30 JG |
1162 | (type->declaration->id != CTF_TYPE_STRUCT) || |
1163 | bt_ctf_field_type_validate(field_type)) { | |
e6235f1f | 1164 | ret = -1; |
273b65be JG |
1165 | goto end; |
1166 | } | |
1167 | ||
1168 | structure = container_of(type, | |
1169 | struct bt_ctf_field_type_structure, parent); | |
1170 | if (add_structure_field(structure->fields, | |
1171 | structure->field_name_to_index, field_type, field_name)) { | |
1172 | ret = -1; | |
1173 | goto end; | |
1174 | } | |
1175 | ||
b92ddaaa JG |
1176 | if (type->declaration->alignment < field_type->declaration->alignment) { |
1177 | type->declaration->alignment = | |
1178 | field_type->declaration->alignment; | |
1179 | } | |
1180 | end: | |
1181 | return ret; | |
1182 | } | |
1183 | ||
074ee56d | 1184 | int bt_ctf_field_type_structure_get_field_count( |
b92ddaaa JG |
1185 | struct bt_ctf_field_type *type) |
1186 | { | |
074ee56d | 1187 | int ret = 0; |
b92ddaaa JG |
1188 | struct bt_ctf_field_type_structure *structure; |
1189 | ||
1190 | if (!type || (type->declaration->id != CTF_TYPE_STRUCT)) { | |
1191 | ret = -1; | |
1192 | goto end; | |
1193 | } | |
1194 | ||
1195 | structure = container_of(type, struct bt_ctf_field_type_structure, | |
1196 | parent); | |
074ee56d | 1197 | ret = (int) structure->fields->len; |
b92ddaaa JG |
1198 | end: |
1199 | return ret; | |
1200 | } | |
1201 | ||
1202 | int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type, | |
1203 | const char **field_name, struct bt_ctf_field_type **field_type, | |
074ee56d | 1204 | int index) |
b92ddaaa JG |
1205 | { |
1206 | struct bt_ctf_field_type_structure *structure; | |
1207 | struct structure_field *field; | |
1208 | int ret = 0; | |
1209 | ||
f9b799fc | 1210 | if (!type || index < 0 || (type->declaration->id != CTF_TYPE_STRUCT)) { |
b92ddaaa JG |
1211 | ret = -1; |
1212 | goto end; | |
1213 | } | |
1214 | ||
1215 | structure = container_of(type, struct bt_ctf_field_type_structure, | |
1216 | parent); | |
1217 | if (index >= structure->fields->len) { | |
1218 | ret = -1; | |
1219 | goto end; | |
1220 | } | |
1221 | ||
1222 | field = g_ptr_array_index(structure->fields, index); | |
f9b799fc JG |
1223 | if (field_type) { |
1224 | *field_type = field->type; | |
1225 | bt_ctf_field_type_get(field->type); | |
1226 | } | |
1227 | if (field_name) { | |
1228 | *field_name = g_quark_to_string(field->name); | |
1229 | } | |
b92ddaaa JG |
1230 | end: |
1231 | return ret; | |
1232 | } | |
1233 | ||
1234 | struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( | |
1235 | struct bt_ctf_field_type *type, | |
1236 | const char *name) | |
1237 | { | |
1238 | size_t index; | |
1239 | GQuark name_quark; | |
1240 | struct structure_field *field; | |
1241 | struct bt_ctf_field_type_structure *structure; | |
1242 | struct bt_ctf_field_type *field_type = NULL; | |
1243 | ||
1244 | if (!type || !name) { | |
1245 | goto end; | |
1246 | } | |
1247 | ||
1248 | name_quark = g_quark_try_string(name); | |
1249 | if (!name_quark) { | |
1250 | goto end; | |
1251 | } | |
1252 | ||
1253 | structure = container_of(type, struct bt_ctf_field_type_structure, | |
1254 | parent); | |
1255 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, | |
1256 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { | |
1257 | goto end; | |
273b65be | 1258 | } |
b92ddaaa JG |
1259 | |
1260 | field = structure->fields->pdata[index]; | |
1261 | field_type = field->type; | |
1262 | bt_ctf_field_type_get(field_type); | |
273b65be | 1263 | end: |
b92ddaaa | 1264 | return field_type; |
273b65be JG |
1265 | } |
1266 | ||
1267 | struct bt_ctf_field_type *bt_ctf_field_type_variant_create( | |
1268 | struct bt_ctf_field_type *enum_tag, const char *tag_name) | |
1269 | { | |
1270 | struct bt_ctf_field_type_variant *variant = NULL; | |
1271 | ||
6964b7fd | 1272 | if (tag_name && bt_ctf_validate_identifier(tag_name)) { |
273b65be JG |
1273 | goto error; |
1274 | } | |
1275 | ||
1276 | variant = g_new0(struct bt_ctf_field_type_variant, 1); | |
1277 | if (!variant) { | |
1278 | goto error; | |
1279 | } | |
1280 | ||
1281 | variant->parent.declaration = &variant->declaration.p; | |
1282 | variant->parent.declaration->id = CTF_TYPE_VARIANT; | |
1283 | variant->tag_name = g_string_new(tag_name); | |
273b65be JG |
1284 | variant->field_name_to_index = g_hash_table_new(NULL, NULL); |
1285 | variant->fields = g_ptr_array_new_with_free_func( | |
1286 | (GDestroyNotify)destroy_structure_field); | |
6964b7fd JG |
1287 | if (enum_tag) { |
1288 | bt_ctf_field_type_get(enum_tag); | |
1289 | variant->tag = container_of(enum_tag, | |
1290 | struct bt_ctf_field_type_enumeration, parent); | |
1291 | } | |
1292 | ||
c35a1669 | 1293 | bt_ctf_field_type_init(&variant->parent); |
273b65be JG |
1294 | return &variant->parent; |
1295 | error: | |
1296 | return NULL; | |
1297 | } | |
1298 | ||
b92ddaaa JG |
1299 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type( |
1300 | struct bt_ctf_field_type *type) | |
1301 | { | |
1302 | struct bt_ctf_field_type_variant *variant; | |
1303 | struct bt_ctf_field_type *tag_type = NULL; | |
1304 | ||
1305 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { | |
1306 | goto end; | |
1307 | } | |
1308 | ||
1309 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); | |
6964b7fd JG |
1310 | if (!variant->tag) { |
1311 | goto end; | |
1312 | } | |
1313 | ||
b92ddaaa JG |
1314 | tag_type = &variant->tag->parent; |
1315 | bt_ctf_field_type_get(tag_type); | |
1316 | end: | |
1317 | return tag_type; | |
1318 | } | |
1319 | ||
1320 | const char *bt_ctf_field_type_variant_get_tag_name( | |
1321 | struct bt_ctf_field_type *type) | |
1322 | { | |
1323 | struct bt_ctf_field_type_variant *variant; | |
1324 | const char *tag_name = NULL; | |
1325 | ||
1326 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { | |
1327 | goto end; | |
1328 | } | |
1329 | ||
1330 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); | |
6964b7fd JG |
1331 | if (variant->tag_name->len == 0) { |
1332 | goto end; | |
1333 | } | |
1334 | ||
b92ddaaa JG |
1335 | tag_name = variant->tag_name->str; |
1336 | end: | |
1337 | return tag_name; | |
1338 | } | |
1339 | ||
d9b1ab6d JG |
1340 | int bt_ctf_field_type_variant_set_tag_name( |
1341 | struct bt_ctf_field_type *type, const char *name) | |
1342 | { | |
1343 | int ret = 0; | |
1344 | struct bt_ctf_field_type_variant *variant; | |
1345 | ||
1346 | if (!type || type->frozen || | |
1347 | (type->declaration->id != CTF_TYPE_VARIANT) || | |
1348 | bt_ctf_validate_identifier(name)) { | |
1349 | ret = -1; | |
1350 | goto end; | |
1351 | } | |
1352 | ||
1353 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); | |
1354 | g_string_assign(variant->tag_name, name); | |
1355 | end: | |
1356 | return ret; | |
1357 | } | |
1358 | ||
273b65be JG |
1359 | int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type, |
1360 | struct bt_ctf_field_type *field_type, | |
1361 | const char *field_name) | |
1362 | { | |
1363 | size_t i; | |
1364 | int ret = 0; | |
273b65be JG |
1365 | struct bt_ctf_field_type_variant *variant; |
1366 | GQuark field_name_quark = g_quark_from_string(field_name); | |
1367 | ||
1368 | if (!type || !field_type || type->frozen || | |
654c1444 | 1369 | bt_ctf_validate_identifier(field_name) || |
9ce21c30 JG |
1370 | (type->declaration->id != CTF_TYPE_VARIANT) || |
1371 | bt_ctf_field_type_validate(field_type)) { | |
273b65be JG |
1372 | ret = -1; |
1373 | goto end; | |
1374 | } | |
1375 | ||
1376 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); | |
273b65be | 1377 | |
6964b7fd JG |
1378 | /* The user has explicitly provided a tag; validate against it. */ |
1379 | if (variant->tag) { | |
1380 | int name_found = 0; | |
1381 | ||
1382 | /* Make sure this name is present in the enum tag */ | |
1383 | for (i = 0; i < variant->tag->entries->len; i++) { | |
1384 | struct enumeration_mapping *mapping = | |
1385 | g_ptr_array_index(variant->tag->entries, i); | |
1386 | ||
1387 | if (mapping->string == field_name_quark) { | |
1388 | name_found = 1; | |
1389 | break; | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | if (!name_found) { | |
1394 | /* Validation failed */ | |
1395 | ret = -1; | |
1396 | goto end; | |
273b65be JG |
1397 | } |
1398 | } | |
1399 | ||
6964b7fd JG |
1400 | if (add_structure_field(variant->fields, variant->field_name_to_index, |
1401 | field_type, field_name)) { | |
273b65be JG |
1402 | ret = -1; |
1403 | goto end; | |
1404 | } | |
1405 | end: | |
1406 | return ret; | |
1407 | } | |
1408 | ||
b92ddaaa JG |
1409 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( |
1410 | struct bt_ctf_field_type *type, | |
1411 | const char *field_name) | |
1412 | { | |
1413 | size_t index; | |
1414 | GQuark name_quark; | |
1415 | struct structure_field *field; | |
1416 | struct bt_ctf_field_type_variant *variant; | |
1417 | struct bt_ctf_field_type *field_type = NULL; | |
1418 | ||
1419 | if (!type || !field_name) { | |
1420 | goto end; | |
1421 | } | |
1422 | ||
1423 | name_quark = g_quark_try_string(field_name); | |
1424 | if (!name_quark) { | |
1425 | goto end; | |
1426 | } | |
1427 | ||
1428 | variant = container_of(type, struct bt_ctf_field_type_variant, parent); | |
1429 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, | |
1430 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { | |
1431 | goto end; | |
1432 | } | |
1433 | ||
1434 | field = g_ptr_array_index(variant->fields, index); | |
1435 | field_type = field->type; | |
1436 | bt_ctf_field_type_get(field_type); | |
1437 | end: | |
1438 | return field_type; | |
1439 | } | |
1440 | ||
1441 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( | |
1442 | struct bt_ctf_field_type *type, | |
1443 | struct bt_ctf_field *tag) | |
1444 | { | |
1445 | const char *enum_value; | |
1446 | struct bt_ctf_field_type *field_type = NULL; | |
1447 | ||
1448 | if (!type || !tag || type->declaration->id != CTF_TYPE_VARIANT) { | |
1449 | goto end; | |
1450 | } | |
1451 | ||
1452 | enum_value = bt_ctf_field_enumeration_get_mapping_name(tag); | |
1453 | if (!enum_value) { | |
1454 | goto end; | |
1455 | } | |
1456 | ||
1457 | /* Already increments field_type's reference count */ | |
1458 | field_type = bt_ctf_field_type_variant_get_field_type_by_name( | |
1459 | type, enum_value); | |
1460 | end: | |
1461 | return field_type; | |
1462 | } | |
1463 | ||
074ee56d | 1464 | int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type) |
b92ddaaa | 1465 | { |
074ee56d | 1466 | int ret = 0; |
b92ddaaa JG |
1467 | struct bt_ctf_field_type_variant *variant; |
1468 | ||
1469 | if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) { | |
1470 | ret = -1; | |
1471 | goto end; | |
1472 | } | |
1473 | ||
1474 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
1475 | parent); | |
074ee56d | 1476 | ret = (int) variant->fields->len; |
b92ddaaa JG |
1477 | end: |
1478 | return ret; | |
1479 | ||
1480 | } | |
1481 | ||
1482 | int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type, | |
1483 | const char **field_name, struct bt_ctf_field_type **field_type, | |
074ee56d | 1484 | int index) |
b92ddaaa JG |
1485 | { |
1486 | struct bt_ctf_field_type_variant *variant; | |
1487 | struct structure_field *field; | |
1488 | int ret = 0; | |
1489 | ||
647f3b93 | 1490 | if (!type || index < 0 || (type->declaration->id != CTF_TYPE_VARIANT)) { |
b92ddaaa JG |
1491 | ret = -1; |
1492 | goto end; | |
1493 | } | |
1494 | ||
1495 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
1496 | parent); | |
1497 | if (index >= variant->fields->len) { | |
1498 | ret = -1; | |
1499 | goto end; | |
1500 | } | |
1501 | ||
1502 | field = g_ptr_array_index(variant->fields, index); | |
647f3b93 JG |
1503 | if (field_type) { |
1504 | *field_type = field->type; | |
1505 | bt_ctf_field_type_get(field->type); | |
1506 | } | |
1507 | if (field_name) { | |
1508 | *field_name = g_quark_to_string(field->name); | |
1509 | } | |
b92ddaaa JG |
1510 | end: |
1511 | return ret; | |
1512 | } | |
1513 | ||
273b65be JG |
1514 | struct bt_ctf_field_type *bt_ctf_field_type_array_create( |
1515 | struct bt_ctf_field_type *element_type, | |
1516 | unsigned int length) | |
1517 | { | |
1518 | struct bt_ctf_field_type_array *array = NULL; | |
1519 | ||
9ce21c30 JG |
1520 | if (!element_type || length == 0 || |
1521 | bt_ctf_field_type_validate(element_type)) { | |
273b65be JG |
1522 | goto error; |
1523 | } | |
1524 | ||
1525 | array = g_new0(struct bt_ctf_field_type_array, 1); | |
1526 | if (!array) { | |
1527 | goto error; | |
1528 | } | |
1529 | ||
1530 | array->parent.declaration = &array->declaration.p; | |
1531 | array->parent.declaration->id = CTF_TYPE_ARRAY; | |
c35a1669 | 1532 | |
273b65be JG |
1533 | bt_ctf_field_type_get(element_type); |
1534 | array->element_type = element_type; | |
1535 | array->length = length; | |
c35a1669 | 1536 | bt_ctf_field_type_init(&array->parent); |
273b65be JG |
1537 | array->parent.declaration->alignment = |
1538 | element_type->declaration->alignment; | |
1539 | return &array->parent; | |
1540 | error: | |
1541 | return NULL; | |
1542 | } | |
1543 | ||
b92ddaaa JG |
1544 | struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type( |
1545 | struct bt_ctf_field_type *type) | |
1546 | { | |
1547 | struct bt_ctf_field_type *ret = NULL; | |
1548 | struct bt_ctf_field_type_array *array; | |
1549 | ||
1550 | if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) { | |
1551 | goto end; | |
1552 | } | |
1553 | ||
1554 | array = container_of(type, struct bt_ctf_field_type_array, parent); | |
1555 | ret = array->element_type; | |
1556 | bt_ctf_field_type_get(ret); | |
1557 | end: | |
1558 | return ret; | |
1559 | } | |
1560 | ||
1561 | int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type) | |
1562 | { | |
1563 | int64_t ret; | |
1564 | struct bt_ctf_field_type_array *array; | |
1565 | ||
1566 | if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) { | |
1567 | ret = -1; | |
1568 | goto end; | |
1569 | } | |
1570 | ||
1571 | array = container_of(type, struct bt_ctf_field_type_array, parent); | |
1572 | ret = (int64_t) array->length; | |
1573 | end: | |
1574 | return ret; | |
1575 | } | |
1576 | ||
273b65be JG |
1577 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( |
1578 | struct bt_ctf_field_type *element_type, | |
1579 | const char *length_field_name) | |
1580 | { | |
1581 | struct bt_ctf_field_type_sequence *sequence = NULL; | |
1582 | ||
654c1444 | 1583 | if (!element_type || bt_ctf_validate_identifier(length_field_name) || |
9ce21c30 | 1584 | bt_ctf_field_type_validate(element_type)) { |
273b65be JG |
1585 | goto error; |
1586 | } | |
1587 | ||
1588 | sequence = g_new0(struct bt_ctf_field_type_sequence, 1); | |
1589 | if (!sequence) { | |
1590 | goto error; | |
1591 | } | |
1592 | ||
1593 | sequence->parent.declaration = &sequence->declaration.p; | |
1594 | sequence->parent.declaration->id = CTF_TYPE_SEQUENCE; | |
273b65be JG |
1595 | bt_ctf_field_type_get(element_type); |
1596 | sequence->element_type = element_type; | |
1597 | sequence->length_field_name = g_string_new(length_field_name); | |
c35a1669 | 1598 | bt_ctf_field_type_init(&sequence->parent); |
273b65be JG |
1599 | sequence->parent.declaration->alignment = |
1600 | element_type->declaration->alignment; | |
1601 | return &sequence->parent; | |
1602 | error: | |
1603 | return NULL; | |
1604 | } | |
1605 | ||
b92ddaaa JG |
1606 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type( |
1607 | struct bt_ctf_field_type *type) | |
1608 | { | |
1609 | struct bt_ctf_field_type *ret = NULL; | |
1610 | struct bt_ctf_field_type_sequence *sequence; | |
1611 | ||
1612 | if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) { | |
1613 | goto end; | |
1614 | } | |
1615 | ||
1616 | sequence = container_of(type, struct bt_ctf_field_type_sequence, | |
1617 | parent); | |
1618 | ret = sequence->element_type; | |
1619 | bt_ctf_field_type_get(ret); | |
1620 | end: | |
1621 | return ret; | |
1622 | } | |
1623 | ||
1624 | const char *bt_ctf_field_type_sequence_get_length_field_name( | |
1625 | struct bt_ctf_field_type *type) | |
1626 | { | |
1627 | const char *ret = NULL; | |
1628 | struct bt_ctf_field_type_sequence *sequence; | |
1629 | ||
1630 | if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) { | |
1631 | goto end; | |
1632 | } | |
1633 | ||
1634 | sequence = container_of(type, struct bt_ctf_field_type_sequence, | |
1635 | parent); | |
1636 | ret = sequence->length_field_name->str; | |
1637 | end: | |
1638 | return ret; | |
1639 | } | |
1640 | ||
273b65be JG |
1641 | struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) |
1642 | { | |
1643 | struct bt_ctf_field_type_string *string = | |
1644 | g_new0(struct bt_ctf_field_type_string, 1); | |
1645 | ||
1646 | if (!string) { | |
1647 | return NULL; | |
1648 | } | |
1649 | ||
1650 | string->parent.declaration = &string->declaration.p; | |
1651 | string->parent.declaration->id = CTF_TYPE_STRING; | |
1652 | bt_ctf_field_type_init(&string->parent); | |
1653 | string->declaration.encoding = CTF_STRING_UTF8; | |
1654 | string->parent.declaration->alignment = CHAR_BIT; | |
1655 | return &string->parent; | |
1656 | } | |
1657 | ||
b92ddaaa JG |
1658 | enum ctf_string_encoding bt_ctf_field_type_string_get_encoding( |
1659 | struct bt_ctf_field_type *type) | |
1660 | { | |
1661 | struct bt_ctf_field_type_string *string; | |
1662 | enum ctf_string_encoding ret = CTF_STRING_UNKNOWN; | |
1663 | ||
1664 | if (!type || (type->declaration->id != CTF_TYPE_STRING)) { | |
1665 | goto end; | |
1666 | } | |
1667 | ||
1668 | string = container_of(type, struct bt_ctf_field_type_string, | |
1669 | parent); | |
1670 | ret = string->declaration.encoding; | |
1671 | end: | |
1672 | return ret; | |
1673 | } | |
1674 | ||
1675 | int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type, | |
273b65be JG |
1676 | enum ctf_string_encoding encoding) |
1677 | { | |
1678 | int ret = 0; | |
1679 | struct bt_ctf_field_type_string *string; | |
1680 | ||
1681 | if (!type || type->declaration->id != CTF_TYPE_STRING || | |
1682 | (encoding != CTF_STRING_UTF8 && | |
1683 | encoding != CTF_STRING_ASCII)) { | |
1684 | ret = -1; | |
1685 | goto end; | |
1686 | } | |
1687 | ||
1688 | string = container_of(type, struct bt_ctf_field_type_string, parent); | |
1689 | string->declaration.encoding = encoding; | |
1690 | end: | |
1691 | return ret; | |
1692 | } | |
1693 | ||
b92ddaaa JG |
1694 | int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type) |
1695 | { | |
1696 | int ret; | |
1697 | ||
1698 | if (!type) { | |
1699 | ret = -1; | |
1700 | goto end; | |
1701 | } | |
1702 | ||
1703 | ret = (int) type->declaration->alignment; | |
1704 | end: | |
1705 | return ret; | |
1706 | } | |
1707 | ||
273b65be JG |
1708 | int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, |
1709 | unsigned int alignment) | |
1710 | { | |
1711 | int ret = 0; | |
1712 | ||
1713 | /* Alignment must be bit-aligned (1) or byte aligned */ | |
1714 | if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) { | |
1715 | ret = -1; | |
1716 | goto end; | |
1717 | } | |
1718 | ||
1719 | if (type->declaration->id == CTF_TYPE_STRING && | |
1720 | alignment != CHAR_BIT) { | |
1721 | ret = -1; | |
1722 | goto end; | |
1723 | } | |
1724 | ||
1725 | type->declaration->alignment = alignment; | |
1726 | ret = 0; | |
1727 | end: | |
1728 | return ret; | |
1729 | } | |
1730 | ||
b92ddaaa JG |
1731 | enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( |
1732 | struct bt_ctf_field_type *type) | |
1733 | { | |
1734 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
c35a1669 | 1735 | int internal_byte_order = -1; |
b92ddaaa JG |
1736 | |
1737 | if (!type) { | |
1738 | goto end; | |
1739 | } | |
1740 | ||
1741 | switch (type->declaration->id) { | |
1742 | case CTF_TYPE_INTEGER: | |
1743 | { | |
1744 | struct bt_ctf_field_type_integer *integer = container_of( | |
1745 | type, struct bt_ctf_field_type_integer, parent); | |
c35a1669 | 1746 | internal_byte_order = integer->declaration.byte_order; |
b92ddaaa JG |
1747 | break; |
1748 | } | |
1749 | case CTF_TYPE_FLOAT: | |
1750 | { | |
1751 | struct bt_ctf_field_type_floating_point *floating_point = | |
1752 | container_of(type, | |
1753 | struct bt_ctf_field_type_floating_point, | |
1754 | parent); | |
c35a1669 | 1755 | internal_byte_order = floating_point->declaration.byte_order; |
b92ddaaa JG |
1756 | break; |
1757 | } | |
1758 | default: | |
c35a1669 JG |
1759 | goto end; |
1760 | } | |
1761 | ||
1762 | switch (internal_byte_order) { | |
1763 | case LITTLE_ENDIAN: | |
1764 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; | |
b92ddaaa | 1765 | break; |
c35a1669 JG |
1766 | case BIG_ENDIAN: |
1767 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; | |
1768 | break; | |
1769 | case 0: | |
1770 | ret = BT_CTF_BYTE_ORDER_NATIVE; | |
1771 | break; | |
1772 | default: | |
1773 | ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
b92ddaaa JG |
1774 | } |
1775 | end: | |
1776 | return ret; | |
1777 | } | |
1778 | ||
273b65be JG |
1779 | int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type, |
1780 | enum bt_ctf_byte_order byte_order) | |
1781 | { | |
1782 | int ret = 0; | |
1783 | int internal_byte_order; | |
1784 | enum ctf_type_id type_id; | |
1785 | ||
1786 | if (!type || type->frozen) { | |
1787 | ret = -1; | |
1788 | goto end; | |
1789 | } | |
1790 | ||
273b65be JG |
1791 | switch (byte_order) { |
1792 | case BT_CTF_BYTE_ORDER_NATIVE: | |
c35a1669 JG |
1793 | /* Leave unset. Will be initialized by parent. */ |
1794 | internal_byte_order = 0; | |
273b65be JG |
1795 | break; |
1796 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
1797 | internal_byte_order = LITTLE_ENDIAN; | |
1798 | break; | |
1799 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
1800 | case BT_CTF_BYTE_ORDER_NETWORK: | |
1801 | internal_byte_order = BIG_ENDIAN; | |
1802 | break; | |
1803 | default: | |
1804 | ret = -1; | |
1805 | goto end; | |
1806 | } | |
1807 | ||
3fa759e9 | 1808 | type_id = type->declaration->id; |
273b65be | 1809 | if (set_byte_order_funcs[type_id]) { |
c35a1669 | 1810 | set_byte_order_funcs[type_id](type, internal_byte_order, 0); |
273b65be JG |
1811 | } |
1812 | end: | |
1813 | return ret; | |
1814 | } | |
1815 | ||
b92ddaaa JG |
1816 | enum ctf_type_id bt_ctf_field_type_get_type_id( |
1817 | struct bt_ctf_field_type *type) | |
1818 | { | |
1819 | if (!type) { | |
1820 | return CTF_TYPE_UNKNOWN; | |
1821 | } | |
1822 | ||
1823 | return type->declaration->id; | |
1824 | } | |
2f2d8e05 | 1825 | |
273b65be JG |
1826 | void bt_ctf_field_type_get(struct bt_ctf_field_type *type) |
1827 | { | |
1828 | if (!type) { | |
1829 | return; | |
1830 | } | |
1831 | ||
1832 | bt_ctf_ref_get(&type->ref_count); | |
1833 | } | |
1834 | ||
1835 | void bt_ctf_field_type_put(struct bt_ctf_field_type *type) | |
1836 | { | |
273b65be JG |
1837 | if (!type) { |
1838 | return; | |
1839 | } | |
1840 | ||
2f2d8e05 | 1841 | bt_ctf_ref_put(&type->ref_count, bt_ctf_field_type_destroy); |
273b65be JG |
1842 | } |
1843 | ||
1844 | BT_HIDDEN | |
1845 | void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type) | |
1846 | { | |
1847 | if (!type) { | |
1848 | return; | |
1849 | } | |
1850 | ||
1851 | type->freeze(type); | |
1852 | } | |
1853 | ||
1854 | BT_HIDDEN | |
b92ddaaa JG |
1855 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed( |
1856 | struct bt_ctf_field_type_variant *variant, | |
1857 | int64_t tag_value) | |
273b65be JG |
1858 | { |
1859 | struct bt_ctf_field_type *type = NULL; | |
b92ddaaa JG |
1860 | GQuark field_name_quark; |
1861 | gpointer index; | |
1862 | struct structure_field *field_entry; | |
1863 | struct range_overlap_query query = { | |
1864 | .range_start._signed = tag_value, | |
1865 | .range_end._signed = tag_value, | |
1866 | .mapping_name = 0, .overlaps = 0}; | |
273b65be | 1867 | |
b92ddaaa JG |
1868 | g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap, |
1869 | &query); | |
1870 | if (!query.overlaps) { | |
273b65be JG |
1871 | goto end; |
1872 | } | |
1873 | ||
b92ddaaa JG |
1874 | field_name_quark = query.mapping_name; |
1875 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, | |
1876 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { | |
273b65be JG |
1877 | goto end; |
1878 | } | |
1879 | ||
e54fab7e | 1880 | field_entry = g_ptr_array_index(variant->fields, (size_t) index); |
b92ddaaa | 1881 | type = field_entry->type; |
273b65be JG |
1882 | end: |
1883 | return type; | |
1884 | } | |
1885 | ||
1886 | BT_HIDDEN | |
b92ddaaa | 1887 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned( |
273b65be | 1888 | struct bt_ctf_field_type_variant *variant, |
b92ddaaa | 1889 | uint64_t tag_value) |
273b65be JG |
1890 | { |
1891 | struct bt_ctf_field_type *type = NULL; | |
1892 | GQuark field_name_quark; | |
1893 | gpointer index; | |
1894 | struct structure_field *field_entry; | |
b92ddaaa JG |
1895 | struct range_overlap_query query = { |
1896 | .range_start._unsigned = tag_value, | |
1897 | .range_end._unsigned = tag_value, | |
1898 | .mapping_name = 0, .overlaps = 0}; | |
273b65be | 1899 | |
b92ddaaa JG |
1900 | g_ptr_array_foreach(variant->tag->entries, |
1901 | check_ranges_overlap_unsigned, | |
273b65be JG |
1902 | &query); |
1903 | if (!query.overlaps) { | |
1904 | goto end; | |
1905 | } | |
1906 | ||
1907 | field_name_quark = query.mapping_name; | |
1908 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, | |
1909 | GUINT_TO_POINTER(field_name_quark), NULL, &index)) { | |
1910 | goto end; | |
1911 | } | |
1912 | ||
1913 | field_entry = g_ptr_array_index(variant->fields, (size_t)index); | |
1914 | type = field_entry->type; | |
1915 | end: | |
1916 | return type; | |
1917 | } | |
1918 | ||
1919 | BT_HIDDEN | |
1920 | int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type, | |
1921 | struct metadata_context *context) | |
1922 | { | |
1923 | int ret; | |
1924 | ||
1925 | if (!type || !context) { | |
1926 | ret = -1; | |
1927 | goto end; | |
1928 | } | |
1929 | ||
1930 | ret = type->serialize(type, context); | |
1931 | end: | |
1932 | return ret; | |
1933 | } | |
1934 | ||
c35a1669 JG |
1935 | BT_HIDDEN |
1936 | void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type, | |
1937 | int byte_order) | |
1938 | { | |
1939 | if (!type) { | |
1940 | return; | |
1941 | } | |
1942 | ||
1943 | assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN); | |
1944 | if (set_byte_order_funcs[type->declaration->id]) { | |
1945 | set_byte_order_funcs[type->declaration->id](type, | |
1946 | byte_order, 1); | |
1947 | } | |
1948 | } | |
1949 | ||
24724933 JG |
1950 | BT_HIDDEN |
1951 | struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type) | |
1952 | { | |
1953 | struct bt_ctf_field_type *copy = NULL; | |
1954 | ||
1955 | if (!type) { | |
1956 | goto end; | |
1957 | } | |
1958 | ||
1959 | copy = type_copy_funcs[type->declaration->id](type); | |
1960 | end: | |
1961 | return copy; | |
1962 | } | |
1963 | ||
6b64f185 JG |
1964 | BT_HIDDEN |
1965 | struct bt_ctf_field_path *bt_ctf_field_path_create(void) | |
1966 | { | |
1967 | struct bt_ctf_field_path *field_path = NULL; | |
1968 | ||
1969 | field_path = g_new0(struct bt_ctf_field_path, 1); | |
1970 | if (!field_path) { | |
1971 | goto end; | |
1972 | } | |
1973 | ||
1974 | field_path->root = CTF_NODE_UNKNOWN; | |
1975 | field_path->path_indexes = g_array_new(TRUE, FALSE, sizeof(int)); | |
1976 | if (!field_path->path_indexes) { | |
1977 | bt_ctf_field_path_destroy(field_path); | |
1978 | field_path = NULL; | |
1979 | } | |
1980 | end: | |
1981 | return field_path; | |
1982 | } | |
1983 | ||
1984 | ||
1985 | BT_HIDDEN | |
1986 | struct bt_ctf_field_path *bt_ctf_field_path_copy( | |
1987 | struct bt_ctf_field_path *path) | |
1988 | { | |
1989 | struct bt_ctf_field_path *new_path = bt_ctf_field_path_create(); | |
1990 | ||
1991 | if (!new_path) { | |
1992 | goto end; | |
1993 | } | |
1994 | ||
1995 | new_path->root = path->root; | |
1996 | g_array_insert_vals(new_path->path_indexes, 0, | |
1997 | path->path_indexes->data, path->path_indexes->len); | |
1998 | end: | |
1999 | return new_path; | |
2000 | } | |
2001 | ||
2002 | BT_HIDDEN | |
2003 | void bt_ctf_field_path_destroy(struct bt_ctf_field_path *path) | |
2004 | { | |
2005 | if (!path) { | |
2006 | return; | |
2007 | } | |
2008 | ||
2009 | if (path->path_indexes) { | |
2010 | g_array_free(path->path_indexes, TRUE); | |
2011 | } | |
2012 | g_free(path); | |
2013 | } | |
39a5e0db JG |
2014 | |
2015 | BT_HIDDEN | |
2016 | int bt_ctf_field_type_structure_get_field_name_index( | |
2017 | struct bt_ctf_field_type *type, const char *name) | |
2018 | { | |
2019 | int ret; | |
2020 | size_t index; | |
2021 | GQuark name_quark; | |
2022 | struct bt_ctf_field_type_structure *structure; | |
2023 | ||
2024 | if (!type || !name || | |
2025 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) { | |
2026 | ret = -1; | |
2027 | goto end; | |
2028 | } | |
2029 | ||
2030 | name_quark = g_quark_try_string(name); | |
2031 | if (!name_quark) { | |
2032 | ret = -1; | |
2033 | goto end; | |
2034 | } | |
2035 | ||
2036 | structure = container_of(type, struct bt_ctf_field_type_structure, | |
2037 | parent); | |
2038 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, | |
2039 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { | |
2040 | ret = -1; | |
2041 | goto end; | |
2042 | } | |
2043 | ret = (int) index; | |
2044 | end: | |
2045 | return ret; | |
2046 | } | |
736133f1 JG |
2047 | |
2048 | BT_HIDDEN | |
2049 | int bt_ctf_field_type_variant_get_field_name_index( | |
2050 | struct bt_ctf_field_type *type, const char *name) | |
2051 | { | |
2052 | int ret; | |
2053 | size_t index; | |
2054 | GQuark name_quark; | |
2055 | struct bt_ctf_field_type_variant *variant; | |
2056 | ||
2057 | if (!type || !name || | |
2058 | bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) { | |
2059 | ret = -1; | |
2060 | goto end; | |
2061 | } | |
2062 | ||
2063 | name_quark = g_quark_try_string(name); | |
2064 | if (!name_quark) { | |
2065 | ret = -1; | |
2066 | goto end; | |
2067 | } | |
2068 | ||
2069 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
2070 | parent); | |
2071 | if (!g_hash_table_lookup_extended(variant->field_name_to_index, | |
2072 | GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) { | |
2073 | ret = -1; | |
2074 | goto end; | |
2075 | } | |
2076 | ret = (int) index; | |
2077 | end: | |
2078 | return ret; | |
2079 | } | |
aa4e271c JG |
2080 | |
2081 | BT_HIDDEN | |
2082 | int bt_ctf_field_type_sequence_set_length_field_path( | |
2083 | struct bt_ctf_field_type *type, | |
2084 | struct bt_ctf_field_path *path) | |
2085 | { | |
2086 | int ret = 0; | |
2087 | struct bt_ctf_field_type_sequence *sequence; | |
2088 | ||
2089 | if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_SEQUENCE) { | |
2090 | ret = -1; | |
2091 | goto end; | |
2092 | } | |
2093 | ||
2094 | sequence = container_of(type, struct bt_ctf_field_type_sequence, | |
2095 | parent); | |
2096 | if (sequence->length_field_path) { | |
2097 | bt_ctf_field_path_destroy(sequence->length_field_path); | |
2098 | } | |
2099 | sequence->length_field_path = path; | |
2100 | end: | |
2101 | return ret; | |
2102 | } | |
4a1e8671 JG |
2103 | |
2104 | BT_HIDDEN | |
2105 | int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type, | |
2106 | struct bt_ctf_field_path *path) | |
2107 | { | |
2108 | int ret = 0; | |
2109 | struct bt_ctf_field_type_variant *variant; | |
2110 | ||
2111 | if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) { | |
2112 | ret = -1; | |
2113 | goto end; | |
2114 | } | |
2115 | ||
2116 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
2117 | parent); | |
2118 | if (variant->tag_path) { | |
2119 | bt_ctf_field_path_destroy(variant->tag_path); | |
2120 | } | |
2121 | variant->tag_path = path; | |
2122 | end: | |
2123 | return ret; | |
2124 | } | |
3f39933a JG |
2125 | |
2126 | BT_HIDDEN | |
2127 | int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type *type, | |
2128 | struct bt_ctf_field_type *tag) | |
2129 | { | |
2130 | int ret = 0; | |
2131 | struct bt_ctf_field_type_variant *variant; | |
2132 | ||
2133 | if (!type || !tag || type->frozen || | |
2134 | bt_ctf_field_type_get_type_id(tag) != CTF_TYPE_ENUM) { | |
2135 | ret = -1; | |
2136 | goto end; | |
2137 | } | |
2138 | ||
2139 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
2140 | parent); | |
2141 | bt_ctf_field_type_get(tag); | |
2142 | if (variant->tag) { | |
2143 | bt_ctf_field_type_put(&variant->tag->parent); | |
2144 | } | |
2145 | variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration, | |
2146 | parent); | |
2147 | end: | |
2148 | return ret; | |
2149 | } | |
2150 | ||
273b65be JG |
2151 | static |
2152 | void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref) | |
2153 | { | |
2154 | struct bt_ctf_field_type_integer *integer; | |
2155 | ||
2156 | if (!ref) { | |
2157 | return; | |
2158 | } | |
2159 | ||
2160 | integer = container_of( | |
2161 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2162 | struct bt_ctf_field_type_integer, parent); | |
eee752e5 JG |
2163 | if (integer->mapped_clock) { |
2164 | bt_ctf_clock_put(integer->mapped_clock); | |
2165 | } | |
273b65be JG |
2166 | g_free(integer); |
2167 | } | |
2168 | ||
2169 | static | |
2170 | void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref) | |
2171 | { | |
2172 | struct bt_ctf_field_type_enumeration *enumeration; | |
2173 | ||
2174 | if (!ref) { | |
2175 | return; | |
2176 | } | |
2177 | ||
2178 | enumeration = container_of( | |
2179 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2180 | struct bt_ctf_field_type_enumeration, parent); | |
2181 | g_ptr_array_free(enumeration->entries, TRUE); | |
2182 | bt_ctf_field_type_put(enumeration->container); | |
2183 | g_free(enumeration); | |
2184 | } | |
2185 | ||
2186 | static | |
2187 | void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref) | |
2188 | { | |
2189 | struct bt_ctf_field_type_floating_point *floating_point; | |
2190 | ||
2191 | if (!ref) { | |
2192 | return; | |
2193 | } | |
2194 | ||
2195 | floating_point = container_of( | |
2196 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2197 | struct bt_ctf_field_type_floating_point, parent); | |
2198 | g_free(floating_point); | |
2199 | } | |
2200 | ||
2201 | static | |
2202 | void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref) | |
2203 | { | |
2204 | struct bt_ctf_field_type_structure *structure; | |
2205 | ||
2206 | if (!ref) { | |
2207 | return; | |
2208 | } | |
2209 | ||
2210 | structure = container_of( | |
2211 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2212 | struct bt_ctf_field_type_structure, parent); | |
2213 | g_ptr_array_free(structure->fields, TRUE); | |
2214 | g_hash_table_destroy(structure->field_name_to_index); | |
2215 | g_free(structure); | |
2216 | } | |
2217 | ||
2218 | static | |
2219 | void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref) | |
2220 | { | |
2221 | struct bt_ctf_field_type_variant *variant; | |
2222 | ||
2223 | if (!ref) { | |
2224 | return; | |
2225 | } | |
2226 | ||
2227 | variant = container_of( | |
2228 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2229 | struct bt_ctf_field_type_variant, parent); | |
2230 | g_ptr_array_free(variant->fields, TRUE); | |
2231 | g_hash_table_destroy(variant->field_name_to_index); | |
2232 | g_string_free(variant->tag_name, TRUE); | |
2233 | bt_ctf_field_type_put(&variant->tag->parent); | |
4a1e8671 | 2234 | bt_ctf_field_path_destroy(variant->tag_path); |
273b65be JG |
2235 | g_free(variant); |
2236 | } | |
2237 | ||
2238 | static | |
2239 | void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref) | |
2240 | { | |
2241 | struct bt_ctf_field_type_array *array; | |
2242 | ||
2243 | if (!ref) { | |
2244 | return; | |
2245 | } | |
2246 | ||
2247 | array = container_of( | |
2248 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2249 | struct bt_ctf_field_type_array, parent); | |
2250 | bt_ctf_field_type_put(array->element_type); | |
2251 | g_free(array); | |
2252 | } | |
2253 | ||
2254 | static | |
2255 | void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref) | |
2256 | { | |
2257 | struct bt_ctf_field_type_sequence *sequence; | |
2258 | ||
2259 | if (!ref) { | |
2260 | return; | |
2261 | } | |
2262 | ||
2263 | sequence = container_of( | |
2264 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2265 | struct bt_ctf_field_type_sequence, parent); | |
2266 | bt_ctf_field_type_put(sequence->element_type); | |
2267 | g_string_free(sequence->length_field_name, TRUE); | |
aa4e271c | 2268 | bt_ctf_field_path_destroy(sequence->length_field_path); |
273b65be JG |
2269 | g_free(sequence); |
2270 | } | |
2271 | ||
2272 | static | |
2273 | void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref) | |
2274 | { | |
2275 | struct bt_ctf_field_type_string *string; | |
2276 | ||
2277 | if (!ref) { | |
2278 | return; | |
2279 | } | |
2280 | ||
2281 | string = container_of( | |
2282 | container_of(ref, struct bt_ctf_field_type, ref_count), | |
2283 | struct bt_ctf_field_type_string, parent); | |
2284 | g_free(string); | |
2285 | } | |
2286 | ||
2287 | static | |
2288 | void generic_field_type_freeze(struct bt_ctf_field_type *type) | |
2289 | { | |
2290 | type->frozen = 1; | |
2291 | } | |
2292 | ||
2293 | static | |
2294 | void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type) | |
2295 | { | |
2296 | struct bt_ctf_field_type_enumeration *enumeration_type = container_of( | |
2297 | type, struct bt_ctf_field_type_enumeration, parent); | |
2298 | ||
2299 | generic_field_type_freeze(type); | |
2300 | bt_ctf_field_type_freeze(enumeration_type->container); | |
2301 | } | |
2302 | ||
2303 | static | |
2304 | void freeze_structure_field(struct structure_field *field) | |
2305 | { | |
2306 | bt_ctf_field_type_freeze(field->type); | |
2307 | } | |
2308 | ||
2309 | static | |
2310 | void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type) | |
2311 | { | |
2312 | struct bt_ctf_field_type_structure *structure_type = container_of( | |
2313 | type, struct bt_ctf_field_type_structure, parent); | |
2314 | ||
2315 | generic_field_type_freeze(type); | |
2316 | g_ptr_array_foreach(structure_type->fields, (GFunc)freeze_structure_field, | |
2317 | NULL); | |
2318 | } | |
2319 | ||
2320 | static | |
2321 | void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type) | |
2322 | { | |
2323 | struct bt_ctf_field_type_variant *variant_type = container_of( | |
2324 | type, struct bt_ctf_field_type_variant, parent); | |
2325 | ||
2326 | generic_field_type_freeze(type); | |
2327 | g_ptr_array_foreach(variant_type->fields, (GFunc)freeze_structure_field, | |
2328 | NULL); | |
2329 | } | |
2330 | ||
2331 | static | |
2332 | void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type) | |
2333 | { | |
2334 | struct bt_ctf_field_type_array *array_type = container_of( | |
2335 | type, struct bt_ctf_field_type_array, parent); | |
2336 | ||
2337 | generic_field_type_freeze(type); | |
2338 | bt_ctf_field_type_freeze(array_type->element_type); | |
2339 | } | |
2340 | ||
2341 | static | |
2342 | void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type) | |
2343 | { | |
2344 | struct bt_ctf_field_type_sequence *sequence_type = container_of( | |
2345 | type, struct bt_ctf_field_type_sequence, parent); | |
2346 | ||
2347 | generic_field_type_freeze(type); | |
2348 | bt_ctf_field_type_freeze(sequence_type->element_type); | |
2349 | } | |
2350 | ||
2351 | static | |
2352 | const char *get_encoding_string(enum ctf_string_encoding encoding) | |
2353 | { | |
2354 | const char *encoding_string; | |
2355 | ||
2356 | switch (encoding) { | |
2357 | case CTF_STRING_NONE: | |
2358 | encoding_string = "none"; | |
2359 | break; | |
2360 | case CTF_STRING_ASCII: | |
2361 | encoding_string = "ASCII"; | |
2362 | break; | |
2363 | case CTF_STRING_UTF8: | |
2364 | encoding_string = "UTF8"; | |
2365 | break; | |
2366 | default: | |
2367 | encoding_string = "unknown"; | |
2368 | break; | |
2369 | } | |
2370 | ||
2371 | return encoding_string; | |
2372 | } | |
2373 | ||
2374 | static | |
2375 | const char *get_integer_base_string(enum bt_ctf_integer_base base) | |
2376 | { | |
2377 | const char *base_string; | |
2378 | ||
2379 | switch (base) { | |
2380 | case BT_CTF_INTEGER_BASE_DECIMAL: | |
2381 | base_string = "decimal"; | |
2382 | break; | |
2383 | case BT_CTF_INTEGER_BASE_HEXADECIMAL: | |
2384 | base_string = "hexadecimal"; | |
2385 | break; | |
2386 | case BT_CTF_INTEGER_BASE_OCTAL: | |
2387 | base_string = "octal"; | |
2388 | break; | |
2389 | case BT_CTF_INTEGER_BASE_BINARY: | |
2390 | base_string = "binary"; | |
2391 | break; | |
2392 | default: | |
2393 | base_string = "unknown"; | |
2394 | break; | |
2395 | } | |
2396 | ||
2397 | return base_string; | |
2398 | } | |
2399 | ||
2400 | static | |
2401 | int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type, | |
2402 | struct metadata_context *context) | |
2403 | { | |
2404 | struct bt_ctf_field_type_integer *integer = container_of(type, | |
2405 | struct bt_ctf_field_type_integer, parent); | |
6cfb906f | 2406 | int ret = 0; |
273b65be JG |
2407 | |
2408 | g_string_append_printf(context->string, | |
6cfb906f | 2409 | "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s", |
273b65be JG |
2410 | integer->declaration.len, type->declaration->alignment, |
2411 | (integer->declaration.signedness ? "true" : "false"), | |
2412 | get_encoding_string(integer->declaration.encoding), | |
2413 | get_integer_base_string(integer->declaration.base), | |
2414 | get_byte_order_string(integer->declaration.byte_order)); | |
6cfb906f JG |
2415 | if (integer->mapped_clock) { |
2416 | const char *clock_name = bt_ctf_clock_get_name( | |
2417 | integer->mapped_clock); | |
2418 | ||
2419 | if (!clock_name) { | |
2420 | ret = -1; | |
2421 | goto end; | |
2422 | } | |
2423 | ||
2424 | g_string_append_printf(context->string, | |
4ebdec03 | 2425 | "; map = clock.%s.value", clock_name); |
6cfb906f JG |
2426 | } |
2427 | ||
2428 | g_string_append(context->string, "; }"); | |
2429 | end: | |
2430 | return ret; | |
273b65be JG |
2431 | } |
2432 | ||
2433 | static | |
2434 | int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type, | |
2435 | struct metadata_context *context) | |
2436 | { | |
2437 | size_t entry; | |
9ce21c30 | 2438 | int ret; |
273b65be JG |
2439 | struct bt_ctf_field_type_enumeration *enumeration = container_of(type, |
2440 | struct bt_ctf_field_type_enumeration, parent); | |
b92ddaaa JG |
2441 | struct bt_ctf_field_type *container_type; |
2442 | int container_signed; | |
273b65be | 2443 | |
9ce21c30 JG |
2444 | ret = bt_ctf_field_type_validate(type); |
2445 | if (ret) { | |
2446 | goto end; | |
2447 | } | |
2448 | ||
b92ddaaa JG |
2449 | container_type = bt_ctf_field_type_enumeration_get_container_type(type); |
2450 | if (!container_type) { | |
2451 | ret = -1; | |
2452 | goto end; | |
2453 | } | |
2454 | ||
2455 | container_signed = bt_ctf_field_type_integer_get_signed(container_type); | |
2456 | if (container_signed < 0) { | |
2457 | ret = container_signed; | |
2458 | goto error_put_container_type; | |
2459 | } | |
2460 | ||
273b65be JG |
2461 | g_string_append(context->string, "enum : "); |
2462 | ret = bt_ctf_field_type_serialize(enumeration->container, context); | |
2463 | if (ret) { | |
b92ddaaa | 2464 | goto error_put_container_type; |
273b65be JG |
2465 | } |
2466 | ||
2467 | g_string_append(context->string, " { "); | |
2468 | for (entry = 0; entry < enumeration->entries->len; entry++) { | |
2469 | struct enumeration_mapping *mapping = | |
2470 | enumeration->entries->pdata[entry]; | |
2471 | ||
b92ddaaa JG |
2472 | if (container_signed) { |
2473 | if (mapping->range_start._signed == | |
2474 | mapping->range_end._signed) { | |
2475 | g_string_append_printf(context->string, | |
2476 | "\"%s\" = %" PRId64, | |
2477 | g_quark_to_string(mapping->string), | |
2478 | mapping->range_start._signed); | |
2479 | } else { | |
2480 | g_string_append_printf(context->string, | |
2481 | "\"%s\" = %" PRId64 " ... %" PRId64, | |
2482 | g_quark_to_string(mapping->string), | |
2483 | mapping->range_start._signed, | |
2484 | mapping->range_end._signed); | |
2485 | } | |
273b65be | 2486 | } else { |
b92ddaaa JG |
2487 | if (mapping->range_start._unsigned == |
2488 | mapping->range_end._unsigned) { | |
2489 | g_string_append_printf(context->string, | |
2490 | "\"%s\" = %" PRIu64, | |
2491 | g_quark_to_string(mapping->string), | |
2492 | mapping->range_start._unsigned); | |
2493 | } else { | |
2494 | g_string_append_printf(context->string, | |
2495 | "\"%s\" = %" PRIu64 " ... %" PRIu64, | |
2496 | g_quark_to_string(mapping->string), | |
2497 | mapping->range_start._unsigned, | |
2498 | mapping->range_end._unsigned); | |
2499 | } | |
273b65be JG |
2500 | } |
2501 | ||
2502 | g_string_append(context->string, | |
2503 | ((entry != (enumeration->entries->len - 1)) ? | |
2504 | ", " : " }")); | |
2505 | } | |
2506 | ||
2507 | if (context->field_name->len) { | |
2508 | g_string_append_printf(context->string, " %s", | |
2509 | context->field_name->str); | |
2510 | g_string_assign(context->field_name, ""); | |
2511 | } | |
b92ddaaa JG |
2512 | error_put_container_type: |
2513 | bt_ctf_field_type_put(container_type); | |
273b65be JG |
2514 | end: |
2515 | return ret; | |
2516 | } | |
2517 | ||
2518 | static | |
2519 | int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type, | |
2520 | struct metadata_context *context) | |
2521 | { | |
2522 | struct bt_ctf_field_type_floating_point *floating_point = container_of( | |
2523 | type, struct bt_ctf_field_type_floating_point, parent); | |
2524 | ||
2525 | g_string_append_printf(context->string, | |
2526 | "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }", | |
2527 | floating_point->declaration.exp->len, | |
2528 | floating_point->declaration.mantissa->len + 1, | |
2529 | get_byte_order_string(floating_point->declaration.byte_order), | |
2530 | type->declaration->alignment); | |
2531 | return 0; | |
2532 | } | |
2533 | ||
2534 | static | |
2535 | int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type, | |
2536 | struct metadata_context *context) | |
2537 | { | |
2538 | size_t i; | |
2539 | unsigned int indent; | |
2540 | int ret = 0; | |
2541 | struct bt_ctf_field_type_structure *structure = container_of(type, | |
2542 | struct bt_ctf_field_type_structure, parent); | |
2543 | GString *structure_field_name = context->field_name; | |
2544 | ||
2545 | context->field_name = g_string_new(""); | |
2546 | ||
2547 | context->current_indentation_level++; | |
2548 | g_string_append(context->string, "struct {\n"); | |
2549 | ||
2550 | for (i = 0; i < structure->fields->len; i++) { | |
2551 | struct structure_field *field; | |
2552 | ||
2553 | for (indent = 0; indent < context->current_indentation_level; | |
2554 | indent++) { | |
2555 | g_string_append_c(context->string, '\t'); | |
2556 | } | |
2557 | ||
2558 | field = structure->fields->pdata[i]; | |
2559 | g_string_assign(context->field_name, | |
2560 | g_quark_to_string(field->name)); | |
2561 | ret = bt_ctf_field_type_serialize(field->type, context); | |
2562 | if (ret) { | |
2563 | goto end; | |
2564 | } | |
2565 | ||
2566 | if (context->field_name->len) { | |
2567 | g_string_append_printf(context->string, " %s", | |
2568 | context->field_name->str); | |
2569 | } | |
2570 | g_string_append(context->string, ";\n"); | |
2571 | } | |
2572 | ||
2573 | context->current_indentation_level--; | |
2574 | for (indent = 0; indent < context->current_indentation_level; | |
2575 | indent++) { | |
2576 | g_string_append_c(context->string, '\t'); | |
2577 | } | |
2578 | ||
2579 | g_string_append_printf(context->string, "} align(%zu)", | |
2580 | type->declaration->alignment); | |
2581 | end: | |
2582 | g_string_free(context->field_name, TRUE); | |
2583 | context->field_name = structure_field_name; | |
2584 | return ret; | |
2585 | } | |
2586 | ||
2587 | static | |
2588 | int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type, | |
2589 | struct metadata_context *context) | |
2590 | { | |
2591 | size_t i; | |
2592 | unsigned int indent; | |
2593 | int ret = 0; | |
2594 | struct bt_ctf_field_type_variant *variant = container_of( | |
2595 | type, struct bt_ctf_field_type_variant, parent); | |
2596 | GString *variant_field_name = context->field_name; | |
2597 | ||
2598 | context->field_name = g_string_new(""); | |
6964b7fd JG |
2599 | if (variant->tag_name->len > 0) { |
2600 | g_string_append_printf(context->string, | |
2601 | "variant <%s> {\n", variant->tag_name->str); | |
2602 | } else { | |
2603 | g_string_append(context->string, "variant {\n"); | |
2604 | } | |
2605 | ||
273b65be JG |
2606 | context->current_indentation_level++; |
2607 | for (i = 0; i < variant->fields->len; i++) { | |
2608 | struct structure_field *field = variant->fields->pdata[i]; | |
2609 | ||
2610 | g_string_assign(context->field_name, | |
2611 | g_quark_to_string(field->name)); | |
2612 | for (indent = 0; indent < context->current_indentation_level; | |
2613 | indent++) { | |
2614 | g_string_append_c(context->string, '\t'); | |
2615 | } | |
2616 | ||
2617 | g_string_assign(context->field_name, | |
2618 | g_quark_to_string(field->name)); | |
2619 | ret = bt_ctf_field_type_serialize(field->type, context); | |
2620 | if (ret) { | |
2621 | goto end; | |
2622 | } | |
2623 | ||
2624 | if (context->field_name->len) { | |
2625 | g_string_append_printf(context->string, " %s;", | |
2626 | context->field_name->str); | |
2627 | } | |
2628 | ||
2629 | g_string_append_c(context->string, '\n'); | |
2630 | } | |
2631 | ||
2632 | context->current_indentation_level--; | |
2633 | for (indent = 0; indent < context->current_indentation_level; | |
2634 | indent++) { | |
2635 | g_string_append_c(context->string, '\t'); | |
2636 | } | |
2637 | ||
2638 | g_string_append(context->string, "}"); | |
2639 | end: | |
2640 | g_string_free(context->field_name, TRUE); | |
2641 | context->field_name = variant_field_name; | |
2642 | return ret; | |
2643 | } | |
2644 | ||
2645 | static | |
2646 | int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type, | |
2647 | struct metadata_context *context) | |
2648 | { | |
2649 | int ret = 0; | |
2650 | struct bt_ctf_field_type_array *array = container_of(type, | |
2651 | struct bt_ctf_field_type_array, parent); | |
2652 | ||
2653 | ret = bt_ctf_field_type_serialize(array->element_type, context); | |
2654 | if (ret) { | |
2655 | goto end; | |
2656 | } | |
2657 | ||
2658 | if (context->field_name->len) { | |
2659 | g_string_append_printf(context->string, " %s[%u]", | |
2660 | context->field_name->str, array->length); | |
2661 | g_string_assign(context->field_name, ""); | |
2662 | } else { | |
2663 | g_string_append_printf(context->string, "[%u]", array->length); | |
2664 | } | |
2665 | end: | |
2666 | return ret; | |
2667 | } | |
2668 | ||
2669 | static | |
2670 | int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type, | |
2671 | struct metadata_context *context) | |
2672 | { | |
2673 | int ret = 0; | |
2674 | struct bt_ctf_field_type_sequence *sequence = container_of( | |
2675 | type, struct bt_ctf_field_type_sequence, parent); | |
2676 | ||
2677 | ret = bt_ctf_field_type_serialize(sequence->element_type, context); | |
2678 | if (ret) { | |
2679 | goto end; | |
2680 | } | |
2681 | ||
2682 | if (context->field_name->len) { | |
2683 | g_string_append_printf(context->string, " %s[%s]", | |
2684 | context->field_name->str, | |
2685 | sequence->length_field_name->str); | |
2686 | g_string_assign(context->field_name, ""); | |
2687 | } else { | |
2688 | g_string_append_printf(context->string, "[%s]", | |
2689 | sequence->length_field_name->str); | |
2690 | } | |
2691 | end: | |
2692 | return ret; | |
2693 | } | |
2694 | ||
2695 | static | |
2696 | int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type, | |
2697 | struct metadata_context *context) | |
2698 | { | |
2699 | struct bt_ctf_field_type_string *string = container_of( | |
2700 | type, struct bt_ctf_field_type_string, parent); | |
2701 | ||
2702 | g_string_append_printf(context->string, | |
2703 | "string { encoding = %s; }", | |
2704 | get_encoding_string(string->declaration.encoding)); | |
2705 | return 0; | |
2706 | } | |
2707 | ||
2708 | static | |
2709 | void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type, | |
c35a1669 | 2710 | int byte_order, int set_native) |
273b65be JG |
2711 | { |
2712 | struct bt_ctf_field_type_integer *integer_type = container_of(type, | |
2713 | struct bt_ctf_field_type_integer, parent); | |
2714 | ||
c35a1669 JG |
2715 | if (set_native) { |
2716 | integer_type->declaration.byte_order = | |
2717 | integer_type->declaration.byte_order == 0 ? | |
2718 | byte_order : integer_type->declaration.byte_order; | |
2719 | } else { | |
2720 | integer_type->declaration.byte_order = byte_order; | |
2721 | } | |
2722 | } | |
2723 | ||
2724 | static | |
2725 | void bt_ctf_field_type_enumeration_set_byte_order( | |
2726 | struct bt_ctf_field_type *type, int byte_order, int set_native) | |
2727 | { | |
2728 | struct bt_ctf_field_type_enumeration *enum_type = container_of(type, | |
2729 | struct bt_ctf_field_type_enumeration, parent); | |
2730 | ||
2731 | /* Safe to assume that container is an integer */ | |
2732 | bt_ctf_field_type_integer_set_byte_order(enum_type->container, | |
2733 | byte_order, set_native); | |
273b65be JG |
2734 | } |
2735 | ||
2736 | static | |
2737 | void bt_ctf_field_type_floating_point_set_byte_order( | |
c35a1669 | 2738 | struct bt_ctf_field_type *type, int byte_order, int set_native) |
273b65be JG |
2739 | { |
2740 | struct bt_ctf_field_type_floating_point *floating_point_type = | |
2741 | container_of(type, struct bt_ctf_field_type_floating_point, | |
2742 | parent); | |
2743 | ||
c35a1669 JG |
2744 | if (set_native) { |
2745 | floating_point_type->declaration.byte_order = | |
2746 | floating_point_type->declaration.byte_order == 0 ? | |
2747 | byte_order : | |
2748 | floating_point_type->declaration.byte_order; | |
2749 | floating_point_type->sign.byte_order = | |
2750 | floating_point_type->sign.byte_order == 0 ? | |
2751 | byte_order : floating_point_type->sign.byte_order; | |
2752 | floating_point_type->mantissa.byte_order = | |
2753 | floating_point_type->mantissa.byte_order == 0 ? | |
2754 | byte_order : floating_point_type->mantissa.byte_order; | |
2755 | floating_point_type->exp.byte_order = | |
2756 | floating_point_type->exp.byte_order == 0 ? | |
2757 | byte_order : floating_point_type->exp.byte_order; | |
2758 | } else { | |
2759 | floating_point_type->declaration.byte_order = byte_order; | |
2760 | floating_point_type->sign.byte_order = byte_order; | |
2761 | floating_point_type->mantissa.byte_order = byte_order; | |
2762 | floating_point_type->exp.byte_order = byte_order; | |
2763 | } | |
2764 | } | |
2765 | ||
2766 | static | |
2767 | void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type, | |
2768 | int byte_order, int set_native) | |
2769 | { | |
2770 | int i; | |
2771 | struct bt_ctf_field_type_structure *structure_type = | |
2772 | container_of(type, struct bt_ctf_field_type_structure, | |
2773 | parent); | |
2774 | ||
2775 | for (i = 0; i < structure_type->fields->len; i++) { | |
2776 | struct structure_field *field = g_ptr_array_index( | |
2777 | structure_type->fields, i); | |
2778 | struct bt_ctf_field_type *field_type = field->type; | |
2779 | ||
2780 | if (set_byte_order_funcs[field_type->declaration->id]) { | |
2781 | set_byte_order_funcs[field_type->declaration->id]( | |
2782 | field_type, byte_order, set_native); | |
2783 | } | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | static | |
2788 | void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type, | |
2789 | int byte_order, int set_native) | |
2790 | { | |
2791 | int i; | |
2792 | struct bt_ctf_field_type_variant *variant_type = | |
2793 | container_of(type, struct bt_ctf_field_type_variant, | |
2794 | parent); | |
2795 | ||
2796 | for (i = 0; i < variant_type->fields->len; i++) { | |
2797 | struct structure_field *field = g_ptr_array_index( | |
2798 | variant_type->fields, i); | |
2799 | struct bt_ctf_field_type *field_type = field->type; | |
2800 | ||
2801 | if (set_byte_order_funcs[field_type->declaration->id]) { | |
2802 | set_byte_order_funcs[field_type->declaration->id]( | |
2803 | field_type, byte_order, set_native); | |
2804 | } | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | static | |
2809 | void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type, | |
2810 | int byte_order, int set_native) | |
2811 | { | |
2812 | struct bt_ctf_field_type_array *array_type = | |
2813 | container_of(type, struct bt_ctf_field_type_array, | |
2814 | parent); | |
2815 | ||
2816 | if (set_byte_order_funcs[array_type->element_type->declaration->id]) { | |
2817 | set_byte_order_funcs[array_type->element_type->declaration->id]( | |
2818 | array_type->element_type, byte_order, set_native); | |
2819 | } | |
2820 | } | |
2821 | ||
2822 | static | |
2823 | void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type, | |
2824 | int byte_order, int set_native) | |
2825 | { | |
2826 | struct bt_ctf_field_type_sequence *sequence_type = | |
2827 | container_of(type, struct bt_ctf_field_type_sequence, | |
2828 | parent); | |
2829 | ||
2830 | if (set_byte_order_funcs[ | |
2831 | sequence_type->element_type->declaration->id]) { | |
2832 | set_byte_order_funcs[ | |
2833 | sequence_type->element_type->declaration->id]( | |
2834 | sequence_type->element_type, byte_order, set_native); | |
2835 | } | |
273b65be | 2836 | } |
24724933 JG |
2837 | |
2838 | static | |
2839 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( | |
2840 | struct bt_ctf_field_type *type) | |
2841 | { | |
2842 | struct bt_ctf_field_type *copy; | |
2843 | struct bt_ctf_field_type_integer *integer, *copy_integer; | |
2844 | ||
2845 | integer = container_of(type, struct bt_ctf_field_type_integer, parent); | |
2846 | copy = bt_ctf_field_type_integer_create(integer->declaration.len); | |
2847 | if (!copy) { | |
2848 | goto end; | |
2849 | } | |
2850 | ||
2851 | copy_integer = container_of(copy, struct bt_ctf_field_type_integer, | |
2852 | parent); | |
2853 | copy_integer->declaration = integer->declaration; | |
2854 | if (integer->mapped_clock) { | |
2855 | bt_ctf_clock_get(integer->mapped_clock); | |
2856 | copy_integer->mapped_clock = integer->mapped_clock; | |
2857 | } | |
2858 | end: | |
2859 | return copy; | |
2860 | } | |
2861 | ||
2862 | static | |
2863 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy( | |
2864 | struct bt_ctf_field_type *type) | |
2865 | { | |
2866 | size_t i; | |
2867 | struct bt_ctf_field_type *copy = NULL, *copy_container; | |
2868 | struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration; | |
2869 | ||
2870 | enumeration = container_of(type, struct bt_ctf_field_type_enumeration, | |
2871 | parent); | |
2872 | ||
2873 | /* Copy the source enumeration's container */ | |
2874 | copy_container = bt_ctf_field_type_copy(enumeration->container); | |
2875 | if (!copy_container) { | |
2876 | goto end; | |
2877 | } | |
2878 | ||
2879 | copy = bt_ctf_field_type_enumeration_create(copy_container); | |
2880 | if (!copy) { | |
2881 | goto end; | |
2882 | } | |
2883 | copy_enumeration = container_of(copy, | |
2884 | struct bt_ctf_field_type_enumeration, parent); | |
2885 | ||
2886 | /* Copy all enumaration entries */ | |
2887 | for (i = 0; i < enumeration->entries->len; i++) { | |
2888 | struct enumeration_mapping *mapping = g_ptr_array_index( | |
2889 | enumeration->entries, i); | |
2890 | struct enumeration_mapping* copy_mapping = g_new0( | |
2891 | struct enumeration_mapping, 1); | |
2892 | ||
2893 | if (!copy_mapping) { | |
2894 | goto error; | |
2895 | } | |
2896 | ||
2897 | *copy_mapping = *mapping; | |
2898 | g_ptr_array_add(copy_enumeration->entries, copy_mapping); | |
2899 | } | |
2900 | ||
2901 | copy_enumeration->declaration = enumeration->declaration; | |
2902 | end: | |
2903 | if (copy_container) { | |
2904 | bt_ctf_field_type_put(copy_container); | |
2905 | } | |
2906 | return copy; | |
2907 | error: | |
2908 | if (copy_container) { | |
2909 | bt_ctf_field_type_put(copy_container); | |
2910 | } | |
2911 | bt_ctf_field_type_put(copy); | |
2912 | return NULL; | |
2913 | } | |
2914 | ||
2915 | static | |
2916 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( | |
2917 | struct bt_ctf_field_type *type) | |
2918 | { | |
2919 | struct bt_ctf_field_type *copy; | |
2920 | struct bt_ctf_field_type_floating_point *floating_point, *copy_float; | |
2921 | ||
2922 | floating_point = container_of(type, | |
2923 | struct bt_ctf_field_type_floating_point, parent); | |
2924 | copy = bt_ctf_field_type_floating_point_create(); | |
2925 | if (!copy) { | |
2926 | goto end; | |
2927 | } | |
2928 | ||
2929 | copy_float = container_of(copy, | |
2930 | struct bt_ctf_field_type_floating_point, parent); | |
2931 | copy_float->declaration = floating_point->declaration; | |
2932 | copy_float->sign = floating_point->sign; | |
2933 | copy_float->mantissa = floating_point->mantissa; | |
2934 | copy_float->exp = floating_point->exp; | |
2935 | end: | |
2936 | return copy; | |
2937 | } | |
2938 | ||
2939 | static | |
2940 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy( | |
2941 | struct bt_ctf_field_type *type) | |
2942 | { | |
2943 | int i; | |
2944 | GHashTableIter iter; | |
2945 | gpointer key, value; | |
2946 | struct bt_ctf_field_type *copy; | |
2947 | struct bt_ctf_field_type_structure *structure, *copy_structure; | |
2948 | ||
2949 | structure = container_of(type, struct bt_ctf_field_type_structure, | |
2950 | parent); | |
2951 | copy = bt_ctf_field_type_structure_create(); | |
2952 | if (!copy) { | |
2953 | goto end; | |
2954 | } | |
2955 | ||
2956 | copy_structure = container_of(copy, | |
2957 | struct bt_ctf_field_type_structure, parent); | |
2958 | ||
2959 | /* Copy field_name_to_index */ | |
2960 | g_hash_table_iter_init(&iter, structure->field_name_to_index); | |
2961 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
2962 | g_hash_table_insert(copy_structure->field_name_to_index, | |
2963 | key, value); | |
2964 | } | |
2965 | ||
2966 | for (i = 0; i < structure->fields->len; i++) { | |
2967 | struct structure_field *entry, *copy_entry; | |
2968 | struct bt_ctf_field_type *copy_field; | |
2969 | ||
2970 | copy_entry = g_new0(struct structure_field, 1); | |
2971 | if (!copy_entry) { | |
2972 | goto error; | |
2973 | } | |
2974 | ||
2975 | entry = g_ptr_array_index(structure->fields, i); | |
2976 | copy_field = bt_ctf_field_type_copy(entry->type); | |
2977 | if (!copy_field) { | |
2978 | g_free(copy_entry); | |
2979 | goto error; | |
2980 | } | |
2981 | ||
2982 | copy_entry->name = entry->name; | |
2983 | copy_entry->type = copy_field; | |
2984 | g_ptr_array_add(copy_structure->fields, copy_entry); | |
2985 | } | |
2986 | ||
2987 | copy_structure->declaration = structure->declaration; | |
2988 | end: | |
2989 | return copy; | |
2990 | error: | |
2991 | bt_ctf_field_type_put(copy); | |
2992 | return NULL; | |
2993 | } | |
2994 | ||
2995 | static | |
2996 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy( | |
2997 | struct bt_ctf_field_type *type) | |
2998 | { | |
2999 | int i; | |
3000 | GHashTableIter iter; | |
3001 | gpointer key, value; | |
3002 | struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL; | |
3003 | struct bt_ctf_field_type_variant *variant, *copy_variant; | |
3004 | ||
3005 | variant = container_of(type, struct bt_ctf_field_type_variant, | |
3006 | parent); | |
3007 | if (variant->tag) { | |
3008 | copy_tag = bt_ctf_field_type_copy(&variant->tag->parent); | |
3009 | if (!copy_tag) { | |
3010 | goto end; | |
3011 | } | |
3012 | } | |
3013 | ||
3014 | copy = bt_ctf_field_type_variant_create(copy_tag, | |
3015 | variant->tag_name->len ? variant->tag_name->str : NULL); | |
3016 | if (!copy) { | |
3017 | goto end; | |
3018 | } | |
3019 | ||
3020 | copy_variant = container_of(copy, struct bt_ctf_field_type_variant, | |
3021 | parent); | |
3022 | ||
3023 | /* Copy field_name_to_index */ | |
3024 | g_hash_table_iter_init(&iter, variant->field_name_to_index); | |
3025 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
3026 | g_hash_table_insert(copy_variant->field_name_to_index, | |
3027 | key, value); | |
3028 | } | |
3029 | ||
3030 | for (i = 0; i < variant->fields->len; i++) { | |
3031 | struct structure_field *entry, *copy_entry; | |
3032 | struct bt_ctf_field_type *copy_field; | |
3033 | ||
3034 | copy_entry = g_new0(struct structure_field, 1); | |
3035 | if (!copy_entry) { | |
3036 | goto error; | |
3037 | } | |
3038 | ||
3039 | entry = g_ptr_array_index(variant->fields, i); | |
3040 | copy_field = bt_ctf_field_type_copy(entry->type); | |
3041 | if (!copy_field) { | |
3042 | g_free(copy_entry); | |
3043 | goto error; | |
3044 | } | |
3045 | ||
3046 | copy_entry->name = entry->name; | |
3047 | copy_entry->type = copy_field; | |
3048 | g_ptr_array_add(copy_variant->fields, copy_entry); | |
3049 | } | |
3050 | ||
3051 | copy_variant->declaration = variant->declaration; | |
4a1e8671 JG |
3052 | if (variant->tag_path) { |
3053 | copy_variant->tag_path = bt_ctf_field_path_copy( | |
3054 | variant->tag_path); | |
3055 | if (!copy_variant->tag_path) { | |
3056 | goto error; | |
3057 | } | |
3058 | } | |
24724933 JG |
3059 | end: |
3060 | if (copy_tag) { | |
3061 | bt_ctf_field_type_put(copy_tag); | |
3062 | } | |
3063 | ||
3064 | return copy; | |
3065 | error: | |
3066 | if (copy_tag) { | |
3067 | bt_ctf_field_type_put(copy_tag); | |
3068 | } | |
3069 | ||
3070 | bt_ctf_field_type_put(copy); | |
3071 | return NULL; | |
3072 | } | |
3073 | ||
3074 | static | |
3075 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy( | |
3076 | struct bt_ctf_field_type *type) | |
3077 | { | |
3078 | struct bt_ctf_field_type *copy = NULL, *copy_element; | |
3079 | struct bt_ctf_field_type_array *array, *copy_array; | |
3080 | ||
3081 | array = container_of(type, struct bt_ctf_field_type_array, | |
3082 | parent); | |
3083 | copy_element = bt_ctf_field_type_copy(array->element_type); | |
3084 | if (!copy_element) { | |
3085 | goto end; | |
3086 | } | |
3087 | ||
3088 | copy = bt_ctf_field_type_array_create(copy_element, array->length); | |
3089 | if (!copy) { | |
3090 | goto end; | |
3091 | } | |
3092 | ||
3093 | copy_array = container_of(copy, struct bt_ctf_field_type_array, | |
3094 | parent); | |
3095 | copy_array->declaration = array->declaration; | |
3096 | end: | |
3097 | if (copy_element) { | |
3098 | bt_ctf_field_type_put(copy_element); | |
3099 | } | |
3100 | ||
3101 | return copy; | |
3102 | } | |
3103 | ||
3104 | static | |
3105 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy( | |
3106 | struct bt_ctf_field_type *type) | |
3107 | { | |
3108 | struct bt_ctf_field_type *copy = NULL, *copy_element; | |
3109 | struct bt_ctf_field_type_sequence *sequence, *copy_sequence; | |
3110 | ||
3111 | sequence = container_of(type, struct bt_ctf_field_type_sequence, | |
3112 | parent); | |
3113 | copy_element = bt_ctf_field_type_copy(sequence->element_type); | |
3114 | if (!copy_element) { | |
3115 | goto end; | |
3116 | } | |
3117 | ||
3118 | copy = bt_ctf_field_type_sequence_create(copy_element, | |
3119 | sequence->length_field_name->len ? | |
3120 | sequence->length_field_name->str : NULL); | |
3121 | if (!copy) { | |
3122 | goto end; | |
3123 | } | |
3124 | ||
3125 | copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence, | |
3126 | parent); | |
3127 | copy_sequence->declaration = sequence->declaration; | |
aa4e271c JG |
3128 | if (sequence->length_field_path) { |
3129 | copy_sequence->length_field_path = bt_ctf_field_path_copy( | |
3130 | sequence->length_field_path); | |
3131 | if (!copy_sequence->length_field_path) { | |
3132 | goto error; | |
3133 | } | |
3134 | } | |
24724933 JG |
3135 | end: |
3136 | if (copy_element) { | |
3137 | bt_ctf_field_type_put(copy_element); | |
3138 | } | |
3139 | ||
3140 | return copy; | |
aa4e271c JG |
3141 | error: |
3142 | if (copy) { | |
3143 | bt_ctf_field_type_put(copy); | |
3144 | copy = NULL; | |
3145 | } | |
3146 | goto end; | |
24724933 JG |
3147 | } |
3148 | ||
3149 | static | |
3150 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( | |
3151 | struct bt_ctf_field_type *type) | |
3152 | { | |
3153 | struct bt_ctf_field_type *copy; | |
3154 | struct bt_ctf_field_type_string *string, *copy_string; | |
3155 | ||
3156 | copy = bt_ctf_field_type_string_create(); | |
3157 | if (!copy) { | |
3158 | goto end; | |
3159 | } | |
3160 | ||
3161 | string = container_of(type, struct bt_ctf_field_type_string, | |
3162 | parent); | |
3163 | copy_string = container_of(type, struct bt_ctf_field_type_string, | |
3164 | parent); | |
3165 | copy_string->declaration = string->declaration; | |
3166 | end: | |
3167 | return copy; | |
3168 | } |