Commit | Line | Data |
---|---|---|
8deee039 PP |
1 | /* |
2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #define BT_LOG_TAG "CTF-WRITER-FIELD-TYPES" | |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
28 | #include <babeltrace/assert-pre-internal.h> | |
29 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
30 | #include <babeltrace/ctf-ir/utils-internal.h> | |
31 | #include <babeltrace/ctf-ir/utils.h> | |
32 | #include <babeltrace/ctf-ir/field-path-internal.h> | |
33 | #include <babeltrace/ctf-writer/field-types.h> | |
34 | #include <babeltrace/ctf-writer/field-types-internal.h> | |
35 | #include <babeltrace/ctf-writer/fields.h> | |
36 | #include <babeltrace/ctf-writer/clock-internal.h> | |
37 | #include <babeltrace/object-internal.h> | |
38 | #include <babeltrace/ref.h> | |
39 | #include <babeltrace/compiler-internal.h> | |
40 | #include <babeltrace/endian-internal.h> | |
41 | #include <babeltrace/assert-internal.h> | |
42 | #include <float.h> | |
43 | #include <inttypes.h> | |
44 | #include <stdlib.h> | |
45 | ||
46 | static | |
47 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( | |
48 | struct bt_ctf_field_type *ft); | |
49 | ||
50 | static | |
51 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy_recursive( | |
52 | struct bt_ctf_field_type *ft); | |
53 | ||
54 | static | |
55 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( | |
56 | struct bt_ctf_field_type *ft); | |
57 | ||
58 | static | |
59 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy_recursive( | |
60 | struct bt_ctf_field_type *ft); | |
61 | ||
62 | static | |
63 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy_recursive( | |
64 | struct bt_ctf_field_type *ft); | |
65 | ||
66 | static | |
67 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy_recursive( | |
68 | struct bt_ctf_field_type *ft); | |
69 | ||
70 | static | |
71 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy_recursive( | |
72 | struct bt_ctf_field_type *type); | |
73 | ||
74 | static | |
75 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy( | |
76 | struct bt_ctf_field_type *type); | |
77 | ||
78 | static struct bt_field_type_common_methods bt_ctf_field_type_integer_methods = { | |
79 | .freeze = bt_field_type_common_generic_freeze, | |
80 | .validate = bt_field_type_common_integer_validate, | |
81 | .set_byte_order = bt_field_type_common_integer_set_byte_order, | |
82 | .copy = (bt_field_type_common_method_copy) | |
83 | bt_ctf_field_type_integer_copy, | |
84 | .compare = bt_field_type_common_integer_compare, | |
85 | }; | |
86 | ||
87 | static struct bt_field_type_common_methods bt_ctf_field_type_floating_point_methods = { | |
88 | .freeze = bt_field_type_common_generic_freeze, | |
89 | .validate = NULL, | |
90 | .set_byte_order = bt_field_type_common_floating_point_set_byte_order, | |
91 | .copy = (bt_field_type_common_method_copy) | |
92 | bt_ctf_field_type_floating_point_copy, | |
93 | .compare = bt_field_type_common_floating_point_compare, | |
94 | }; | |
95 | ||
96 | static struct bt_field_type_common_methods bt_ctf_field_type_enumeration_methods = { | |
97 | .freeze = bt_field_type_common_enumeration_freeze_recursive, | |
98 | .validate = bt_field_type_common_enumeration_validate_recursive, | |
99 | .set_byte_order = bt_field_type_common_enumeration_set_byte_order_recursive, | |
100 | .copy = (bt_field_type_common_method_copy) | |
101 | bt_ctf_field_type_enumeration_copy_recursive, | |
102 | .compare = bt_field_type_common_enumeration_compare_recursive, | |
103 | }; | |
104 | ||
105 | static struct bt_field_type_common_methods bt_ctf_field_type_string_methods = { | |
106 | .freeze = bt_field_type_common_generic_freeze, | |
107 | .validate = NULL, | |
108 | .set_byte_order = NULL, | |
109 | .copy = (bt_field_type_common_method_copy) | |
110 | bt_ctf_field_type_string_copy, | |
111 | .compare = bt_field_type_common_string_compare, | |
112 | }; | |
113 | ||
114 | static struct bt_field_type_common_methods bt_ctf_field_type_array_methods = { | |
115 | .freeze = bt_field_type_common_array_freeze_recursive, | |
116 | .validate = bt_field_type_common_array_validate_recursive, | |
117 | .set_byte_order = bt_field_type_common_array_set_byte_order_recursive, | |
118 | .copy = (bt_field_type_common_method_copy) | |
119 | bt_ctf_field_type_array_copy_recursive, | |
120 | .compare = bt_field_type_common_array_compare_recursive, | |
121 | }; | |
122 | ||
123 | static struct bt_field_type_common_methods bt_ctf_field_type_sequence_methods = { | |
124 | .freeze = bt_field_type_common_sequence_freeze_recursive, | |
125 | .validate = bt_field_type_common_sequence_validate_recursive, | |
126 | .set_byte_order = bt_field_type_common_sequence_set_byte_order_recursive, | |
127 | .copy = (bt_field_type_common_method_copy) | |
128 | bt_ctf_field_type_sequence_copy_recursive, | |
129 | .compare = bt_field_type_common_sequence_compare_recursive, | |
130 | }; | |
131 | ||
132 | static struct bt_field_type_common_methods bt_ctf_field_type_structure_methods = { | |
133 | .freeze = bt_field_type_common_structure_freeze_recursive, | |
134 | .validate = bt_field_type_common_structure_validate_recursive, | |
135 | .set_byte_order = bt_field_type_common_structure_set_byte_order_recursive, | |
136 | .copy = (bt_field_type_common_method_copy) | |
137 | bt_ctf_field_type_structure_copy_recursive, | |
138 | .compare = bt_field_type_common_structure_compare_recursive, | |
139 | }; | |
140 | ||
141 | static struct bt_field_type_common_methods bt_ctf_field_type_variant_methods = { | |
142 | .freeze = bt_field_type_common_variant_freeze_recursive, | |
143 | .validate = bt_field_type_common_variant_validate_recursive, | |
144 | .set_byte_order = bt_field_type_common_variant_set_byte_order_recursive, | |
145 | .copy = (bt_field_type_common_method_copy) | |
146 | bt_ctf_field_type_variant_copy_recursive, | |
147 | .compare = bt_field_type_common_variant_compare_recursive, | |
148 | }; | |
149 | ||
150 | typedef int (*bt_ctf_field_type_serialize_func)(struct bt_field_type_common *, | |
151 | struct metadata_context *); | |
152 | ||
153 | BT_HIDDEN | |
154 | int bt_ctf_field_type_serialize_recursive(struct bt_ctf_field_type *type, | |
155 | struct metadata_context *context) | |
156 | { | |
157 | int ret; | |
158 | struct bt_field_type_common *type_common = (void *) type; | |
159 | bt_ctf_field_type_serialize_func serialize_func; | |
160 | ||
161 | BT_ASSERT(type); | |
162 | BT_ASSERT(context); | |
163 | ||
164 | /* Make sure field type is valid before serializing it */ | |
165 | ret = bt_field_type_common_validate((void *) type); | |
166 | if (ret) { | |
167 | BT_LOGW("Cannot serialize field type's metadata: field type is invalid: " | |
168 | "addr=%p", type); | |
169 | goto end; | |
170 | } | |
171 | ||
172 | serialize_func = type_common->spec.writer.serialize_func; | |
173 | ret = serialize_func((void *) type, context); | |
174 | ||
175 | end: | |
176 | return ret; | |
177 | } | |
178 | ||
179 | static | |
180 | const char *get_encoding_string(enum bt_string_encoding encoding) | |
181 | { | |
182 | const char *encoding_string; | |
183 | ||
184 | switch (encoding) { | |
185 | case BT_STRING_ENCODING_NONE: | |
186 | encoding_string = "none"; | |
187 | break; | |
188 | case BT_STRING_ENCODING_ASCII: | |
189 | encoding_string = "ASCII"; | |
190 | break; | |
191 | case BT_STRING_ENCODING_UTF8: | |
192 | encoding_string = "UTF8"; | |
193 | break; | |
194 | default: | |
195 | encoding_string = "unknown"; | |
196 | break; | |
197 | } | |
198 | ||
199 | return encoding_string; | |
200 | } | |
201 | ||
202 | static | |
203 | const char *get_integer_base_string(enum bt_integer_base base) | |
204 | { | |
205 | const char *base_string; | |
206 | ||
207 | switch (base) { | |
208 | case BT_INTEGER_BASE_DECIMAL: | |
209 | case BT_INTEGER_BASE_UNSPECIFIED: | |
210 | base_string = "decimal"; | |
211 | break; | |
212 | case BT_INTEGER_BASE_HEXADECIMAL: | |
213 | base_string = "hexadecimal"; | |
214 | break; | |
215 | case BT_INTEGER_BASE_OCTAL: | |
216 | base_string = "octal"; | |
217 | break; | |
218 | case BT_INTEGER_BASE_BINARY: | |
219 | base_string = "binary"; | |
220 | break; | |
221 | default: | |
222 | base_string = "unknown"; | |
223 | break; | |
224 | } | |
225 | ||
226 | return base_string; | |
227 | } | |
228 | ||
229 | static | |
230 | void append_field_name(struct metadata_context *context, | |
231 | const char *name) | |
232 | { | |
233 | g_string_append_c(context->string, ' '); | |
234 | ||
235 | if (!bt_identifier_is_valid(name) || *name == '_') { | |
236 | g_string_append_c(context->string, '_'); | |
237 | } | |
238 | ||
239 | g_string_append(context->string, name); | |
240 | } | |
241 | ||
242 | static | |
243 | int bt_ctf_field_type_integer_serialize(struct bt_field_type_common *type, | |
244 | struct metadata_context *context) | |
245 | { | |
246 | struct bt_field_type_common_integer *integer = BT_FROM_COMMON(type); | |
247 | int ret = 0; | |
248 | ||
249 | BT_LOGD("Serializing CTF writer integer field type's metadata: " | |
250 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
251 | g_string_append_printf(context->string, | |
252 | "integer { size = %u; align = %u; signed = %s; encoding = %s; base = %s; byte_order = %s", | |
253 | integer->size, type->alignment, | |
254 | (integer->is_signed ? "true" : "false"), | |
255 | get_encoding_string(integer->encoding), | |
256 | get_integer_base_string(integer->base), | |
257 | get_byte_order_string(integer->user_byte_order)); | |
258 | if (integer->mapped_clock_class) { | |
259 | const char *clock_name = bt_clock_class_get_name( | |
260 | integer->mapped_clock_class); | |
261 | ||
262 | BT_ASSERT(clock_name); | |
263 | g_string_append_printf(context->string, | |
264 | "; map = clock.%s.value", clock_name); | |
265 | } | |
266 | ||
267 | g_string_append(context->string, "; }"); | |
268 | return ret; | |
269 | } | |
270 | ||
271 | static | |
272 | int bt_ctf_field_type_enumeration_serialize_recursive( | |
273 | struct bt_field_type_common *type, | |
274 | struct metadata_context *context) | |
275 | { | |
276 | size_t entry; | |
277 | int ret; | |
278 | struct bt_field_type_common_enumeration *enumeration = | |
279 | BT_FROM_COMMON(type); | |
280 | struct bt_field_type_common *container_type; | |
281 | int container_signed; | |
282 | ||
283 | BT_LOGD("Serializing CTF writer enumeration field type's metadata: " | |
284 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
285 | container_type = | |
5fe68922 | 286 | bt_field_type_common_enumeration_borrow_container_field_type(type); |
8deee039 PP |
287 | BT_ASSERT(container_type); |
288 | container_signed = bt_field_type_common_integer_is_signed( | |
289 | container_type); | |
290 | BT_ASSERT(container_signed >= 0); | |
291 | g_string_append(context->string, "enum : "); | |
292 | BT_LOGD_STR("Serializing CTF writer enumeration field type's container field type's metadata."); | |
293 | ret = bt_ctf_field_type_serialize_recursive( | |
294 | (void *) enumeration->container_ft, context); | |
295 | if (ret) { | |
296 | BT_LOGW("Cannot serialize CTF writer enumeration field type's container field type's metadata: " | |
297 | "container-ft-addr=%p", enumeration->container_ft); | |
298 | goto end; | |
299 | } | |
300 | ||
301 | g_string_append(context->string, " { "); | |
302 | for (entry = 0; entry < enumeration->entries->len; entry++) { | |
303 | struct enumeration_mapping *mapping = | |
304 | enumeration->entries->pdata[entry]; | |
305 | const char *label = g_quark_to_string(mapping->string); | |
306 | ||
307 | g_string_append(context->string, "\""); | |
308 | ||
309 | if (!bt_identifier_is_valid(label) || label[0] == '_') { | |
310 | g_string_append(context->string, "_"); | |
311 | } | |
312 | ||
313 | g_string_append_printf(context->string, "%s\" = ", label); | |
314 | ||
315 | if (container_signed) { | |
316 | if (mapping->range_start._signed == | |
317 | mapping->range_end._signed) { | |
318 | g_string_append_printf(context->string, | |
319 | "%" PRId64, | |
320 | mapping->range_start._signed); | |
321 | } else { | |
322 | g_string_append_printf(context->string, | |
323 | "%" PRId64 " ... %" PRId64, | |
324 | mapping->range_start._signed, | |
325 | mapping->range_end._signed); | |
326 | } | |
327 | } else { | |
328 | if (mapping->range_start._unsigned == | |
329 | mapping->range_end._unsigned) { | |
330 | g_string_append_printf(context->string, | |
331 | "%" PRIu64, | |
332 | mapping->range_start._unsigned); | |
333 | } else { | |
334 | g_string_append_printf(context->string, | |
335 | "%" PRIu64 " ... %" PRIu64, | |
336 | mapping->range_start._unsigned, | |
337 | mapping->range_end._unsigned); | |
338 | } | |
339 | } | |
340 | ||
341 | g_string_append(context->string, | |
342 | ((entry != (enumeration->entries->len - 1)) ? | |
343 | ", " : " }")); | |
344 | } | |
345 | ||
346 | if (context->field_name->len) { | |
347 | append_field_name(context, | |
348 | context->field_name->str); | |
349 | g_string_assign(context->field_name, ""); | |
350 | } | |
351 | ||
352 | end: | |
8deee039 PP |
353 | return ret; |
354 | } | |
355 | ||
356 | static | |
357 | int bt_ctf_field_type_floating_point_serialize(struct bt_field_type_common *type, | |
358 | struct metadata_context *context) | |
359 | { | |
360 | struct bt_field_type_common_floating_point *floating_point = | |
361 | BT_FROM_COMMON(type); | |
362 | ||
363 | BT_LOGD("Serializing CTF writer floating point number field type's metadata: " | |
364 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
365 | g_string_append_printf(context->string, | |
366 | "floating_point { exp_dig = %u; mant_dig = %u; byte_order = %s; align = %u; }", | |
367 | floating_point->exp_dig, | |
368 | floating_point->mant_dig, | |
369 | get_byte_order_string(floating_point->user_byte_order), | |
370 | type->alignment); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | static | |
375 | int bt_ctf_field_type_structure_serialize_recursive( | |
376 | struct bt_field_type_common *type, | |
377 | struct metadata_context *context) | |
378 | { | |
379 | size_t i; | |
380 | unsigned int indent; | |
381 | int ret = 0; | |
382 | struct bt_field_type_common_structure *structure = BT_FROM_COMMON(type); | |
383 | GString *structure_field_name = context->field_name; | |
384 | ||
385 | BT_LOGD("Serializing CTF writer structure field type's metadata: " | |
386 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
387 | context->field_name = g_string_new(""); | |
388 | ||
389 | context->current_indentation_level++; | |
390 | g_string_append(context->string, "struct {\n"); | |
391 | ||
392 | for (i = 0; i < structure->fields->len; i++) { | |
393 | struct structure_field_common *field = structure->fields->pdata[i]; | |
394 | ||
395 | BT_LOGD("Serializing CTF writer structure field type's field metadata: " | |
396 | "index=%zu, " | |
397 | "field-ft-addr=%p, field-name=\"%s\"", | |
398 | i, field, g_quark_to_string(field->name)); | |
399 | ||
400 | for (indent = 0; indent < context->current_indentation_level; | |
401 | indent++) { | |
402 | g_string_append_c(context->string, '\t'); | |
403 | } | |
404 | ||
405 | g_string_assign(context->field_name, | |
406 | g_quark_to_string(field->name)); | |
407 | ret = bt_ctf_field_type_serialize_recursive( | |
408 | (void *) field->type, context); | |
409 | if (ret) { | |
410 | BT_LOGW("Cannot serialize CTF writer structure field type's field's metadata: " | |
411 | "index=%zu, " | |
412 | "field-ft-addr=%p, field-name=\"%s\"", | |
413 | i, field->type, | |
414 | g_quark_to_string(field->name)); | |
415 | goto end; | |
416 | } | |
417 | ||
418 | if (context->field_name->len) { | |
419 | append_field_name(context, | |
420 | context->field_name->str); | |
421 | } | |
422 | g_string_append(context->string, ";\n"); | |
423 | } | |
424 | ||
425 | context->current_indentation_level--; | |
426 | for (indent = 0; indent < context->current_indentation_level; | |
427 | indent++) { | |
428 | g_string_append_c(context->string, '\t'); | |
429 | } | |
430 | ||
431 | g_string_append_printf(context->string, "} align(%u)", | |
432 | type->alignment); | |
433 | ||
434 | end: | |
435 | g_string_free(context->field_name, TRUE); | |
436 | context->field_name = structure_field_name; | |
437 | return ret; | |
438 | } | |
439 | ||
440 | static | |
441 | int bt_ctf_field_type_variant_serialize_recursive( | |
442 | struct bt_field_type_common *type, | |
443 | struct metadata_context *context) | |
444 | { | |
445 | size_t i; | |
446 | unsigned int indent; | |
447 | int ret = 0; | |
448 | struct bt_field_type_common_variant *variant = BT_FROM_COMMON(type); | |
449 | GString *variant_field_name = context->field_name; | |
450 | ||
451 | BT_LOGD("Serializing CTF writer variant field type's metadata: " | |
452 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
453 | context->field_name = g_string_new(""); | |
454 | if (variant->tag_name->len > 0) { | |
455 | g_string_append(context->string, "variant <"); | |
456 | append_field_name(context, variant->tag_name->str); | |
457 | g_string_append(context->string, "> {\n"); | |
458 | } else { | |
459 | g_string_append(context->string, "variant {\n"); | |
460 | } | |
461 | ||
462 | context->current_indentation_level++; | |
463 | for (i = 0; i < variant->fields->len; i++) { | |
464 | struct structure_field_common *field = | |
465 | variant->fields->pdata[i]; | |
466 | ||
467 | BT_LOGD("Serializing CTF writer variant field type's field metadata: " | |
468 | "index=%zu, " | |
469 | "field-ft-addr=%p, field-name=\"%s\"", | |
470 | i, field, g_quark_to_string(field->name)); | |
471 | ||
472 | g_string_assign(context->field_name, | |
473 | g_quark_to_string(field->name)); | |
474 | for (indent = 0; indent < context->current_indentation_level; | |
475 | indent++) { | |
476 | g_string_append_c(context->string, '\t'); | |
477 | } | |
478 | ||
479 | g_string_assign(context->field_name, | |
480 | g_quark_to_string(field->name)); | |
481 | ret = bt_ctf_field_type_serialize_recursive( | |
482 | (void *) field->type, context); | |
483 | if (ret) { | |
484 | BT_LOGW("Cannot serialize CTF writer variant field type's field's metadata: " | |
485 | "index=%zu, " | |
486 | "field-ft-addr=%p, field-name=\"%s\"", | |
487 | i, field->type, | |
488 | g_quark_to_string(field->name)); | |
489 | goto end; | |
490 | } | |
491 | ||
492 | if (context->field_name->len) { | |
493 | append_field_name(context, | |
494 | context->field_name->str); | |
495 | g_string_append_c(context->string, ';'); | |
496 | } | |
497 | ||
498 | g_string_append_c(context->string, '\n'); | |
499 | } | |
500 | ||
501 | context->current_indentation_level--; | |
502 | for (indent = 0; indent < context->current_indentation_level; | |
503 | indent++) { | |
504 | g_string_append_c(context->string, '\t'); | |
505 | } | |
506 | ||
507 | g_string_append(context->string, "}"); | |
508 | ||
509 | end: | |
510 | g_string_free(context->field_name, TRUE); | |
511 | context->field_name = variant_field_name; | |
512 | return ret; | |
513 | } | |
514 | ||
515 | static | |
516 | int bt_ctf_field_type_array_serialize_recursive( | |
517 | struct bt_field_type_common *type, | |
518 | struct metadata_context *context) | |
519 | { | |
520 | int ret = 0; | |
521 | struct bt_field_type_common_array *array = BT_FROM_COMMON(type); | |
522 | ||
523 | BT_LOGD("Serializing CTF writer array field type's metadata: " | |
524 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
525 | BT_LOGD_STR("Serializing CTF writer array field type's element field type's metadata."); | |
526 | ret = bt_ctf_field_type_serialize_recursive( | |
527 | (void *) array->element_ft, context); | |
528 | if (ret) { | |
529 | BT_LOGW("Cannot serialize CTF writer array field type's element field type's metadata: " | |
530 | "element-ft-addr=%p", array->element_ft); | |
531 | goto end; | |
532 | } | |
533 | ||
534 | if (context->field_name->len) { | |
535 | append_field_name(context, | |
536 | context->field_name->str); | |
537 | ||
538 | g_string_append_printf(context->string, "[%u]", array->length); | |
539 | g_string_assign(context->field_name, ""); | |
540 | } else { | |
541 | g_string_append_printf(context->string, "[%u]", array->length); | |
542 | } | |
543 | ||
544 | end: | |
545 | return ret; | |
546 | } | |
547 | ||
548 | static | |
549 | int bt_ctf_field_type_sequence_serialize_recursive( | |
550 | struct bt_field_type_common *type, | |
551 | struct metadata_context *context) | |
552 | { | |
553 | int ret = 0; | |
554 | struct bt_field_type_common_sequence *sequence = BT_FROM_COMMON(type); | |
555 | ||
556 | BT_LOGD("Serializing CTF writer sequence field type's metadata: " | |
557 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
558 | BT_LOGD_STR("Serializing CTF writer sequence field type's element field type's metadata."); | |
559 | ret = bt_ctf_field_type_serialize_recursive( | |
560 | (void *) sequence->element_ft, context); | |
561 | if (ret) { | |
562 | BT_LOGW("Cannot serialize CTF writer sequence field type's element field type's metadata: " | |
563 | "element-ft-addr=%p", sequence->element_ft); | |
564 | goto end; | |
565 | } | |
566 | ||
567 | if (context->field_name->len) { | |
568 | append_field_name(context, context->field_name->str); | |
569 | g_string_assign(context->field_name, ""); | |
570 | } | |
571 | g_string_append(context->string, "["); | |
572 | append_field_name(context, sequence->length_field_name->str); | |
573 | g_string_append(context->string, "]"); | |
574 | ||
575 | end: | |
576 | return ret; | |
577 | } | |
578 | ||
579 | static | |
580 | int bt_ctf_field_type_string_serialize(struct bt_field_type_common *type, | |
581 | struct metadata_context *context) | |
582 | { | |
583 | struct bt_field_type_common_string *string = BT_FROM_COMMON(type); | |
584 | ||
585 | BT_LOGD("Serializing CTF writer string field type's metadata: " | |
586 | "ft-addr=%p, metadata-context-addr=%p", type, context); | |
587 | g_string_append_printf(context->string, | |
588 | "string { encoding = %s; }", | |
589 | get_encoding_string(string->encoding)); | |
590 | return 0; | |
591 | } | |
592 | ||
593 | struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size) | |
594 | { | |
595 | struct bt_field_type_common_integer *integer = NULL; | |
596 | ||
597 | BT_LOGD("Creating CTF writer integer field type object: size=%u", size); | |
598 | ||
599 | if (size == 0 || size > 64) { | |
600 | BT_LOGW("Invalid parameter: size must be between 1 and 64: " | |
601 | "size=%u", size); | |
602 | goto error; | |
603 | } | |
604 | ||
605 | integer = g_new0(struct bt_field_type_common_integer, 1); | |
606 | if (!integer) { | |
607 | BT_LOGE_STR("Failed to allocate one integer field type."); | |
608 | goto error; | |
609 | } | |
610 | ||
611 | bt_field_type_common_integer_initialize(BT_TO_COMMON(integer), | |
612 | size, bt_field_type_common_integer_destroy, | |
613 | &bt_ctf_field_type_integer_methods); | |
614 | integer->common.spec.writer.serialize_func = | |
615 | bt_ctf_field_type_integer_serialize; | |
616 | BT_LOGD("Created CTF writer integer field type object: addr=%p, size=%u", | |
617 | integer, size); | |
618 | goto end; | |
619 | ||
620 | error: | |
621 | BT_PUT(integer); | |
622 | ||
623 | end: | |
624 | return (void *) integer; | |
625 | } | |
626 | ||
627 | int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *ft) | |
628 | { | |
629 | return bt_field_type_common_integer_get_size((void *) ft); | |
630 | } | |
631 | ||
632 | bt_bool bt_ctf_field_type_integer_is_signed(struct bt_ctf_field_type *ft) | |
633 | { | |
634 | return bt_field_type_common_integer_is_signed((void *) ft); | |
635 | } | |
636 | ||
637 | int bt_ctf_field_type_integer_set_is_signed(struct bt_ctf_field_type *ft, | |
638 | bt_bool is_signed) | |
639 | { | |
640 | return bt_field_type_common_integer_set_is_signed((void *) ft, | |
641 | is_signed); | |
642 | } | |
643 | ||
644 | int bt_ctf_field_type_integer_set_size(struct bt_ctf_field_type *ft, | |
645 | unsigned int size) | |
646 | { | |
647 | return bt_field_type_common_integer_set_size((void *) ft, size); | |
648 | } | |
649 | ||
650 | enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base( | |
651 | struct bt_ctf_field_type *ft) | |
652 | { | |
653 | return (int) bt_field_type_common_integer_get_base((void *) ft); | |
654 | } | |
655 | ||
656 | int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *ft, | |
657 | enum bt_ctf_integer_base base) | |
658 | { | |
659 | return bt_field_type_common_integer_set_base((void *) ft, | |
660 | (int) base); | |
661 | } | |
662 | ||
663 | enum bt_ctf_string_encoding bt_ctf_field_type_integer_get_encoding( | |
664 | struct bt_ctf_field_type *ft) | |
665 | { | |
666 | return (int) bt_field_type_common_integer_get_encoding((void *) ft); | |
667 | } | |
668 | ||
669 | int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *ft, | |
670 | enum bt_ctf_string_encoding encoding) | |
671 | { | |
672 | return bt_field_type_common_integer_set_encoding((void *) ft, | |
673 | (int) encoding); | |
674 | } | |
675 | ||
676 | struct bt_ctf_clock_class *bt_ctf_field_type_integer_get_mapped_clock_class( | |
677 | struct bt_ctf_field_type *ft) | |
678 | { | |
5fe68922 PP |
679 | return bt_get(bt_field_type_common_integer_borrow_mapped_clock_class( |
680 | (void *) ft)); | |
8deee039 PP |
681 | } |
682 | ||
683 | int bt_ctf_field_type_integer_set_mapped_clock_class( | |
684 | struct bt_ctf_field_type *ft, | |
685 | struct bt_ctf_clock_class *clock_class) | |
686 | { | |
687 | return bt_field_type_common_integer_set_mapped_clock_class((void *) ft, | |
688 | BT_TO_COMMON(clock_class)); | |
689 | } | |
690 | ||
691 | int bt_ctf_field_type_enumeration_signed_get_mapping_by_index( | |
692 | struct bt_ctf_field_type *ft, uint64_t index, | |
693 | const char **mapping_name, int64_t *range_begin, | |
694 | int64_t *range_end) | |
695 | { | |
696 | return bt_field_type_common_enumeration_signed_get_mapping_by_index( | |
697 | (void *) ft, index, mapping_name, range_begin, range_end); | |
698 | } | |
699 | ||
700 | int bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index( | |
701 | struct bt_ctf_field_type *ft, uint64_t index, | |
702 | const char **mapping_name, uint64_t *range_begin, | |
703 | uint64_t *range_end) | |
704 | { | |
705 | return bt_field_type_common_enumeration_unsigned_get_mapping_by_index( | |
706 | (void *) ft, index, mapping_name, range_begin, range_end); | |
707 | } | |
708 | ||
709 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create( | |
710 | struct bt_ctf_field_type *container_ft) | |
711 | { | |
712 | struct bt_field_type_common_enumeration *enumeration = NULL; | |
713 | struct bt_field_type_common *int_ft = (void *) container_ft; | |
714 | ||
715 | BT_LOGD("Creating CTF writer enumeration field type object: int-ft-addr=%p", | |
716 | container_ft); | |
717 | ||
718 | if (!container_ft) { | |
719 | BT_LOGW_STR("Invalid parameter: field type is NULL."); | |
720 | goto error; | |
721 | } | |
722 | ||
723 | if (int_ft->id != BT_FIELD_TYPE_ID_INTEGER) { | |
724 | BT_LOGW("Invalid parameter: container field type is not an integer field type: " | |
725 | "container-ft-addr=%p, container-ft-id=%s", | |
726 | container_ft, bt_common_field_type_id_string(int_ft->id)); | |
727 | goto error; | |
728 | } | |
729 | ||
730 | enumeration = g_new0(struct bt_field_type_common_enumeration, 1); | |
731 | if (!enumeration) { | |
732 | BT_LOGE_STR("Failed to allocate one enumeration field type."); | |
733 | goto error; | |
734 | } | |
735 | ||
736 | bt_field_type_common_enumeration_initialize(BT_TO_COMMON(enumeration), | |
737 | int_ft, bt_field_type_common_enumeration_destroy_recursive, | |
738 | &bt_ctf_field_type_enumeration_methods); | |
739 | enumeration->common.spec.writer.serialize_func = | |
740 | bt_ctf_field_type_enumeration_serialize_recursive; | |
741 | BT_LOGD("Created CTF writer enumeration field type object: addr=%p, " | |
742 | "int-ft-addr=%p, int-ft-size=%u", | |
743 | enumeration, container_ft, | |
744 | bt_ctf_field_type_integer_get_size(container_ft)); | |
745 | goto end; | |
746 | ||
747 | error: | |
748 | BT_PUT(enumeration); | |
749 | ||
750 | end: | |
751 | return (void *) enumeration; | |
752 | } | |
753 | ||
754 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_field_type( | |
755 | struct bt_ctf_field_type *ft) | |
756 | { | |
5fe68922 PP |
757 | return bt_get( |
758 | bt_field_type_common_enumeration_borrow_container_field_type( | |
759 | (void *) ft)); | |
8deee039 PP |
760 | } |
761 | ||
762 | int bt_ctf_field_type_enumeration_signed_add_mapping( | |
763 | struct bt_ctf_field_type *ft, const char *string, | |
764 | int64_t range_start, int64_t range_end) | |
765 | { | |
766 | return bt_field_type_common_enumeration_signed_add_mapping( | |
767 | (void *) ft, string, range_start, range_end); | |
768 | } | |
769 | ||
770 | int bt_ctf_field_type_enumeration_unsigned_add_mapping( | |
771 | struct bt_ctf_field_type *ft, const char *string, | |
772 | uint64_t range_start, uint64_t range_end) | |
773 | { | |
774 | return bt_field_type_common_enumeration_unsigned_add_mapping( | |
775 | (void *) ft, string, range_start, range_end); | |
776 | } | |
777 | ||
778 | int64_t bt_ctf_field_type_enumeration_get_mapping_count( | |
779 | struct bt_ctf_field_type *ft) | |
780 | { | |
781 | return bt_field_type_common_enumeration_get_mapping_count((void *) ft); | |
782 | } | |
783 | ||
784 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void) | |
785 | { | |
786 | struct bt_field_type_common_floating_point *floating_point = | |
787 | g_new0(struct bt_field_type_common_floating_point, 1); | |
788 | ||
789 | BT_LOGD_STR("Creating CTF writer floating point number field type object."); | |
790 | ||
791 | if (!floating_point) { | |
792 | BT_LOGE_STR("Failed to allocate one floating point number field type."); | |
793 | goto end; | |
794 | } | |
795 | ||
796 | bt_field_type_common_floating_point_initialize( | |
797 | BT_TO_COMMON(floating_point), | |
798 | bt_field_type_common_floating_point_destroy, | |
799 | &bt_ctf_field_type_floating_point_methods); | |
800 | floating_point->common.spec.writer.serialize_func = | |
801 | bt_ctf_field_type_floating_point_serialize; | |
802 | BT_LOGD("Created CTF writer floating point number field type object: addr=%p, " | |
803 | "exp-size=%u, mant-size=%u", floating_point, | |
804 | floating_point->exp_dig, floating_point->mant_dig); | |
805 | ||
806 | end: | |
807 | return (void *) floating_point; | |
808 | } | |
809 | ||
810 | int bt_ctf_field_type_floating_point_get_exponent_digits( | |
811 | struct bt_ctf_field_type *ft) | |
812 | { | |
813 | return bt_field_type_common_floating_point_get_exponent_digits( | |
814 | (void *) ft); | |
815 | } | |
816 | ||
817 | int bt_ctf_field_type_floating_point_set_exponent_digits( | |
818 | struct bt_ctf_field_type *ft, unsigned int exponent_digits) | |
819 | { | |
820 | return bt_field_type_common_floating_point_set_exponent_digits( | |
821 | (void *) ft, exponent_digits); | |
822 | } | |
823 | ||
824 | int bt_ctf_field_type_floating_point_get_mantissa_digits( | |
825 | struct bt_ctf_field_type *ft) | |
826 | { | |
827 | return bt_field_type_common_floating_point_get_mantissa_digits( | |
828 | (void *) ft); | |
829 | } | |
830 | ||
831 | int bt_ctf_field_type_floating_point_set_mantissa_digits( | |
832 | struct bt_ctf_field_type *ft, unsigned int mantissa_digits) | |
833 | { | |
834 | return bt_field_type_common_floating_point_set_mantissa_digits( | |
835 | (void *) ft, mantissa_digits); | |
836 | } | |
837 | ||
838 | struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void) | |
839 | { | |
840 | struct bt_field_type_common_structure *structure = | |
841 | g_new0(struct bt_field_type_common_structure, 1); | |
842 | ||
843 | BT_LOGD_STR("Creating CTF writer structure field type object."); | |
844 | ||
845 | if (!structure) { | |
846 | BT_LOGE_STR("Failed to allocate one structure field type."); | |
847 | goto error; | |
848 | } | |
849 | ||
850 | bt_field_type_common_structure_initialize(BT_TO_COMMON(structure), | |
851 | bt_field_type_common_structure_destroy_recursive, | |
852 | &bt_ctf_field_type_structure_methods); | |
853 | structure->common.spec.writer.serialize_func = | |
854 | bt_ctf_field_type_structure_serialize_recursive; | |
855 | BT_LOGD("Created CTF writer structure field type object: addr=%p", | |
856 | structure); | |
857 | goto end; | |
858 | ||
859 | error: | |
860 | BT_PUT(structure); | |
861 | ||
862 | end: | |
863 | return (void *) structure; | |
864 | } | |
865 | ||
866 | int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *ft, | |
867 | struct bt_ctf_field_type *field_type, | |
868 | const char *field_name) | |
869 | { | |
870 | return bt_field_type_common_structure_add_field((void *) ft, | |
871 | (void *) field_type, field_name); | |
872 | } | |
873 | ||
874 | int64_t bt_ctf_field_type_structure_get_field_count(struct bt_ctf_field_type *ft) | |
875 | { | |
876 | return bt_field_type_common_structure_get_field_count((void *) ft); | |
877 | } | |
878 | ||
879 | int bt_ctf_field_type_structure_get_field_by_index( | |
880 | struct bt_ctf_field_type *ft, | |
881 | const char **field_name, | |
882 | struct bt_ctf_field_type **field_type, uint64_t index) | |
883 | { | |
5fe68922 | 884 | int ret = bt_field_type_common_structure_borrow_field_by_index( |
8deee039 | 885 | (void *) ft, field_name, (void *) field_type, index); |
5fe68922 PP |
886 | |
887 | if (ret == 0 && field_type) { | |
888 | bt_get(*field_type); | |
889 | } | |
890 | ||
891 | return ret; | |
8deee039 PP |
892 | } |
893 | ||
894 | struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name( | |
895 | struct bt_ctf_field_type *ft, const char *name) | |
896 | { | |
5fe68922 PP |
897 | return bt_get(bt_field_type_common_structure_borrow_field_type_by_name( |
898 | (void *) ft, name)); | |
8deee039 PP |
899 | } |
900 | ||
901 | struct bt_ctf_field_type *bt_ctf_field_type_variant_create( | |
902 | struct bt_ctf_field_type *tag_ft, const char *tag_name) | |
903 | { | |
904 | struct bt_field_type_common_variant *var_ft = NULL; | |
905 | ||
906 | BT_LOGD("Creating CTF writer variant field type object: " | |
907 | "tag-ft-addr=%p, tag-field-name=\"%s\"", | |
908 | tag_ft, tag_name); | |
909 | ||
910 | if (tag_name && !bt_identifier_is_valid(tag_name)) { | |
911 | BT_LOGW("Invalid parameter: tag field name is not a valid CTF identifier: " | |
912 | "tag-ft-addr=%p, tag-field-name=\"%s\"", | |
913 | tag_ft, tag_name); | |
914 | goto error; | |
915 | } | |
916 | ||
917 | var_ft = g_new0(struct bt_field_type_common_variant, 1); | |
918 | if (!var_ft) { | |
919 | BT_LOGE_STR("Failed to allocate one variant field type."); | |
920 | goto error; | |
921 | } | |
922 | ||
923 | bt_field_type_common_variant_initialize(BT_TO_COMMON(var_ft), | |
924 | (void *) tag_ft, tag_name, | |
925 | bt_field_type_common_variant_destroy_recursive, | |
926 | &bt_ctf_field_type_variant_methods); | |
927 | var_ft->common.spec.writer.serialize_func = | |
928 | bt_ctf_field_type_variant_serialize_recursive; | |
929 | BT_LOGD("Created CTF writer variant field type object: addr=%p, " | |
930 | "tag-ft-addr=%p, tag-field-name=\"%s\"", | |
931 | var_ft, tag_ft, tag_name); | |
932 | goto end; | |
933 | ||
934 | error: | |
935 | BT_PUT(var_ft); | |
936 | ||
937 | end: | |
938 | return (void *) var_ft; | |
939 | } | |
940 | ||
941 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_field_type( | |
942 | struct bt_ctf_field_type *ft) | |
943 | { | |
5fe68922 PP |
944 | return bt_get(bt_field_type_common_variant_borrow_tag_field_type( |
945 | (void *) ft)); | |
8deee039 PP |
946 | } |
947 | ||
948 | const char *bt_ctf_field_type_variant_get_tag_name(struct bt_ctf_field_type *ft) | |
949 | { | |
950 | return bt_field_type_common_variant_get_tag_name((void *) ft); | |
951 | } | |
952 | ||
953 | int bt_ctf_field_type_variant_set_tag_name( | |
954 | struct bt_ctf_field_type *ft, const char *name) | |
955 | { | |
956 | return bt_field_type_common_variant_set_tag_name((void *) ft, name); | |
957 | } | |
958 | ||
959 | int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *ft, | |
960 | struct bt_ctf_field_type *field_type, | |
961 | const char *field_name) | |
962 | { | |
963 | return bt_field_type_common_variant_add_field((void *) ft, | |
964 | (void *) field_type, field_name); | |
965 | } | |
966 | ||
967 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name( | |
968 | struct bt_ctf_field_type *ft, | |
969 | const char *field_name) | |
970 | { | |
5fe68922 PP |
971 | return bt_get(bt_field_type_common_variant_borrow_field_type_by_name( |
972 | (void *) ft, field_name)); | |
8deee039 PP |
973 | } |
974 | ||
975 | struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag( | |
976 | struct bt_ctf_field_type *ft, | |
977 | struct bt_ctf_field *tag_field) | |
978 | { | |
5fe68922 | 979 | return bt_get(bt_field_type_common_variant_borrow_field_type_from_tag( |
8deee039 | 980 | (void *) ft, (void *) tag_field, |
5fe68922 | 981 | (bt_field_common_create_func) bt_field_create)); |
8deee039 PP |
982 | } |
983 | ||
984 | int64_t bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *ft) | |
985 | { | |
986 | return bt_field_type_common_variant_get_field_count((void *) ft); | |
987 | } | |
988 | ||
989 | int bt_ctf_field_type_variant_get_field_by_index(struct bt_ctf_field_type *ft, | |
990 | const char **field_name, struct bt_ctf_field_type **field_type, | |
991 | uint64_t index) | |
992 | { | |
5fe68922 PP |
993 | int ret = bt_field_type_common_variant_borrow_field_by_index( |
994 | (void *) ft, field_name, (void *) field_type, index); | |
995 | ||
996 | if (ret == 0 && field_type) { | |
997 | bt_get(*field_type); | |
998 | } | |
999 | ||
1000 | return ret; | |
8deee039 PP |
1001 | } |
1002 | ||
1003 | struct bt_ctf_field_type *bt_ctf_field_type_array_create( | |
1004 | struct bt_ctf_field_type *element_ft, unsigned int length) | |
1005 | { | |
1006 | struct bt_field_type_common_array *array = NULL; | |
1007 | ||
1008 | BT_LOGD("Creating CTF writer array field type object: element-ft-addr=%p, " | |
1009 | "length=%u", element_ft, length); | |
1010 | ||
1011 | if (!element_ft) { | |
1012 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); | |
1013 | goto error; | |
1014 | } | |
1015 | ||
1016 | if (length == 0) { | |
1017 | BT_LOGW_STR("Invalid parameter: length is zero."); | |
1018 | goto error; | |
1019 | } | |
1020 | ||
1021 | array = g_new0(struct bt_field_type_common_array, 1); | |
1022 | if (!array) { | |
1023 | BT_LOGE_STR("Failed to allocate one array field type."); | |
1024 | goto error; | |
1025 | } | |
1026 | ||
1027 | bt_field_type_common_array_initialize(BT_TO_COMMON(array), | |
1028 | (void *) element_ft, length, | |
1029 | bt_field_type_common_array_destroy_recursive, | |
1030 | &bt_ctf_field_type_array_methods); | |
1031 | array->common.spec.writer.serialize_func = | |
1032 | bt_ctf_field_type_array_serialize_recursive; | |
1033 | BT_LOGD("Created CTF writer array field type object: addr=%p, " | |
1034 | "element-ft-addr=%p, length=%u", | |
1035 | array, element_ft, length); | |
1036 | goto end; | |
1037 | ||
1038 | error: | |
1039 | BT_PUT(array); | |
1040 | ||
1041 | end: | |
1042 | return (void *) array; | |
1043 | } | |
1044 | ||
1045 | struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_field_type( | |
1046 | struct bt_ctf_field_type *ft) | |
1047 | { | |
5fe68922 PP |
1048 | return bt_get(bt_field_type_common_array_borrow_element_field_type( |
1049 | (void *) ft)); | |
8deee039 PP |
1050 | } |
1051 | ||
1052 | int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *ft) | |
1053 | { | |
1054 | return bt_field_type_common_array_get_length((void *) ft); | |
1055 | } | |
1056 | ||
1057 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_create( | |
1058 | struct bt_ctf_field_type *element_ft, | |
1059 | const char *length_field_name) | |
1060 | { | |
1061 | struct bt_field_type_common_sequence *sequence = NULL; | |
1062 | ||
1063 | BT_LOGD("Creating CTF writer sequence field type object: element-ft-addr=%p, " | |
1064 | "length-field-name=\"%s\"", element_ft, length_field_name); | |
1065 | ||
1066 | if (!element_ft) { | |
1067 | BT_LOGW_STR("Invalid parameter: element field type is NULL."); | |
1068 | goto error; | |
1069 | } | |
1070 | ||
1071 | if (!bt_identifier_is_valid(length_field_name)) { | |
1072 | BT_LOGW("Invalid parameter: length field name is not a valid CTF identifier: " | |
1073 | "length-field-name=\"%s\"", length_field_name); | |
1074 | goto error; | |
1075 | } | |
1076 | ||
1077 | sequence = g_new0(struct bt_field_type_common_sequence, 1); | |
1078 | if (!sequence) { | |
1079 | BT_LOGE_STR("Failed to allocate one sequence field type."); | |
1080 | goto error; | |
1081 | } | |
1082 | ||
1083 | bt_field_type_common_sequence_initialize(BT_TO_COMMON(sequence), | |
1084 | (void *) element_ft, length_field_name, | |
1085 | bt_field_type_common_sequence_destroy_recursive, | |
1086 | &bt_ctf_field_type_sequence_methods); | |
1087 | sequence->common.spec.writer.serialize_func = | |
1088 | bt_ctf_field_type_sequence_serialize_recursive; | |
1089 | BT_LOGD("Created CTF writer sequence field type object: addr=%p, " | |
1090 | "element-ft-addr=%p, length-field-name=\"%s\"", | |
1091 | sequence, element_ft, length_field_name); | |
1092 | goto end; | |
1093 | ||
1094 | error: | |
1095 | BT_PUT(sequence); | |
1096 | ||
1097 | end: | |
1098 | return (void *) sequence; | |
1099 | } | |
1100 | ||
1101 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_field_type( | |
1102 | struct bt_ctf_field_type *ft) | |
1103 | { | |
5fe68922 PP |
1104 | return bt_get(bt_field_type_common_sequence_borrow_element_field_type( |
1105 | (void *) ft)); | |
8deee039 PP |
1106 | } |
1107 | ||
1108 | const char *bt_ctf_field_type_sequence_get_length_field_name( | |
1109 | struct bt_ctf_field_type *ft) | |
1110 | { | |
1111 | return bt_field_type_common_sequence_get_length_field_name((void *) ft); | |
1112 | } | |
1113 | ||
1114 | struct bt_ctf_field_type *bt_ctf_field_type_string_create(void) | |
1115 | { | |
1116 | struct bt_field_type_common_string *string = | |
1117 | g_new0(struct bt_field_type_common_string, 1); | |
1118 | ||
1119 | BT_LOGD_STR("Creating CTF writer string field type object."); | |
1120 | ||
1121 | if (!string) { | |
1122 | BT_LOGE_STR("Failed to allocate one string field type."); | |
1123 | return NULL; | |
1124 | } | |
1125 | ||
1126 | bt_field_type_common_string_initialize(BT_TO_COMMON(string), | |
1127 | bt_field_type_common_string_destroy, | |
1128 | &bt_ctf_field_type_string_methods); | |
1129 | string->common.spec.writer.serialize_func = | |
1130 | bt_ctf_field_type_string_serialize; | |
1131 | BT_LOGD("Created CTF writer string field type object: addr=%p", string); | |
1132 | return (void *) string; | |
1133 | } | |
1134 | ||
1135 | enum bt_ctf_string_encoding bt_ctf_field_type_string_get_encoding( | |
1136 | struct bt_ctf_field_type *ft) | |
1137 | { | |
1138 | return (int) bt_field_type_common_string_get_encoding((void *) ft); | |
1139 | } | |
1140 | ||
1141 | int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *ft, | |
1142 | enum bt_ctf_string_encoding encoding) | |
1143 | { | |
1144 | return bt_field_type_common_string_set_encoding((void *) ft, | |
1145 | (int) encoding); | |
1146 | } | |
1147 | ||
1148 | int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *ft) | |
1149 | { | |
1150 | return bt_field_type_common_get_alignment((void *) ft); | |
1151 | } | |
1152 | ||
1153 | int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *ft, | |
1154 | unsigned int alignment) | |
1155 | { | |
1156 | return bt_field_type_common_set_alignment((void *) ft, alignment); | |
1157 | } | |
1158 | ||
1159 | enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order( | |
1160 | struct bt_ctf_field_type *ft) | |
1161 | { | |
1162 | return (int) bt_field_type_common_get_byte_order((void *) ft); | |
1163 | } | |
1164 | ||
1165 | int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *ft, | |
1166 | enum bt_ctf_byte_order byte_order) | |
1167 | { | |
1168 | return bt_field_type_common_set_byte_order((void *) ft, | |
1169 | (int) byte_order); | |
1170 | } | |
1171 | ||
1172 | enum bt_ctf_field_type_id bt_ctf_field_type_get_type_id( | |
1173 | struct bt_ctf_field_type *ft) | |
1174 | { | |
1175 | return (int) bt_field_type_common_get_type_id((void *) ft); | |
1176 | } | |
1177 | ||
1178 | BT_HIDDEN | |
1179 | struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *ft) | |
1180 | { | |
1181 | return (void *) bt_field_type_common_copy((void *) ft); | |
1182 | } | |
1183 | ||
1184 | static | |
1185 | struct bt_ctf_field_type *bt_ctf_field_type_integer_copy( | |
1186 | struct bt_ctf_field_type *ft) | |
1187 | { | |
1188 | struct bt_field_type_common_integer *int_ft = (void *) ft; | |
1189 | struct bt_field_type_common_integer *copy_ft; | |
1190 | ||
1191 | BT_LOGD("Copying CTF writer integer field type's: addr=%p", ft); | |
1192 | copy_ft = (void *) bt_ctf_field_type_integer_create(int_ft->size); | |
1193 | if (!copy_ft) { | |
1194 | BT_LOGE_STR("Cannot create CTF writer integer field type."); | |
1195 | goto end; | |
1196 | } | |
1197 | ||
1198 | copy_ft->mapped_clock_class = bt_get(int_ft->mapped_clock_class); | |
1199 | copy_ft->user_byte_order = int_ft->user_byte_order; | |
1200 | copy_ft->is_signed = int_ft->is_signed; | |
1201 | copy_ft->size = int_ft->size; | |
1202 | copy_ft->base = int_ft->base; | |
1203 | copy_ft->encoding = int_ft->encoding; | |
1204 | BT_LOGD("Copied CTF writer integer field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1205 | ft, copy_ft); | |
1206 | ||
1207 | end: | |
1208 | return (void *) copy_ft; | |
1209 | } | |
1210 | ||
1211 | static | |
1212 | struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy_recursive( | |
1213 | struct bt_ctf_field_type *ft) | |
1214 | { | |
1215 | size_t i; | |
1216 | struct bt_field_type_common_enumeration *enum_ft = (void *) ft; | |
1217 | struct bt_field_type_common_enumeration *copy_ft = NULL; | |
1218 | struct bt_field_type_common_enumeration *container_copy_ft; | |
1219 | ||
1220 | BT_LOGD("Copying CTF writer enumeration field type's: addr=%p", ft); | |
1221 | ||
1222 | /* Copy the source enumeration's container */ | |
1223 | BT_LOGD_STR("Copying CTF writer enumeration field type's container field type."); | |
1224 | container_copy_ft = BT_FROM_COMMON(bt_field_type_common_copy( | |
1225 | BT_TO_COMMON(enum_ft->container_ft))); | |
1226 | if (!container_copy_ft) { | |
1227 | BT_LOGE_STR("Cannot copy CTF writer enumeration field type's container field type."); | |
1228 | goto end; | |
1229 | } | |
1230 | ||
1231 | copy_ft = (void *) bt_ctf_field_type_enumeration_create( | |
1232 | (void *) container_copy_ft); | |
1233 | if (!copy_ft) { | |
1234 | BT_LOGE_STR("Cannot create CTF writer enumeration field type."); | |
1235 | goto end; | |
1236 | } | |
1237 | ||
1238 | /* Copy all enumaration entries */ | |
1239 | for (i = 0; i < enum_ft->entries->len; i++) { | |
1240 | struct enumeration_mapping *mapping = g_ptr_array_index( | |
1241 | enum_ft->entries, i); | |
1242 | struct enumeration_mapping *copy_mapping = g_new0( | |
1243 | struct enumeration_mapping, 1); | |
1244 | ||
1245 | if (!copy_mapping) { | |
1246 | BT_LOGE_STR("Failed to allocate one enumeration mapping."); | |
1247 | goto error; | |
1248 | } | |
1249 | ||
1250 | *copy_mapping = *mapping; | |
1251 | g_ptr_array_add(copy_ft->entries, copy_mapping); | |
1252 | } | |
1253 | ||
1254 | BT_LOGD("Copied CTF writer enumeration field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1255 | ft, copy_ft); | |
1256 | ||
1257 | end: | |
1258 | bt_put(container_copy_ft); | |
1259 | return (void *) copy_ft; | |
1260 | ||
1261 | error: | |
1262 | bt_put(container_copy_ft); | |
1263 | BT_PUT(copy_ft); | |
1264 | return (void *) copy_ft; | |
1265 | } | |
1266 | ||
1267 | static | |
1268 | struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy( | |
1269 | struct bt_ctf_field_type *ft) | |
1270 | { | |
1271 | struct bt_field_type_common_floating_point *flt_ft = BT_FROM_COMMON(ft); | |
1272 | struct bt_field_type_common_floating_point *copy_ft; | |
1273 | ||
1274 | BT_LOGD("Copying CTF writer floating point number field type's: addr=%p", ft); | |
1275 | copy_ft = (void *) bt_ctf_field_type_floating_point_create(); | |
1276 | if (!copy_ft) { | |
1277 | BT_LOGE_STR("Cannot create CTF writer floating point number field type."); | |
1278 | goto end; | |
1279 | } | |
1280 | ||
1281 | copy_ft->user_byte_order = flt_ft->user_byte_order; | |
1282 | copy_ft->exp_dig = flt_ft->exp_dig; | |
1283 | copy_ft->mant_dig = flt_ft->mant_dig; | |
1284 | BT_LOGD("Copied CTF writer floating point number field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1285 | ft, copy_ft); | |
1286 | ||
1287 | end: | |
1288 | return (void *) copy_ft; | |
1289 | } | |
1290 | ||
1291 | static | |
1292 | struct bt_ctf_field_type *bt_ctf_field_type_structure_copy_recursive( | |
1293 | struct bt_ctf_field_type *ft) | |
1294 | { | |
1295 | int64_t i; | |
1296 | GHashTableIter iter; | |
1297 | gpointer key, value; | |
1298 | struct bt_field_type_common_structure *struct_ft = (void *) ft; | |
1299 | struct bt_field_type_common_structure *copy_ft; | |
1300 | ||
1301 | BT_LOGD("Copying CTF writer structure field type's: addr=%p", ft); | |
1302 | copy_ft = (void *) bt_ctf_field_type_structure_create(); | |
1303 | if (!copy_ft) { | |
1304 | BT_LOGE_STR("Cannot create CTF writer structure field type."); | |
1305 | goto end; | |
1306 | } | |
1307 | ||
1308 | /* Copy field_name_to_index */ | |
1309 | g_hash_table_iter_init(&iter, struct_ft->field_name_to_index); | |
1310 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
1311 | g_hash_table_insert(copy_ft->field_name_to_index, | |
1312 | key, value); | |
1313 | } | |
1314 | ||
1315 | for (i = 0; i < struct_ft->fields->len; i++) { | |
1316 | struct structure_field_common *entry, *copy_entry; | |
1317 | struct bt_field_type_common *field_ft_copy; | |
1318 | ||
1319 | entry = g_ptr_array_index(struct_ft->fields, i); | |
1320 | BT_LOGD("Copying CTF writer structure field type's field: " | |
1321 | "index=%" PRId64 ", " | |
1322 | "field-ft-addr=%p, field-name=\"%s\"", | |
1323 | i, entry, g_quark_to_string(entry->name)); | |
1324 | copy_entry = g_new0(struct structure_field_common, 1); | |
1325 | if (!copy_entry) { | |
1326 | BT_LOGE_STR("Failed to allocate one structure field type field."); | |
1327 | goto error; | |
1328 | } | |
1329 | ||
1330 | field_ft_copy = (void *) bt_ctf_field_type_copy( | |
1331 | (void *) entry->type); | |
1332 | if (!field_ft_copy) { | |
1333 | BT_LOGE("Cannot copy CTF writer structure field type's field: " | |
1334 | "index=%" PRId64 ", " | |
1335 | "field-ft-addr=%p, field-name=\"%s\"", | |
1336 | i, entry, g_quark_to_string(entry->name)); | |
1337 | g_free(copy_entry); | |
1338 | goto error; | |
1339 | } | |
1340 | ||
1341 | copy_entry->name = entry->name; | |
1342 | copy_entry->type = field_ft_copy; | |
1343 | g_ptr_array_add(copy_ft->fields, copy_entry); | |
1344 | } | |
1345 | ||
1346 | BT_LOGD("Copied CTF writer structure field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1347 | ft, copy_ft); | |
1348 | ||
1349 | end: | |
1350 | return (void *) copy_ft; | |
1351 | ||
1352 | error: | |
1353 | BT_PUT(copy_ft); | |
1354 | return NULL; | |
1355 | } | |
1356 | ||
1357 | static | |
1358 | struct bt_ctf_field_type *bt_ctf_field_type_variant_copy_recursive( | |
1359 | struct bt_ctf_field_type *ft) | |
1360 | { | |
1361 | int64_t i; | |
1362 | GHashTableIter iter; | |
1363 | gpointer key, value; | |
1364 | struct bt_field_type_common *tag_ft_copy = NULL; | |
1365 | struct bt_field_type_common_variant *var_ft = (void *) ft; | |
1366 | struct bt_field_type_common_variant *copy_ft = NULL; | |
1367 | ||
1368 | BT_LOGD("Copying CTF writer variant field type's: addr=%p", ft); | |
1369 | if (var_ft->tag_ft) { | |
1370 | BT_LOGD_STR("Copying CTF writer variant field type's tag field type."); | |
1371 | tag_ft_copy = bt_field_type_common_copy( | |
1372 | BT_TO_COMMON(var_ft->tag_ft)); | |
1373 | if (!tag_ft_copy) { | |
1374 | BT_LOGE_STR("Cannot copy CTF writer variant field type's tag field type."); | |
1375 | goto end; | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | copy_ft = (void *) bt_ctf_field_type_variant_create( | |
1380 | (void *) tag_ft_copy, | |
1381 | var_ft->tag_name->len ? var_ft->tag_name->str : NULL); | |
1382 | if (!copy_ft) { | |
1383 | BT_LOGE_STR("Cannot create CTF writer variant field type."); | |
1384 | goto end; | |
1385 | } | |
1386 | ||
1387 | /* Copy field_name_to_index */ | |
1388 | g_hash_table_iter_init(&iter, var_ft->field_name_to_index); | |
1389 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
1390 | g_hash_table_insert(copy_ft->field_name_to_index, | |
1391 | key, value); | |
1392 | } | |
1393 | ||
1394 | for (i = 0; i < var_ft->fields->len; i++) { | |
1395 | struct structure_field_common *entry, *copy_entry; | |
1396 | struct bt_field_type_common *field_ft_copy; | |
1397 | ||
1398 | entry = g_ptr_array_index(var_ft->fields, i); | |
1399 | BT_LOGD("Copying CTF writer variant field type's field: " | |
1400 | "index=%" PRId64 ", " | |
1401 | "field-ft-addr=%p, field-name=\"%s\"", | |
1402 | i, entry, g_quark_to_string(entry->name)); | |
1403 | copy_entry = g_new0(struct structure_field_common, 1); | |
1404 | if (!copy_entry) { | |
1405 | BT_LOGE_STR("Failed to allocate one variant field type field."); | |
1406 | goto error; | |
1407 | } | |
1408 | ||
1409 | field_ft_copy = (void *) bt_ctf_field_type_copy( | |
1410 | (void *) entry->type); | |
1411 | if (!field_ft_copy) { | |
1412 | BT_LOGE("Cannot copy CTF writer variant field type's field: " | |
1413 | "index=%" PRId64 ", " | |
1414 | "field-ft-addr=%p, field-name=\"%s\"", | |
1415 | i, entry, g_quark_to_string(entry->name)); | |
1416 | g_free(copy_entry); | |
1417 | goto error; | |
1418 | } | |
1419 | ||
1420 | copy_entry->name = entry->name; | |
1421 | copy_entry->type = field_ft_copy; | |
1422 | g_ptr_array_add(copy_ft->fields, copy_entry); | |
1423 | } | |
1424 | ||
1425 | if (var_ft->tag_field_path) { | |
1426 | BT_LOGD_STR("Copying CTF writer variant field type's tag field path."); | |
1427 | copy_ft->tag_field_path = bt_field_path_copy( | |
1428 | var_ft->tag_field_path); | |
1429 | if (!copy_ft->tag_field_path) { | |
1430 | BT_LOGE_STR("Cannot copy CTF writer variant field type's tag field path."); | |
1431 | goto error; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | BT_LOGD("Copied variant field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1436 | ft, copy_ft); | |
1437 | ||
1438 | end: | |
1439 | bt_put(tag_ft_copy); | |
1440 | return (void *) copy_ft; | |
1441 | ||
1442 | error: | |
1443 | bt_put(tag_ft_copy); | |
1444 | BT_PUT(copy_ft); | |
1445 | return NULL; | |
1446 | } | |
1447 | ||
1448 | static | |
1449 | struct bt_ctf_field_type *bt_ctf_field_type_array_copy_recursive( | |
1450 | struct bt_ctf_field_type *ft) | |
1451 | { | |
1452 | struct bt_field_type_common *container_ft_copy = NULL; | |
1453 | struct bt_field_type_common_array *array_ft = (void *) ft; | |
1454 | struct bt_field_type_common_array *copy_ft = NULL; | |
1455 | ||
1456 | BT_LOGD("Copying CTF writer array field type's: addr=%p", ft); | |
1457 | BT_LOGD_STR("Copying CTF writer array field type's element field type."); | |
1458 | container_ft_copy = bt_field_type_common_copy(array_ft->element_ft); | |
1459 | if (!container_ft_copy) { | |
1460 | BT_LOGE_STR("Cannot copy CTF writer array field type's element field type."); | |
1461 | goto end; | |
1462 | } | |
1463 | ||
1464 | copy_ft = (void *) bt_ctf_field_type_array_create( | |
1465 | (void *) container_ft_copy, array_ft->length); | |
1466 | if (!copy_ft) { | |
1467 | BT_LOGE_STR("Cannot create CTF writer array field type."); | |
1468 | goto end; | |
1469 | } | |
1470 | ||
1471 | BT_LOGD("Copied CTF writer array field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1472 | ft, copy_ft); | |
1473 | ||
1474 | end: | |
1475 | bt_put(container_ft_copy); | |
1476 | return (void *) copy_ft; | |
1477 | } | |
1478 | ||
1479 | static | |
1480 | struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy_recursive( | |
1481 | struct bt_ctf_field_type *ft) | |
1482 | { | |
1483 | struct bt_field_type_common *container_ft_copy = NULL; | |
1484 | struct bt_field_type_common_sequence *seq_ft = (void *) ft; | |
1485 | struct bt_field_type_common_sequence *copy_ft = NULL; | |
1486 | ||
1487 | BT_LOGD("Copying CTF writer sequence field type's: addr=%p", ft); | |
1488 | BT_LOGD_STR("Copying CTF writer sequence field type's element field type."); | |
1489 | container_ft_copy = bt_field_type_common_copy(seq_ft->element_ft); | |
1490 | if (!container_ft_copy) { | |
1491 | BT_LOGE_STR("Cannot copy CTF writer sequence field type's element field type."); | |
1492 | goto end; | |
1493 | } | |
1494 | ||
1495 | copy_ft = (void *) bt_ctf_field_type_sequence_create( | |
1496 | (void *) container_ft_copy, | |
1497 | seq_ft->length_field_name->len ? | |
1498 | seq_ft->length_field_name->str : NULL); | |
1499 | if (!copy_ft) { | |
1500 | BT_LOGE_STR("Cannot create CTF writer sequence field type."); | |
1501 | goto end; | |
1502 | } | |
1503 | ||
1504 | if (seq_ft->length_field_path) { | |
1505 | BT_LOGD_STR("Copying CTF writer sequence field type's length field path."); | |
1506 | copy_ft->length_field_path = bt_field_path_copy( | |
1507 | seq_ft->length_field_path); | |
1508 | if (!copy_ft->length_field_path) { | |
1509 | BT_LOGE_STR("Cannot copy CTF writer sequence field type's length field path."); | |
1510 | goto error; | |
1511 | } | |
1512 | } | |
1513 | ||
1514 | BT_LOGD("Copied CTF writer sequence field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1515 | ft, copy_ft); | |
1516 | ||
1517 | end: | |
1518 | bt_put(container_ft_copy); | |
1519 | return (void *) copy_ft; | |
1520 | error: | |
1521 | bt_put(container_ft_copy); | |
1522 | BT_PUT(copy_ft); | |
1523 | return NULL; | |
1524 | } | |
1525 | ||
1526 | static | |
1527 | struct bt_ctf_field_type *bt_ctf_field_type_string_copy(struct bt_ctf_field_type *ft) | |
1528 | { | |
1529 | struct bt_field_type_common_string *string_ft = (void *) ft; | |
1530 | struct bt_field_type_common_string *copy_ft = NULL; | |
1531 | ||
1532 | BT_LOGD("Copying CTF writer string field type's: addr=%p", ft); | |
1533 | copy_ft = (void *) bt_ctf_field_type_string_create(); | |
1534 | if (!copy_ft) { | |
1535 | BT_LOGE_STR("Cannot create CTF writer string field type."); | |
1536 | goto end; | |
1537 | } | |
1538 | ||
1539 | copy_ft->encoding = string_ft->encoding; | |
1540 | BT_LOGD("Copied CTF writer string field type: original-ft-addr=%p, copy-ft-addr=%p", | |
1541 | ft, copy_ft); | |
1542 | ||
1543 | end: | |
1544 | return (void *) copy_ft; | |
1545 | } |