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-FIELDS" | |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
086dc475 PP |
28 | #include <babeltrace/assert-pre-internal.h> |
29 | #include <babeltrace/align-internal.h> | |
30 | #include <babeltrace/assert-internal.h> | |
8deee039 | 31 | #include <babeltrace/compat/fcntl-internal.h> |
086dc475 | 32 | #include <babeltrace/compiler-internal.h> |
8deee039 | 33 | #include <babeltrace/ctf-writer/field-types-internal.h> |
086dc475 | 34 | #include <babeltrace/ctf-writer/fields-internal.h> |
086dc475 | 35 | #include <babeltrace/endian-internal.h> |
c2606e2f PP |
36 | #include <babeltrace/ctf-writer/object-internal.h> |
37 | #include <babeltrace/ctf-writer/object.h> | |
6f4eefbe | 38 | #include <babeltrace/ctfser-internal.h> |
8deee039 PP |
39 | #include <float.h> |
40 | #include <inttypes.h> | |
086dc475 | 41 | #include <inttypes.h> |
8deee039 PP |
42 | #include <stdlib.h> |
43 | ||
086dc475 PP |
44 | #define BT_ASSERT_PRE_CTF_FIELD_IS_INT_OR_ENUM(_field, _name) \ |
45 | BT_ASSERT_PRE((_field)->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || \ | |
46 | (_field)->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, \ | |
47 | _name " is not an integer or an enumeration field: " \ | |
48 | "field-addr=%p", (_field)) | |
49 | ||
50 | BT_HIDDEN | |
51 | struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field) | |
52 | { | |
53 | struct bt_ctf_field_common *copy = NULL; | |
54 | ||
55 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
56 | BT_ASSERT(field_type_common_has_known_id(field->type)); | |
57 | BT_ASSERT(field->methods->copy); | |
58 | copy = field->methods->copy(field); | |
59 | if (!copy) { | |
60 | BT_LOGW("Cannot create field: ft-addr=%p", field->type); | |
61 | goto end; | |
62 | } | |
63 | ||
64 | bt_ctf_field_common_set(copy, field->payload_set); | |
65 | ||
66 | end: | |
67 | return copy; | |
68 | } | |
69 | ||
70 | BT_HIDDEN | |
71 | int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field, | |
72 | struct bt_ctf_field_type_common *type, | |
c2606e2f | 73 | bool is_shared, bt_ctf_object_release_func release_func, |
086dc475 PP |
74 | struct bt_ctf_field_common_methods *methods, |
75 | bt_ctf_field_common_create_func field_create_func, | |
76 | GDestroyNotify field_release_func) | |
77 | { | |
78 | int ret = 0; | |
79 | struct bt_ctf_field_type_common_structure *structure_type = | |
80 | BT_CTF_FROM_COMMON(type); | |
81 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); | |
82 | size_t i; | |
83 | ||
84 | BT_LOGD("Initializing common structure field object: ft-addr=%p", type); | |
85 | bt_ctf_field_common_initialize(field, type, is_shared, | |
86 | release_func, methods); | |
87 | structure->fields = g_ptr_array_new_with_free_func(field_release_func); | |
88 | g_ptr_array_set_size(structure->fields, structure_type->fields->len); | |
89 | ||
90 | /* Create all fields contained in the structure field. */ | |
91 | for (i = 0; i < structure_type->fields->len; i++) { | |
92 | struct bt_ctf_field_common *field; | |
93 | struct bt_ctf_field_type_common_structure_field *struct_field = | |
94 | BT_CTF_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX( | |
95 | structure_type, i); | |
96 | field = field_create_func(struct_field->type); | |
97 | if (!field) { | |
98 | BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu", | |
99 | g_quark_to_string(struct_field->name), i); | |
100 | ret = -1; | |
101 | goto end; | |
102 | } | |
103 | ||
104 | g_ptr_array_index(structure->fields, i) = field; | |
105 | } | |
106 | ||
107 | BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p", | |
108 | field, type); | |
109 | ||
110 | end: | |
111 | return ret; | |
112 | } | |
113 | ||
114 | BT_HIDDEN | |
115 | int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field, | |
116 | struct bt_ctf_field_type_common *type, | |
c2606e2f | 117 | bool is_shared, bt_ctf_object_release_func release_func, |
086dc475 PP |
118 | struct bt_ctf_field_common_methods *methods, |
119 | bt_ctf_field_common_create_func field_create_func, | |
120 | GDestroyNotify field_release_func) | |
121 | { | |
122 | int ret = 0; | |
123 | struct bt_ctf_field_type_common_variant *variant_type = | |
124 | BT_CTF_FROM_COMMON(type); | |
125 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); | |
126 | size_t i; | |
127 | ||
128 | BT_LOGD("Initializing common variant field object: ft-addr=%p", type); | |
129 | bt_ctf_field_common_initialize(field, type, is_shared, | |
130 | release_func, methods); | |
131 | ret = bt_ctf_field_type_common_variant_update_choices(type); | |
132 | if (ret) { | |
133 | BT_LOGE("Cannot update common variant field type choices: " | |
134 | "ret=%d", ret); | |
135 | goto end; | |
136 | } | |
137 | ||
138 | variant->fields = g_ptr_array_new_with_free_func(field_release_func); | |
139 | g_ptr_array_set_size(variant->fields, variant_type->choices->len); | |
140 | ||
141 | /* Create all fields contained in the variant field. */ | |
142 | for (i = 0; i < variant_type->choices->len; i++) { | |
143 | struct bt_ctf_field_common *field; | |
144 | struct bt_ctf_field_type_common_variant_choice *var_choice = | |
145 | BT_CTF_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX( | |
146 | variant_type, i); | |
147 | ||
148 | field = field_create_func(var_choice->type); | |
149 | if (!field) { | |
150 | BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu", | |
151 | g_quark_to_string(var_choice->name), i); | |
152 | ret = -1; | |
153 | goto end; | |
154 | } | |
155 | ||
156 | g_ptr_array_index(variant->fields, i) = field; | |
157 | } | |
158 | ||
159 | BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p", | |
160 | field, type); | |
161 | ||
162 | end: | |
163 | return ret; | |
164 | } | |
165 | ||
166 | BT_HIDDEN | |
167 | int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field, | |
168 | struct bt_ctf_field_type_common *type, | |
c2606e2f | 169 | bool is_shared, bt_ctf_object_release_func release_func, |
086dc475 PP |
170 | struct bt_ctf_field_common_methods *methods) |
171 | { | |
172 | int ret = 0; | |
173 | struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field); | |
174 | ||
175 | BT_LOGD("Initializing common string field object: ft-addr=%p", type); | |
176 | bt_ctf_field_common_initialize(field, type, is_shared, | |
177 | release_func, methods); | |
178 | string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1); | |
179 | if (!string->buf) { | |
180 | ret = -1; | |
181 | goto end; | |
182 | } | |
183 | ||
184 | g_array_index(string->buf, char, 0) = '\0'; | |
185 | BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p", | |
186 | field, type); | |
187 | ||
188 | end: | |
189 | return ret; | |
190 | } | |
191 | ||
192 | BT_HIDDEN | |
193 | int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field, | |
194 | struct bt_ctf_field_type_common *type, | |
c2606e2f | 195 | bool is_shared, bt_ctf_object_release_func release_func, |
086dc475 PP |
196 | struct bt_ctf_field_common_methods *methods, |
197 | bt_ctf_field_common_create_func field_create_func, | |
198 | GDestroyNotify field_destroy_func) | |
199 | { | |
200 | struct bt_ctf_field_type_common_array *array_type = BT_CTF_FROM_COMMON(type); | |
201 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); | |
202 | unsigned int array_length; | |
203 | int ret = 0; | |
204 | uint64_t i; | |
205 | ||
206 | BT_LOGD("Initializing common array field object: ft-addr=%p", type); | |
207 | BT_ASSERT(type); | |
208 | bt_ctf_field_common_initialize(field, type, is_shared, | |
209 | release_func, methods); | |
210 | array_length = array_type->length; | |
211 | array->elements = g_ptr_array_sized_new(array_length); | |
212 | if (!array->elements) { | |
213 | ret = -1; | |
214 | goto end; | |
215 | } | |
216 | ||
217 | g_ptr_array_set_free_func(array->elements, field_destroy_func); | |
218 | g_ptr_array_set_size(array->elements, array_length); | |
219 | ||
220 | for (i = 0; i < array_length; i++) { | |
221 | array->elements->pdata[i] = field_create_func( | |
222 | array_type->element_ft); | |
223 | if (!array->elements->pdata[i]) { | |
224 | ret = -1; | |
225 | goto end; | |
226 | } | |
227 | } | |
228 | ||
229 | BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p", | |
230 | field, type); | |
231 | ||
232 | end: | |
233 | return ret; | |
234 | } | |
235 | ||
236 | BT_HIDDEN | |
237 | int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field, | |
238 | struct bt_ctf_field_type_common *type, | |
c2606e2f | 239 | bool is_shared, bt_ctf_object_release_func release_func, |
086dc475 PP |
240 | struct bt_ctf_field_common_methods *methods, |
241 | GDestroyNotify field_destroy_func) | |
242 | { | |
243 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); | |
244 | int ret = 0; | |
245 | ||
246 | BT_LOGD("Initializing common sequence field object: ft-addr=%p", type); | |
247 | BT_ASSERT(type); | |
248 | bt_ctf_field_common_initialize(field, type, is_shared, | |
249 | release_func, methods); | |
250 | sequence->elements = g_ptr_array_new(); | |
251 | if (!sequence->elements) { | |
252 | ret = -1; | |
253 | goto end; | |
254 | } | |
255 | ||
256 | g_ptr_array_set_free_func(sequence->elements, field_destroy_func); | |
257 | BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p", | |
258 | field, type); | |
259 | ||
260 | end: | |
261 | return ret; | |
262 | } | |
263 | ||
264 | BT_HIDDEN | |
265 | int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field) | |
266 | { | |
267 | return (field && field->payload_set) ? 0 : -1; | |
268 | } | |
269 | ||
270 | BT_HIDDEN | |
271 | int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field) | |
272 | { | |
273 | int64_t i; | |
274 | int ret = 0; | |
275 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); | |
276 | ||
277 | BT_ASSERT(field); | |
278 | ||
279 | for (i = 0; i < structure->fields->len; i++) { | |
280 | ret = bt_ctf_field_common_validate_recursive( | |
281 | (void *) structure->fields->pdata[i]); | |
282 | ||
283 | if (ret) { | |
284 | int this_ret; | |
285 | const char *name; | |
286 | ||
287 | this_ret = bt_ctf_field_type_common_structure_borrow_field_by_index( | |
288 | field->type, &name, NULL, i); | |
289 | BT_ASSERT(this_ret == 0); | |
290 | BT_ASSERT_PRE_MSG("Invalid structure field's field: " | |
291 | "struct-field-addr=%p, field-name=\"%s\", " | |
292 | "index=%" PRId64 ", field-addr=%p", | |
293 | field, name, i, structure->fields->pdata[i]); | |
294 | goto end; | |
295 | } | |
296 | } | |
297 | ||
298 | end: | |
299 | return ret; | |
300 | } | |
301 | ||
302 | BT_HIDDEN | |
303 | int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field) | |
304 | { | |
305 | int ret = 0; | |
306 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); | |
307 | ||
308 | BT_ASSERT(field); | |
309 | ||
310 | if (!variant->current_field) { | |
311 | ret = -1; | |
312 | goto end; | |
313 | } | |
314 | ||
315 | ret = bt_ctf_field_common_validate_recursive(variant->current_field); | |
316 | ||
317 | end: | |
318 | return ret; | |
319 | } | |
320 | ||
321 | BT_HIDDEN | |
322 | int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field) | |
323 | { | |
324 | int64_t i; | |
325 | int ret = 0; | |
326 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); | |
327 | ||
328 | BT_ASSERT(field); | |
329 | ||
330 | for (i = 0; i < array->elements->len; i++) { | |
331 | ret = bt_ctf_field_common_validate_recursive((void *) array->elements->pdata[i]); | |
332 | if (ret) { | |
333 | BT_ASSERT_PRE_MSG("Invalid array field's element field: " | |
334 | "array-field-addr=%p, " PRId64 ", " | |
335 | "elem-field-addr=%p", | |
336 | field, i, array->elements->pdata[i]); | |
337 | goto end; | |
338 | } | |
339 | } | |
340 | ||
341 | end: | |
342 | return ret; | |
343 | } | |
344 | ||
345 | BT_HIDDEN | |
346 | int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field) | |
347 | { | |
348 | size_t i; | |
349 | int ret = 0; | |
350 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); | |
351 | ||
352 | BT_ASSERT(field); | |
353 | ||
354 | for (i = 0; i < sequence->elements->len; i++) { | |
355 | ret = bt_ctf_field_common_validate_recursive( | |
356 | (void *) sequence->elements->pdata[i]); | |
357 | if (ret) { | |
358 | BT_ASSERT_PRE_MSG("Invalid sequence field's element field: " | |
359 | "seq-field-addr=%p, " PRId64 ", " | |
360 | "elem-field-addr=%p", | |
361 | field, i, sequence->elements->pdata[i]); | |
362 | goto end; | |
363 | } | |
364 | } | |
365 | end: | |
366 | return ret; | |
367 | } | |
368 | ||
369 | BT_HIDDEN | |
370 | void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field) | |
371 | { | |
372 | BT_ASSERT(field); | |
373 | field->payload_set = false; | |
374 | } | |
375 | ||
376 | BT_HIDDEN | |
377 | void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field) | |
378 | { | |
379 | int64_t i; | |
380 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); | |
381 | ||
382 | BT_ASSERT(field); | |
383 | ||
384 | for (i = 0; i < structure->fields->len; i++) { | |
385 | struct bt_ctf_field_common *member = structure->fields->pdata[i]; | |
386 | ||
387 | if (!member) { | |
388 | /* | |
389 | * Structure members are lazily initialized; | |
390 | * skip if this member has not been allocated | |
391 | * yet. | |
392 | */ | |
393 | continue; | |
394 | } | |
395 | ||
396 | bt_ctf_field_common_reset_recursive(member); | |
397 | } | |
398 | } | |
399 | ||
400 | BT_HIDDEN | |
401 | void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field) | |
402 | { | |
403 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); | |
404 | ||
405 | BT_ASSERT(field); | |
406 | variant->current_field = NULL; | |
407 | } | |
408 | ||
409 | BT_HIDDEN | |
410 | void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field) | |
411 | { | |
412 | size_t i; | |
413 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); | |
414 | ||
415 | BT_ASSERT(field); | |
416 | ||
417 | for (i = 0; i < array->elements->len; i++) { | |
418 | struct bt_ctf_field_common *member = array->elements->pdata[i]; | |
419 | ||
420 | if (!member) { | |
421 | /* | |
422 | * Array elements are lazily initialized; skip | |
423 | * if this member has not been allocated yet. | |
424 | */ | |
425 | continue; | |
426 | } | |
427 | ||
428 | bt_ctf_field_common_reset_recursive(member); | |
429 | } | |
430 | } | |
431 | ||
432 | BT_HIDDEN | |
433 | void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field) | |
434 | { | |
435 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); | |
436 | uint64_t i; | |
437 | ||
438 | BT_ASSERT(field); | |
439 | ||
440 | for (i = 0; i < sequence->elements->len; i++) { | |
441 | if (sequence->elements->pdata[i]) { | |
442 | bt_ctf_field_common_reset_recursive( | |
443 | sequence->elements->pdata[i]); | |
444 | } | |
445 | } | |
446 | ||
447 | sequence->length = 0; | |
448 | } | |
449 | ||
450 | BT_HIDDEN | |
451 | void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field, | |
452 | bool is_frozen) | |
453 | { | |
454 | field->frozen = is_frozen; | |
455 | } | |
456 | ||
457 | BT_HIDDEN | |
458 | void bt_ctf_field_common_structure_set_is_frozen_recursive( | |
459 | struct bt_ctf_field_common *field, bool is_frozen) | |
460 | { | |
461 | uint64_t i; | |
462 | struct bt_ctf_field_common_structure *structure_field = | |
463 | BT_CTF_FROM_COMMON(field); | |
464 | ||
465 | BT_LOGD("Freezing structure field object: addr=%p", field); | |
466 | ||
467 | for (i = 0; i < structure_field->fields->len; i++) { | |
468 | struct bt_ctf_field_common *struct_field = | |
469 | g_ptr_array_index(structure_field->fields, i); | |
470 | ||
471 | BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64, | |
472 | struct_field, i); | |
473 | bt_ctf_field_common_set_is_frozen_recursive(struct_field, | |
474 | is_frozen); | |
475 | } | |
476 | ||
477 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); | |
478 | } | |
479 | ||
480 | BT_HIDDEN | |
481 | void bt_ctf_field_common_variant_set_is_frozen_recursive( | |
482 | struct bt_ctf_field_common *field, bool is_frozen) | |
483 | { | |
484 | uint64_t i; | |
485 | struct bt_ctf_field_common_variant *variant_field = BT_CTF_FROM_COMMON(field); | |
486 | ||
487 | BT_LOGD("Freezing variant field object: addr=%p", field); | |
488 | ||
489 | for (i = 0; i < variant_field->fields->len; i++) { | |
490 | struct bt_ctf_field_common *var_field = | |
491 | g_ptr_array_index(variant_field->fields, i); | |
492 | ||
493 | BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64, | |
494 | var_field, i); | |
495 | bt_ctf_field_common_set_is_frozen_recursive(var_field, is_frozen); | |
496 | } | |
497 | ||
498 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); | |
499 | } | |
500 | ||
501 | BT_HIDDEN | |
502 | void bt_ctf_field_common_array_set_is_frozen_recursive( | |
503 | struct bt_ctf_field_common *field, bool is_frozen) | |
504 | { | |
505 | int64_t i; | |
506 | struct bt_ctf_field_common_array *array_field = BT_CTF_FROM_COMMON(field); | |
507 | ||
508 | BT_LOGD("Freezing array field object: addr=%p", field); | |
509 | ||
510 | for (i = 0; i < array_field->elements->len; i++) { | |
511 | struct bt_ctf_field_common *elem_field = | |
512 | g_ptr_array_index(array_field->elements, i); | |
513 | ||
514 | BT_LOGD("Freezing array field object's element field: " | |
515 | "element-field-addr=%p, index=%" PRId64, | |
516 | elem_field, i); | |
517 | bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen); | |
518 | } | |
519 | ||
520 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); | |
521 | } | |
522 | ||
523 | BT_HIDDEN | |
524 | void bt_ctf_field_common_sequence_set_is_frozen_recursive( | |
525 | struct bt_ctf_field_common *field, bool is_frozen) | |
526 | { | |
527 | int64_t i; | |
528 | struct bt_ctf_field_common_sequence *sequence_field = | |
529 | BT_CTF_FROM_COMMON(field); | |
530 | ||
531 | BT_LOGD("Freezing sequence field object: addr=%p", field); | |
532 | ||
533 | for (i = 0; i < sequence_field->length; i++) { | |
534 | struct bt_ctf_field_common *elem_field = | |
535 | g_ptr_array_index(sequence_field->elements, i); | |
536 | ||
537 | BT_LOGD("Freezing sequence field object's element field: " | |
538 | "element-field-addr=%p, index=%" PRId64, | |
539 | elem_field, i); | |
540 | bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen); | |
541 | } | |
542 | ||
543 | bt_ctf_field_common_generic_set_is_frozen(field, is_frozen); | |
544 | } | |
545 | ||
546 | BT_HIDDEN | |
547 | void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field, | |
548 | bool is_frozen) | |
549 | { | |
550 | if (!field) { | |
551 | goto end; | |
552 | } | |
553 | ||
554 | BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d", | |
555 | field, is_frozen); | |
556 | BT_ASSERT(field_type_common_has_known_id(field->type)); | |
557 | BT_ASSERT(field->methods->set_is_frozen); | |
558 | field->methods->set_is_frozen(field, is_frozen); | |
559 | ||
560 | end: | |
561 | return; | |
562 | } | |
563 | ||
564 | BT_HIDDEN | |
565 | bt_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field) | |
566 | { | |
567 | return field && field->payload_set; | |
568 | } | |
569 | ||
570 | BT_HIDDEN | |
571 | bt_bool bt_ctf_field_common_structure_is_set_recursive( | |
572 | struct bt_ctf_field_common *field) | |
573 | { | |
574 | bt_bool is_set = BT_FALSE; | |
575 | size_t i; | |
576 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); | |
577 | ||
578 | BT_ASSERT(field); | |
579 | ||
580 | for (i = 0; i < structure->fields->len; i++) { | |
581 | is_set = bt_ctf_field_common_is_set_recursive( | |
582 | structure->fields->pdata[i]); | |
583 | if (!is_set) { | |
584 | goto end; | |
585 | } | |
586 | } | |
587 | ||
588 | end: | |
589 | return is_set; | |
590 | } | |
591 | ||
592 | BT_HIDDEN | |
593 | bt_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field) | |
594 | { | |
595 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); | |
596 | bt_bool is_set = BT_FALSE; | |
597 | ||
598 | BT_ASSERT(field); | |
599 | ||
600 | if (variant->current_field) { | |
601 | is_set = bt_ctf_field_common_is_set_recursive( | |
602 | variant->current_field); | |
603 | } | |
604 | ||
605 | return is_set; | |
606 | } | |
607 | ||
608 | BT_HIDDEN | |
609 | bt_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field) | |
610 | { | |
611 | size_t i; | |
612 | bt_bool is_set = BT_FALSE; | |
613 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); | |
614 | ||
615 | BT_ASSERT(field); | |
616 | ||
617 | for (i = 0; i < array->elements->len; i++) { | |
618 | is_set = bt_ctf_field_common_is_set_recursive(array->elements->pdata[i]); | |
619 | if (!is_set) { | |
620 | goto end; | |
621 | } | |
622 | } | |
623 | ||
624 | end: | |
625 | return is_set; | |
626 | } | |
627 | ||
628 | BT_HIDDEN | |
629 | bt_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field) | |
630 | { | |
631 | size_t i; | |
632 | bt_bool is_set = BT_FALSE; | |
633 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); | |
634 | ||
635 | BT_ASSERT(field); | |
636 | ||
637 | if (!sequence->elements) { | |
638 | goto end; | |
639 | } | |
640 | ||
641 | for (i = 0; i < sequence->elements->len; i++) { | |
642 | is_set = bt_ctf_field_common_is_set_recursive( | |
643 | sequence->elements->pdata[i]); | |
644 | if (!is_set) { | |
645 | goto end; | |
646 | } | |
647 | } | |
648 | ||
649 | end: | |
650 | return is_set; | |
651 | } | |
652 | ||
653 | static struct bt_ctf_field_common_methods bt_ctf_field_integer_methods = { | |
654 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, | |
655 | .validate = bt_ctf_field_common_generic_validate, | |
8deee039 | 656 | .copy = NULL, |
086dc475 PP |
657 | .is_set = bt_ctf_field_common_generic_is_set, |
658 | .reset = bt_ctf_field_common_generic_reset, | |
8deee039 PP |
659 | }; |
660 | ||
086dc475 PP |
661 | static struct bt_ctf_field_common_methods bt_ctf_field_floating_point_methods = { |
662 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, | |
663 | .validate = bt_ctf_field_common_generic_validate, | |
8deee039 | 664 | .copy = NULL, |
086dc475 PP |
665 | .is_set = bt_ctf_field_common_generic_is_set, |
666 | .reset = bt_ctf_field_common_generic_reset, | |
8deee039 PP |
667 | }; |
668 | ||
a6918753 PP |
669 | static |
670 | void bt_ctf_field_enumeration_set_is_frozen_recursive( | |
086dc475 | 671 | struct bt_ctf_field_common *field, bool is_frozen); |
a6918753 PP |
672 | |
673 | static | |
086dc475 | 674 | int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field); |
a6918753 PP |
675 | |
676 | static | |
677 | bt_bool bt_ctf_field_enumeration_is_set_recursive( | |
086dc475 | 678 | struct bt_ctf_field_common *field); |
a6918753 PP |
679 | |
680 | static | |
086dc475 | 681 | void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field); |
a6918753 | 682 | |
086dc475 | 683 | static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods = { |
a6918753 PP |
684 | .set_is_frozen = bt_ctf_field_enumeration_set_is_frozen_recursive, |
685 | .validate = bt_ctf_field_enumeration_validate_recursive, | |
8deee039 | 686 | .copy = NULL, |
a6918753 PP |
687 | .is_set = bt_ctf_field_enumeration_is_set_recursive, |
688 | .reset = bt_ctf_field_enumeration_reset_recursive, | |
8deee039 PP |
689 | }; |
690 | ||
086dc475 PP |
691 | static struct bt_ctf_field_common_methods bt_ctf_field_string_methods = { |
692 | .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen, | |
693 | .validate = bt_ctf_field_common_generic_validate, | |
8deee039 | 694 | .copy = NULL, |
086dc475 PP |
695 | .is_set = bt_ctf_field_common_generic_is_set, |
696 | .reset = bt_ctf_field_common_generic_reset, | |
8deee039 PP |
697 | }; |
698 | ||
086dc475 PP |
699 | static struct bt_ctf_field_common_methods bt_ctf_field_structure_methods = { |
700 | .set_is_frozen = bt_ctf_field_common_structure_set_is_frozen_recursive, | |
701 | .validate = bt_ctf_field_common_structure_validate_recursive, | |
8deee039 | 702 | .copy = NULL, |
086dc475 PP |
703 | .is_set = bt_ctf_field_common_structure_is_set_recursive, |
704 | .reset = bt_ctf_field_common_structure_reset_recursive, | |
8deee039 PP |
705 | }; |
706 | ||
086dc475 PP |
707 | static struct bt_ctf_field_common_methods bt_ctf_field_sequence_methods = { |
708 | .set_is_frozen = bt_ctf_field_common_sequence_set_is_frozen_recursive, | |
709 | .validate = bt_ctf_field_common_sequence_validate_recursive, | |
8deee039 | 710 | .copy = NULL, |
086dc475 PP |
711 | .is_set = bt_ctf_field_common_sequence_is_set_recursive, |
712 | .reset = bt_ctf_field_common_sequence_reset_recursive, | |
8deee039 PP |
713 | }; |
714 | ||
086dc475 PP |
715 | static struct bt_ctf_field_common_methods bt_ctf_field_array_methods = { |
716 | .set_is_frozen = bt_ctf_field_common_array_set_is_frozen_recursive, | |
717 | .validate = bt_ctf_field_common_array_validate_recursive, | |
8deee039 | 718 | .copy = NULL, |
086dc475 PP |
719 | .is_set = bt_ctf_field_common_array_is_set_recursive, |
720 | .reset = bt_ctf_field_common_array_reset_recursive, | |
8deee039 PP |
721 | }; |
722 | ||
a6918753 | 723 | static |
086dc475 | 724 | void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common *field, |
a6918753 PP |
725 | bool is_frozen); |
726 | ||
727 | static | |
086dc475 | 728 | int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field); |
a6918753 PP |
729 | |
730 | static | |
086dc475 | 731 | bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field); |
a6918753 PP |
732 | |
733 | static | |
086dc475 | 734 | void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field); |
a6918753 | 735 | |
086dc475 | 736 | static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods = { |
a6918753 PP |
737 | .set_is_frozen = bt_ctf_field_variant_set_is_frozen_recursive, |
738 | .validate = bt_ctf_field_variant_validate_recursive, | |
8deee039 | 739 | .copy = NULL, |
a6918753 PP |
740 | .is_set = bt_ctf_field_variant_is_set_recursive, |
741 | .reset = bt_ctf_field_variant_reset_recursive, | |
8deee039 PP |
742 | }; |
743 | ||
744 | static | |
745 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); | |
746 | ||
747 | static | |
748 | struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *); | |
749 | ||
750 | static | |
751 | struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *); | |
752 | ||
753 | static | |
754 | struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *); | |
755 | ||
756 | static | |
757 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *); | |
758 | ||
759 | static | |
760 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *); | |
761 | ||
762 | static | |
763 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *); | |
764 | ||
765 | static | |
766 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); | |
767 | ||
768 | static | |
769 | struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = { | |
086dc475 PP |
770 | [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, |
771 | [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, | |
772 | [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create, | |
773 | [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, | |
774 | [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, | |
775 | [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, | |
776 | [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, | |
777 | [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, | |
8deee039 PP |
778 | }; |
779 | ||
780 | typedef int (*bt_ctf_field_serialize_recursive_func)( | |
6f4eefbe | 781 | struct bt_ctf_field_common *, struct bt_ctfser *, |
8deee039 PP |
782 | enum bt_ctf_byte_order); |
783 | ||
a6918753 PP |
784 | static |
785 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) | |
786 | { | |
787 | BT_LOGD("Destroying CTF writer integer field object: addr=%p", field); | |
086dc475 | 788 | bt_ctf_field_common_integer_finalize((void *) field); |
a6918753 PP |
789 | g_free(field); |
790 | } | |
791 | ||
792 | static | |
793 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) | |
794 | { | |
795 | BT_LOGD("Destroying CTF writer floating point field object: addr=%p", | |
796 | field); | |
086dc475 | 797 | bt_ctf_field_common_floating_point_finalize((void *) field); |
a6918753 PP |
798 | g_free(field); |
799 | } | |
800 | ||
801 | static | |
802 | void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field *field) | |
803 | { | |
086dc475 | 804 | struct bt_ctf_field_enumeration *enumeration = BT_CTF_FROM_COMMON(field); |
a6918753 PP |
805 | |
806 | BT_LOGD("Destroying CTF writer enumeration field object: addr=%p", | |
807 | field); | |
808 | BT_LOGD_STR("Putting container field."); | |
c2606e2f | 809 | bt_ctf_object_put_ref(enumeration->container); |
086dc475 | 810 | bt_ctf_field_common_finalize((void *) field); |
a6918753 PP |
811 | g_free(field); |
812 | } | |
813 | ||
814 | static | |
815 | void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field *field) | |
816 | { | |
817 | BT_LOGD("Destroying CTF writer structure field object: addr=%p", field); | |
086dc475 | 818 | bt_ctf_field_common_structure_finalize_recursive((void *) field); |
a6918753 PP |
819 | g_free(field); |
820 | } | |
821 | ||
822 | static | |
823 | void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field *field) | |
824 | { | |
086dc475 | 825 | struct bt_ctf_field_variant *variant = BT_CTF_FROM_COMMON(field); |
a6918753 PP |
826 | |
827 | BT_LOGD("Destroying CTF writer variant field object: addr=%p", field); | |
828 | BT_LOGD_STR("Putting tag field."); | |
c2606e2f | 829 | bt_ctf_object_put_ref(variant->tag); |
086dc475 | 830 | bt_ctf_field_common_variant_finalize_recursive((void *) field); |
a6918753 PP |
831 | g_free(field); |
832 | } | |
833 | ||
834 | static | |
835 | void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field *field) | |
836 | { | |
837 | BT_LOGD("Destroying CTF writer array field object: addr=%p", field); | |
086dc475 | 838 | bt_ctf_field_common_array_finalize_recursive((void *) field); |
a6918753 PP |
839 | g_free(field); |
840 | } | |
841 | ||
842 | static | |
843 | void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field *field) | |
844 | { | |
845 | BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field); | |
086dc475 | 846 | bt_ctf_field_common_sequence_finalize_recursive((void *) field); |
a6918753 PP |
847 | g_free(field); |
848 | } | |
849 | ||
850 | static | |
851 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) | |
852 | { | |
853 | BT_LOGD("Destroying CTF writer string field object: addr=%p", field); | |
086dc475 | 854 | bt_ctf_field_common_string_finalize((void *) field); |
a6918753 PP |
855 | g_free(field); |
856 | } | |
857 | ||
8deee039 PP |
858 | BT_HIDDEN |
859 | int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field, | |
6f4eefbe | 860 | struct bt_ctfser *ctfser, |
8deee039 PP |
861 | enum bt_ctf_byte_order native_byte_order) |
862 | { | |
086dc475 | 863 | struct bt_ctf_field_common *field_common = (void *) field; |
8deee039 PP |
864 | bt_ctf_field_serialize_recursive_func serialize_func; |
865 | ||
6f4eefbe | 866 | BT_ASSERT(ctfser); |
8deee039 PP |
867 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
868 | BT_ASSERT(field_common->spec.writer.serialize_func); | |
869 | serialize_func = field_common->spec.writer.serialize_func; | |
6f4eefbe | 870 | return serialize_func(field_common, ctfser, |
8deee039 PP |
871 | native_byte_order); |
872 | } | |
873 | ||
874 | static | |
6f4eefbe PP |
875 | int bt_ctf_field_integer_serialize(struct bt_ctf_field_common *field, |
876 | struct bt_ctfser *ctfser, | |
877 | enum bt_ctf_byte_order native_byte_order) | |
8deee039 PP |
878 | { |
879 | int ret; | |
6f4eefbe PP |
880 | struct bt_ctf_field_type_common_integer *int_type = |
881 | BT_CTF_FROM_COMMON(field->type); | |
882 | struct bt_ctf_field_common_integer *int_field = | |
883 | BT_CTF_FROM_COMMON(field); | |
884 | enum bt_ctf_byte_order byte_order; | |
885 | union bt_ctfser_int_val value; | |
8deee039 | 886 | |
6f4eefbe PP |
887 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Integer field"); |
888 | BT_LOGV("Serializing CTF writer integer field: addr=%p, native-bo=%s", | |
889 | field, | |
890 | bt_ctf_byte_order_string(native_byte_order)); | |
891 | byte_order = int_type->user_byte_order; | |
892 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
893 | byte_order = native_byte_order; | |
8deee039 PP |
894 | } |
895 | ||
6f4eefbe PP |
896 | value.i = int_field->payload.signd; |
897 | value.u = int_field->payload.unsignd; | |
898 | ret = bt_ctfser_write_int(ctfser, value, int_type->common.alignment, | |
899 | int_type->size, int_type->is_signed, | |
900 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? | |
901 | LITTLE_ENDIAN : BIG_ENDIAN); | |
902 | if (unlikely(ret)) { | |
903 | BT_LOGE("Cannot serialize integer field: ret=%d", ret); | |
8deee039 PP |
904 | goto end; |
905 | } | |
906 | ||
8deee039 PP |
907 | end: |
908 | return ret; | |
909 | } | |
910 | ||
911 | static | |
6f4eefbe PP |
912 | int bt_ctf_field_enumeration_serialize_recursive( |
913 | struct bt_ctf_field_common *field, struct bt_ctfser *ctfser, | |
8deee039 PP |
914 | enum bt_ctf_byte_order native_byte_order) |
915 | { | |
a6918753 | 916 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
8deee039 | 917 | |
6f4eefbe PP |
918 | BT_LOGV("Serializing enumeration field: addr=%p, native-bo=%s", |
919 | field, bt_ctf_byte_order_string(native_byte_order)); | |
8deee039 | 920 | BT_LOGV_STR("Serializing enumeration field's payload field."); |
a6918753 | 921 | return bt_ctf_field_serialize_recursive( |
6f4eefbe | 922 | (void *) enumeration->container, ctfser, native_byte_order); |
8deee039 PP |
923 | } |
924 | ||
925 | static | |
086dc475 | 926 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common *field, |
6f4eefbe | 927 | struct bt_ctfser *ctfser, |
8deee039 PP |
928 | enum bt_ctf_byte_order native_byte_order) |
929 | { | |
6f4eefbe PP |
930 | int ret = -1; |
931 | struct bt_ctf_field_type_common_floating_point *flt_type = | |
932 | BT_CTF_FROM_COMMON(field->type); | |
933 | struct bt_ctf_field_common_floating_point *flt_field = BT_CTF_FROM_COMMON(field); | |
934 | enum bt_ctf_byte_order byte_order; | |
8deee039 | 935 | |
086dc475 | 936 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field"); |
6f4eefbe PP |
937 | BT_LOGV("Serializing floating point number field: " |
938 | "addr=%p, native-bo=%s", field, | |
939 | bt_ctf_byte_order_string(native_byte_order)); | |
8deee039 | 940 | |
6f4eefbe PP |
941 | byte_order = flt_type->user_byte_order; |
942 | if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
943 | byte_order = native_byte_order; | |
944 | } | |
945 | ||
946 | if (flt_type->mant_dig == FLT_MANT_DIG) { | |
947 | ret = bt_ctfser_write_float32(ctfser, flt_field->payload, | |
948 | flt_type->common.alignment, | |
949 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? | |
950 | LITTLE_ENDIAN : BIG_ENDIAN); | |
951 | } else if (flt_type->mant_dig == DBL_MANT_DIG) { | |
952 | ret = bt_ctfser_write_float64(ctfser, flt_field->payload, | |
953 | flt_type->common.alignment, | |
954 | byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ? | |
955 | LITTLE_ENDIAN : BIG_ENDIAN); | |
956 | } else { | |
957 | abort(); | |
958 | } | |
959 | ||
960 | if (unlikely(ret)) { | |
961 | BT_LOGE("Cannot serialize floating point number field: " | |
962 | "ret=%d", ret); | |
963 | goto end; | |
8deee039 PP |
964 | } |
965 | ||
966 | end: | |
967 | return ret; | |
968 | } | |
969 | ||
970 | static | |
086dc475 | 971 | int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common *field, |
6f4eefbe | 972 | struct bt_ctfser *ctfser, |
8deee039 PP |
973 | enum bt_ctf_byte_order native_byte_order) |
974 | { | |
975 | int64_t i; | |
6f4eefbe | 976 | int ret; |
086dc475 | 977 | struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field); |
8deee039 | 978 | |
6f4eefbe PP |
979 | BT_LOGV("Serializing structure field: addr=%p, native-bo=%s", |
980 | field, bt_ctf_byte_order_string(native_byte_order)); | |
981 | ret = bt_ctfser_align_offset_in_current_packet(ctfser, | |
982 | field->type->alignment); | |
983 | if (unlikely(ret)) { | |
984 | BT_LOGE("Cannot align offset before serializing structure field: " | |
985 | "ret=%d", ret); | |
8deee039 PP |
986 | goto end; |
987 | } | |
988 | ||
989 | for (i = 0; i < structure->fields->len; i++) { | |
086dc475 | 990 | struct bt_ctf_field_common *member = g_ptr_array_index( |
8deee039 PP |
991 | structure->fields, i); |
992 | const char *field_name = NULL; | |
993 | ||
6f4eefbe PP |
994 | BT_LOGV("Serializing structure field's field: ser-offset=%" PRIu64 ", " |
995 | "field-addr=%p, index=%" PRIu64, | |
996 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), | |
997 | member, i); | |
8deee039 | 998 | |
6f4eefbe | 999 | if (unlikely(!member)) { |
086dc475 | 1000 | ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
8deee039 PP |
1001 | field->type, &field_name, NULL, i); |
1002 | BT_ASSERT(ret == 0); | |
1003 | BT_LOGW("Cannot serialize structure field's field: field is not set: " | |
1004 | "struct-field-addr=%p, " | |
1005 | "field-name=\"%s\", index=%" PRId64, | |
1006 | field, field_name, i); | |
1007 | ret = -1; | |
1008 | goto end; | |
1009 | } | |
1010 | ||
6f4eefbe | 1011 | ret = bt_ctf_field_serialize_recursive((void *) member, ctfser, |
8deee039 | 1012 | native_byte_order); |
6f4eefbe | 1013 | if (unlikely(ret)) { |
086dc475 | 1014 | ret = bt_ctf_field_type_common_structure_borrow_field_by_index( |
8deee039 PP |
1015 | field->type, &field_name, NULL, i); |
1016 | BT_ASSERT(ret == 0); | |
1017 | BT_LOGW("Cannot serialize structure field's field: " | |
1018 | "struct-field-addr=%p, field-addr=%p, " | |
1019 | "field-name=\"%s\", index=%" PRId64, | |
1020 | field->type, member, field_name, i); | |
1021 | break; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | end: | |
1026 | return ret; | |
1027 | } | |
1028 | ||
1029 | static | |
086dc475 | 1030 | int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common *field, |
6f4eefbe | 1031 | struct bt_ctfser *ctfser, |
8deee039 PP |
1032 | enum bt_ctf_byte_order native_byte_order) |
1033 | { | |
086dc475 | 1034 | struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field); |
8deee039 | 1035 | |
6f4eefbe PP |
1036 | BT_LOGV("Serializing variant field: addr=%p, native-bo=%s", |
1037 | field, bt_ctf_byte_order_string(native_byte_order)); | |
8deee039 PP |
1038 | BT_LOGV_STR("Serializing variant field's payload field."); |
1039 | return bt_ctf_field_serialize_recursive( | |
6f4eefbe | 1040 | (void *) variant->current_field, ctfser, native_byte_order); |
8deee039 PP |
1041 | } |
1042 | ||
1043 | static | |
086dc475 | 1044 | int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common *field, |
6f4eefbe | 1045 | struct bt_ctfser *ctfser, |
8deee039 PP |
1046 | enum bt_ctf_byte_order native_byte_order) |
1047 | { | |
1048 | int64_t i; | |
1049 | int ret = 0; | |
086dc475 | 1050 | struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field); |
8deee039 | 1051 | |
6f4eefbe PP |
1052 | BT_LOGV("Serializing array field: addr=%p, native-bo=%s", |
1053 | field, bt_ctf_byte_order_string(native_byte_order)); | |
8deee039 PP |
1054 | |
1055 | for (i = 0; i < array->elements->len; i++) { | |
086dc475 | 1056 | struct bt_ctf_field_common *elem_field = |
8deee039 PP |
1057 | g_ptr_array_index(array->elements, i); |
1058 | ||
1059 | BT_LOGV("Serializing array field's element field: " | |
6f4eefbe PP |
1060 | "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64, |
1061 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), | |
1062 | elem_field, i); | |
8deee039 | 1063 | ret = bt_ctf_field_serialize_recursive( |
6f4eefbe PP |
1064 | (void *) elem_field, ctfser, native_byte_order); |
1065 | if (unlikely(ret)) { | |
8deee039 PP |
1066 | BT_LOGW("Cannot serialize array field's element field: " |
1067 | "array-field-addr=%p, field-addr=%p, " | |
1068 | "index=%" PRId64, field, elem_field, i); | |
1069 | goto end; | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | end: | |
1074 | return ret; | |
1075 | } | |
1076 | ||
1077 | static | |
086dc475 | 1078 | int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common *field, |
6f4eefbe | 1079 | struct bt_ctfser *ctfser, |
8deee039 PP |
1080 | enum bt_ctf_byte_order native_byte_order) |
1081 | { | |
1082 | int64_t i; | |
1083 | int ret = 0; | |
086dc475 | 1084 | struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field); |
8deee039 | 1085 | |
6f4eefbe PP |
1086 | BT_LOGV("Serializing sequence field: addr=%p, native-bo=%s", |
1087 | field, bt_ctf_byte_order_string(native_byte_order)); | |
8deee039 PP |
1088 | |
1089 | for (i = 0; i < sequence->elements->len; i++) { | |
086dc475 | 1090 | struct bt_ctf_field_common *elem_field = |
8deee039 PP |
1091 | g_ptr_array_index(sequence->elements, i); |
1092 | ||
1093 | BT_LOGV("Serializing sequence field's element field: " | |
6f4eefbe PP |
1094 | "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64, |
1095 | bt_ctfser_get_offset_in_current_packet_bits(ctfser), | |
1096 | elem_field, i); | |
8deee039 | 1097 | ret = bt_ctf_field_serialize_recursive( |
6f4eefbe PP |
1098 | (void *) elem_field, ctfser, native_byte_order); |
1099 | if (unlikely(ret)) { | |
8deee039 PP |
1100 | BT_LOGW("Cannot serialize sequence field's element field: " |
1101 | "sequence-field-addr=%p, field-addr=%p, " | |
1102 | "index=%" PRId64, field, elem_field, i); | |
1103 | goto end; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | end: | |
1108 | return ret; | |
1109 | } | |
1110 | ||
1111 | static | |
086dc475 | 1112 | int bt_ctf_field_string_serialize(struct bt_ctf_field_common *field, |
6f4eefbe | 1113 | struct bt_ctfser *ctfser, |
8deee039 PP |
1114 | enum bt_ctf_byte_order native_byte_order) |
1115 | { | |
6f4eefbe | 1116 | int ret; |
086dc475 | 1117 | struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field); |
8deee039 | 1118 | |
086dc475 | 1119 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field"); |
6f4eefbe PP |
1120 | BT_LOGV("Serializing string field: addr=%p, native-bo=%s", |
1121 | field, bt_ctf_byte_order_string((int) native_byte_order)); | |
1122 | ret = bt_ctfser_write_string(ctfser, (const char *) string->buf->data); | |
1123 | if (unlikely(ret)) { | |
1124 | BT_LOGE("Cannot serialize string field: ret=%d", ret); | |
1125 | goto end; | |
8deee039 PP |
1126 | } |
1127 | ||
1128 | end: | |
8deee039 PP |
1129 | return ret; |
1130 | } | |
1131 | ||
1132 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) | |
1133 | { | |
1134 | struct bt_ctf_field *field = NULL; | |
1135 | enum bt_ctf_field_type_id type_id; | |
1136 | ||
1137 | BT_ASSERT_PRE_NON_NULL(type, "Field type"); | |
1138 | BT_ASSERT(field_type_common_has_known_id((void *) type)); | |
086dc475 PP |
1139 | BT_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type) == 0, |
1140 | "Field type is invalid: ft-addr=%p", type); | |
8deee039 PP |
1141 | type_id = bt_ctf_field_type_get_type_id(type); |
1142 | field = field_create_funcs[type_id](type); | |
1143 | if (!field) { | |
1144 | goto end; | |
1145 | } | |
1146 | ||
086dc475 | 1147 | bt_ctf_field_type_common_freeze((void *) type); |
8deee039 PP |
1148 | |
1149 | end: | |
1150 | return field; | |
1151 | } | |
1152 | ||
1153 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) | |
1154 | { | |
c2606e2f | 1155 | return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field)); |
8deee039 PP |
1156 | } |
1157 | ||
1158 | enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) | |
1159 | { | |
086dc475 | 1160 | struct bt_ctf_field_common *field_common = (void *) field; |
8deee039 PP |
1161 | |
1162 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
1163 | return (int) field_common->type->id; | |
1164 | } | |
1165 | ||
8deee039 PP |
1166 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, |
1167 | struct bt_ctf_field *length_field) | |
1168 | { | |
a6918753 | 1169 | int ret; |
086dc475 | 1170 | struct bt_ctf_field_common *common_length_field = (void *) length_field; |
a6918753 PP |
1171 | uint64_t length; |
1172 | ||
1173 | BT_ASSERT_PRE_NON_NULL(length_field, "Length field"); | |
086dc475 PP |
1174 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) length_field, "Length field"); |
1175 | BT_ASSERT_PRE(common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || | |
1176 | common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, | |
1177 | "Length field must be an integer or enumeration field: field-addr=%p", | |
a6918753 PP |
1178 | length_field); |
1179 | ||
086dc475 | 1180 | if (common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM) { |
a6918753 PP |
1181 | struct bt_ctf_field_enumeration *enumeration = (void *) |
1182 | length_field; | |
1183 | ||
1184 | length_field = (void *) enumeration->container; | |
1185 | } | |
1186 | ||
1187 | ret = bt_ctf_field_integer_unsigned_get_value(length_field, &length); | |
1188 | BT_ASSERT(ret == 0); | |
086dc475 PP |
1189 | return bt_ctf_field_common_sequence_set_length((void *) field, |
1190 | length, (bt_ctf_field_common_create_func) bt_ctf_field_create); | |
8deee039 PP |
1191 | } |
1192 | ||
1193 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( | |
1194 | struct bt_ctf_field *field, uint64_t index) | |
1195 | { | |
c2606e2f | 1196 | return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index( |
5fe68922 | 1197 | (void *) field, index)); |
8deee039 PP |
1198 | } |
1199 | ||
1200 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( | |
1201 | struct bt_ctf_field *field, const char *name) | |
1202 | { | |
c2606e2f | 1203 | return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name( |
5fe68922 | 1204 | (void *) field, name)); |
8deee039 PP |
1205 | } |
1206 | ||
8deee039 PP |
1207 | struct bt_ctf_field *bt_ctf_field_array_get_field( |
1208 | struct bt_ctf_field *field, uint64_t index) | |
1209 | { | |
c2606e2f | 1210 | return bt_ctf_object_get_ref( |
086dc475 | 1211 | bt_ctf_field_common_array_borrow_field((void *) field, index)); |
8deee039 PP |
1212 | } |
1213 | ||
1214 | struct bt_ctf_field *bt_ctf_field_sequence_get_field( | |
1215 | struct bt_ctf_field *field, uint64_t index) | |
1216 | { | |
c2606e2f | 1217 | return bt_ctf_object_get_ref( |
086dc475 | 1218 | bt_ctf_field_common_sequence_borrow_field((void *) field, index)); |
8deee039 PP |
1219 | } |
1220 | ||
1221 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, | |
1222 | struct bt_ctf_field *tag_field) | |
1223 | { | |
a6918753 PP |
1224 | struct bt_ctf_field_variant *variant_field = (void *) field; |
1225 | struct bt_ctf_field_enumeration *enum_field = (void *) tag_field; | |
086dc475 PP |
1226 | struct bt_ctf_field_type_common_variant *variant_ft; |
1227 | struct bt_ctf_field_type_common_enumeration *tag_ft; | |
a6918753 PP |
1228 | struct bt_ctf_field *current_field = NULL; |
1229 | bt_bool is_signed; | |
1230 | uint64_t tag_uval; | |
1231 | int ret; | |
1232 | ||
1233 | BT_ASSERT_PRE_NON_NULL(field, "Variant field"); | |
1234 | BT_ASSERT_PRE_NON_NULL(tag_field, "Tag field"); | |
086dc475 PP |
1235 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) tag_field, "Tag field"); |
1236 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID( | |
1237 | (struct bt_ctf_field_common *) tag_field, | |
1238 | BT_CTF_FIELD_TYPE_ID_ENUM, "Tag field"); | |
1239 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID( | |
1240 | (struct bt_ctf_field_common *) field, | |
1241 | BT_CTF_FIELD_TYPE_ID_VARIANT, "Field"); | |
a6918753 | 1242 | BT_ASSERT_PRE( |
086dc475 PP |
1243 | bt_ctf_field_common_validate_recursive((void *) tag_field) == 0, |
1244 | "Tag field is invalid: field-addr=%p", tag_field); | |
1245 | variant_ft = BT_CTF_FROM_COMMON(variant_field->common.common.type); | |
1246 | BT_ASSERT_PRE(bt_ctf_field_type_common_compare( | |
1247 | BT_CTF_TO_COMMON(variant_ft->tag_ft), enum_field->common.type) == 0, | |
1248 | "Unexpected tag field's type: expected-ft-addr=%p, " | |
1249 | "tag-ft-addr=%p", variant_ft->tag_ft, | |
a6918753 | 1250 | enum_field->common.type); |
086dc475 | 1251 | tag_ft = BT_CTF_FROM_COMMON(enum_field->common.type); |
a6918753 PP |
1252 | is_signed = tag_ft->container_ft->is_signed; |
1253 | ||
1254 | if (is_signed) { | |
1255 | int64_t tag_ival; | |
1256 | ||
1257 | ret = bt_ctf_field_integer_signed_get_value( | |
1258 | (void *) enum_field->container, &tag_ival); | |
1259 | tag_uval = (uint64_t) tag_ival; | |
1260 | } else { | |
1261 | ret = bt_ctf_field_integer_unsigned_get_value( | |
1262 | (void *) enum_field->container, &tag_uval); | |
1263 | } | |
1264 | ||
1265 | BT_ASSERT(ret == 0); | |
086dc475 | 1266 | ret = bt_ctf_field_common_variant_set_tag((void *) field, tag_uval, |
a6918753 PP |
1267 | is_signed); |
1268 | if (ret) { | |
1269 | goto end; | |
1270 | } | |
1271 | ||
c2606e2f PP |
1272 | bt_ctf_object_put_ref(variant_field->tag); |
1273 | variant_field->tag = bt_ctf_object_get_ref(tag_field); | |
a6918753 PP |
1274 | current_field = bt_ctf_field_variant_get_current_field(field); |
1275 | BT_ASSERT(current_field); | |
1276 | ||
1277 | end: | |
1278 | return current_field; | |
8deee039 PP |
1279 | } |
1280 | ||
1281 | struct bt_ctf_field *bt_ctf_field_variant_get_current_field( | |
1282 | struct bt_ctf_field *variant_field) | |
1283 | { | |
c2606e2f | 1284 | return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field( |
5fe68922 | 1285 | (void *) variant_field)); |
8deee039 PP |
1286 | } |
1287 | ||
a6918753 PP |
1288 | BT_HIDDEN |
1289 | struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container( | |
1290 | struct bt_ctf_field *field) | |
8deee039 | 1291 | { |
a6918753 PP |
1292 | struct bt_ctf_field_enumeration *enumeration = (void *) field; |
1293 | ||
1294 | BT_ASSERT_PRE_NON_NULL(field, "Enumeration field"); | |
086dc475 | 1295 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID((struct bt_ctf_field_common *) field, |
a6918753 PP |
1296 | BT_CTF_FIELD_TYPE_ID_ENUM, "Field"); |
1297 | BT_ASSERT(enumeration->container); | |
1298 | return (void *) enumeration->container; | |
8deee039 PP |
1299 | } |
1300 | ||
a6918753 PP |
1301 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( |
1302 | struct bt_ctf_field *field) | |
8deee039 | 1303 | { |
c2606e2f | 1304 | return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field)); |
8deee039 PP |
1305 | } |
1306 | ||
a6918753 PP |
1307 | int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field, |
1308 | int64_t *value) | |
8deee039 | 1309 | { |
086dc475 | 1310 | struct bt_ctf_field_common_integer *integer = (void *) field; |
a6918753 PP |
1311 | |
1312 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); | |
1313 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
086dc475 PP |
1314 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field"); |
1315 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), | |
1316 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); | |
1317 | BT_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed( | |
a6918753 | 1318 | integer->common.type), |
086dc475 | 1319 | "Field's type is unsigned: field-addr=%p", field); |
a6918753 PP |
1320 | *value = integer->payload.signd; |
1321 | return 0; | |
8deee039 PP |
1322 | } |
1323 | ||
1324 | int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field, | |
1325 | int64_t value) | |
1326 | { | |
a6918753 | 1327 | int ret = 0; |
086dc475 PP |
1328 | struct bt_ctf_field_common_integer *integer = (void *) field; |
1329 | struct bt_ctf_field_type_common_integer *integer_type; | |
a6918753 PP |
1330 | |
1331 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); | |
086dc475 PP |
1332 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field"); |
1333 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), | |
1334 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); | |
1335 | integer_type = BT_CTF_FROM_COMMON(integer->common.type); | |
a6918753 | 1336 | BT_ASSERT_PRE( |
086dc475 PP |
1337 | bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
1338 | "Field's type is unsigned: field-addr=%p", field); | |
a6918753 | 1339 | BT_ASSERT_PRE(value_is_in_range_signed(integer_type->size, value), |
086dc475 | 1340 | "Value is out of bounds: value=%" PRId64 ", field-addr=%p", |
a6918753 PP |
1341 | value, field); |
1342 | integer->payload.signd = value; | |
086dc475 | 1343 | bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true); |
a6918753 | 1344 | return ret; |
8deee039 PP |
1345 | } |
1346 | ||
1347 | int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field, | |
1348 | uint64_t *value) | |
1349 | { | |
086dc475 | 1350 | struct bt_ctf_field_common_integer *integer = (void *) field; |
a6918753 PP |
1351 | |
1352 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); | |
1353 | BT_ASSERT_PRE_NON_NULL(value, "Value"); | |
086dc475 PP |
1354 | BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field"); |
1355 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), | |
1356 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); | |
a6918753 | 1357 | BT_ASSERT_PRE( |
086dc475 PP |
1358 | !bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
1359 | "Field's type is signed: field-addr=%p", field); | |
a6918753 PP |
1360 | *value = integer->payload.unsignd; |
1361 | return 0; | |
8deee039 PP |
1362 | } |
1363 | ||
a6918753 PP |
1364 | int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field, |
1365 | uint64_t value) | |
8deee039 | 1366 | { |
086dc475 PP |
1367 | struct bt_ctf_field_common_integer *integer = (void *) field; |
1368 | struct bt_ctf_field_type_common_integer *integer_type; | |
a6918753 PP |
1369 | |
1370 | BT_ASSERT_PRE_NON_NULL(field, "Integer field"); | |
086dc475 PP |
1371 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field"); |
1372 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer), | |
1373 | BT_CTF_FIELD_TYPE_ID_INTEGER, "Field"); | |
1374 | integer_type = BT_CTF_FROM_COMMON(integer->common.type); | |
a6918753 | 1375 | BT_ASSERT_PRE( |
086dc475 PP |
1376 | !bt_ctf_field_type_common_integer_is_signed(integer->common.type), |
1377 | "Field's type is signed: field-addr=%p", field); | |
a6918753 | 1378 | BT_ASSERT_PRE(value_is_in_range_unsigned(integer_type->size, value), |
086dc475 | 1379 | "Value is out of bounds: value=%" PRIu64 ", field-addr=%p", |
a6918753 PP |
1380 | value, field); |
1381 | integer->payload.unsignd = value; | |
086dc475 | 1382 | bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true); |
a6918753 | 1383 | return 0; |
8deee039 PP |
1384 | } |
1385 | ||
1386 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, | |
1387 | double *value) | |
1388 | { | |
086dc475 | 1389 | return bt_ctf_field_common_floating_point_get_value((void *) field, value); |
8deee039 PP |
1390 | } |
1391 | ||
1392 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, | |
1393 | double value) | |
1394 | { | |
086dc475 | 1395 | return bt_ctf_field_common_floating_point_set_value((void *) field, value); |
8deee039 PP |
1396 | } |
1397 | ||
1398 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) | |
1399 | { | |
086dc475 | 1400 | return bt_ctf_field_common_string_get_value((void *) field); |
8deee039 PP |
1401 | } |
1402 | ||
1403 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value) | |
1404 | { | |
086dc475 | 1405 | return bt_ctf_field_common_string_set_value((void *) field, value); |
8deee039 PP |
1406 | } |
1407 | ||
1408 | int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value) | |
1409 | { | |
086dc475 | 1410 | return bt_ctf_field_common_string_append((void *) field, value); |
8deee039 PP |
1411 | } |
1412 | ||
1413 | int bt_ctf_field_string_append_len(struct bt_ctf_field *field, | |
1414 | const char *value, unsigned int length) | |
1415 | { | |
086dc475 | 1416 | return bt_ctf_field_common_string_append_len((void *) field, value, length); |
8deee039 PP |
1417 | } |
1418 | ||
1419 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) | |
1420 | { | |
086dc475 | 1421 | return (void *) bt_ctf_field_common_copy((void *) field); |
8deee039 PP |
1422 | } |
1423 | ||
1424 | static | |
1425 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) | |
1426 | { | |
086dc475 PP |
1427 | struct bt_ctf_field_common_integer *integer = |
1428 | g_new0(struct bt_ctf_field_common_integer, 1); | |
8deee039 PP |
1429 | |
1430 | BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type); | |
1431 | ||
1432 | if (integer) { | |
086dc475 | 1433 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer), (void *) type, |
1d7bf349 | 1434 | true, |
c2606e2f | 1435 | (bt_ctf_object_release_func) bt_ctf_field_integer_destroy, |
8deee039 PP |
1436 | &bt_ctf_field_integer_methods); |
1437 | integer->common.spec.writer.serialize_func = | |
1438 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize; | |
1439 | BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p", | |
1440 | integer, type); | |
1441 | } else { | |
1442 | BT_LOGE_STR("Failed to allocate one integer field."); | |
1443 | } | |
1444 | ||
1445 | return (void *) integer; | |
1446 | } | |
1447 | ||
1448 | static | |
1449 | struct bt_ctf_field *bt_ctf_field_enumeration_create( | |
1450 | struct bt_ctf_field_type *type) | |
1451 | { | |
086dc475 | 1452 | struct bt_ctf_field_type_common_enumeration *enum_ft = (void *) type; |
a6918753 PP |
1453 | struct bt_ctf_field_enumeration *enumeration = g_new0( |
1454 | struct bt_ctf_field_enumeration, 1); | |
8deee039 PP |
1455 | |
1456 | BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type); | |
1457 | ||
a6918753 | 1458 | if (!enumeration) { |
8deee039 | 1459 | BT_LOGE_STR("Failed to allocate one enumeration field."); |
a6918753 | 1460 | goto end; |
8deee039 PP |
1461 | } |
1462 | ||
086dc475 | 1463 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration), |
a6918753 | 1464 | (void *) type, |
c2606e2f | 1465 | true, (bt_ctf_object_release_func) |
a6918753 PP |
1466 | bt_ctf_field_enumeration_destroy_recursive, |
1467 | &bt_ctf_field_enumeration_methods); | |
1468 | enumeration->container = (void *) bt_ctf_field_create( | |
086dc475 | 1469 | BT_CTF_FROM_COMMON(enum_ft->container_ft)); |
a6918753 | 1470 | if (!enumeration->container) { |
c2606e2f | 1471 | BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration); |
a6918753 PP |
1472 | goto end; |
1473 | } | |
1474 | ||
1475 | enumeration->common.spec.writer.serialize_func = | |
1476 | (bt_ctf_field_serialize_recursive_func) | |
1477 | bt_ctf_field_enumeration_serialize_recursive; | |
1478 | BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p", | |
1479 | enumeration, type); | |
1480 | ||
1481 | end: | |
8deee039 PP |
1482 | return (void *) enumeration; |
1483 | } | |
1484 | ||
1485 | static | |
1486 | struct bt_ctf_field *bt_ctf_field_floating_point_create( | |
1487 | struct bt_ctf_field_type *type) | |
1488 | { | |
086dc475 | 1489 | struct bt_ctf_field_common_floating_point *floating_point; |
8deee039 PP |
1490 | |
1491 | BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type); | |
086dc475 | 1492 | floating_point = g_new0(struct bt_ctf_field_common_floating_point, 1); |
8deee039 PP |
1493 | |
1494 | if (floating_point) { | |
086dc475 | 1495 | bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point), |
8deee039 | 1496 | (void *) type, |
c2606e2f | 1497 | true, (bt_ctf_object_release_func) |
a6918753 | 1498 | bt_ctf_field_floating_point_destroy, |
8deee039 PP |
1499 | &bt_ctf_field_floating_point_methods); |
1500 | floating_point->common.spec.writer.serialize_func = | |
1501 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize; | |
1502 | BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p", | |
1503 | floating_point, type); | |
1504 | } else { | |
1505 | BT_LOGE_STR("Failed to allocate one floating point number field."); | |
1506 | } | |
1507 | ||
1508 | return (void *) floating_point; | |
1509 | } | |
1510 | ||
1511 | static | |
1512 | struct bt_ctf_field *bt_ctf_field_structure_create( | |
1513 | struct bt_ctf_field_type *type) | |
1514 | { | |
086dc475 PP |
1515 | struct bt_ctf_field_common_structure *structure = g_new0( |
1516 | struct bt_ctf_field_common_structure, 1); | |
8deee039 PP |
1517 | int iret; |
1518 | ||
1519 | BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type); | |
1520 | ||
1521 | if (!structure) { | |
1522 | BT_LOGE_STR("Failed to allocate one structure field."); | |
1523 | goto end; | |
1524 | } | |
1525 | ||
086dc475 | 1526 | iret = bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure), |
a6918753 | 1527 | (void *) type, |
c2606e2f | 1528 | true, (bt_ctf_object_release_func) |
a6918753 | 1529 | bt_ctf_field_structure_destroy_recursive, |
8deee039 | 1530 | &bt_ctf_field_structure_methods, |
086dc475 | 1531 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
c2606e2f | 1532 | (GDestroyNotify) bt_ctf_object_put_ref); |
8deee039 PP |
1533 | structure->common.spec.writer.serialize_func = |
1534 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive; | |
1535 | if (iret) { | |
c2606e2f | 1536 | BT_CTF_OBJECT_PUT_REF_AND_RESET(structure); |
8deee039 PP |
1537 | goto end; |
1538 | } | |
1539 | ||
1540 | BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p", | |
1541 | structure, type); | |
1542 | ||
1543 | end: | |
1544 | return (void *) structure; | |
1545 | } | |
1546 | ||
1547 | static | |
1548 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) | |
1549 | { | |
086dc475 | 1550 | struct bt_ctf_field_type_common_variant *var_ft = (void *) type; |
a6918753 PP |
1551 | struct bt_ctf_field_variant *variant = g_new0( |
1552 | struct bt_ctf_field_variant, 1); | |
8deee039 PP |
1553 | |
1554 | BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type); | |
1555 | ||
a6918753 | 1556 | if (!variant) { |
8deee039 | 1557 | BT_LOGE_STR("Failed to allocate one variant field."); |
a6918753 | 1558 | goto end; |
8deee039 PP |
1559 | } |
1560 | ||
086dc475 | 1561 | bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant)), |
a6918753 | 1562 | (void *) type, |
c2606e2f | 1563 | true, (bt_ctf_object_release_func) |
a6918753 PP |
1564 | bt_ctf_field_variant_destroy_recursive, |
1565 | &bt_ctf_field_variant_methods, | |
086dc475 | 1566 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
c2606e2f | 1567 | (GDestroyNotify) bt_ctf_object_put_ref); |
a6918753 | 1568 | variant->tag = (void *) bt_ctf_field_create( |
086dc475 | 1569 | BT_CTF_FROM_COMMON(var_ft->tag_ft)); |
a6918753 PP |
1570 | variant->common.common.spec.writer.serialize_func = |
1571 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive; | |
1572 | BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p", | |
1573 | variant, type); | |
1574 | ||
1575 | end: | |
8deee039 PP |
1576 | return (void *) variant; |
1577 | } | |
1578 | ||
1579 | static | |
1580 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) | |
1581 | { | |
086dc475 PP |
1582 | struct bt_ctf_field_common_array *array = |
1583 | g_new0(struct bt_ctf_field_common_array, 1); | |
8deee039 PP |
1584 | int ret; |
1585 | ||
1586 | BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type); | |
1587 | BT_ASSERT(type); | |
1588 | ||
1589 | if (!array) { | |
1590 | BT_LOGE_STR("Failed to allocate one array field."); | |
1591 | goto end; | |
1592 | } | |
1593 | ||
086dc475 | 1594 | ret = bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array), |
8deee039 | 1595 | (void *) type, |
c2606e2f | 1596 | true, (bt_ctf_object_release_func) |
a6918753 PP |
1597 | bt_ctf_field_array_destroy_recursive, |
1598 | &bt_ctf_field_array_methods, | |
086dc475 | 1599 | (bt_ctf_field_common_create_func) bt_ctf_field_create, |
c2606e2f | 1600 | (GDestroyNotify) bt_ctf_object_put_ref); |
8deee039 PP |
1601 | array->common.spec.writer.serialize_func = |
1602 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive; | |
1603 | if (ret) { | |
c2606e2f | 1604 | BT_CTF_OBJECT_PUT_REF_AND_RESET(array); |
8deee039 PP |
1605 | goto end; |
1606 | } | |
1607 | ||
1608 | BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p", | |
1609 | array, type); | |
1610 | ||
1611 | end: | |
1612 | return (void *) array; | |
1613 | } | |
1614 | ||
1615 | static | |
1616 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type) | |
1617 | { | |
086dc475 PP |
1618 | struct bt_ctf_field_common_sequence *sequence = g_new0( |
1619 | struct bt_ctf_field_common_sequence, 1); | |
8deee039 PP |
1620 | |
1621 | BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type); | |
1622 | ||
1623 | if (sequence) { | |
086dc475 | 1624 | bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence), |
8deee039 | 1625 | (void *) type, |
c2606e2f | 1626 | true, (bt_ctf_object_release_func) |
a6918753 PP |
1627 | bt_ctf_field_sequence_destroy_recursive, |
1628 | &bt_ctf_field_sequence_methods, | |
c2606e2f | 1629 | (GDestroyNotify) bt_ctf_object_put_ref); |
8deee039 PP |
1630 | sequence->common.spec.writer.serialize_func = |
1631 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive; | |
1632 | BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p", | |
1633 | sequence, type); | |
1634 | } else { | |
1635 | BT_LOGE_STR("Failed to allocate one sequence field."); | |
1636 | } | |
1637 | ||
1638 | return (void *) sequence; | |
1639 | } | |
1640 | ||
1641 | static | |
1642 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) | |
1643 | { | |
086dc475 PP |
1644 | struct bt_ctf_field_common_string *string = g_new0( |
1645 | struct bt_ctf_field_common_string, 1); | |
8deee039 PP |
1646 | |
1647 | BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type); | |
1648 | ||
1649 | if (string) { | |
086dc475 | 1650 | bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string), |
8deee039 | 1651 | (void *) type, |
c2606e2f | 1652 | true, (bt_ctf_object_release_func) |
a6918753 | 1653 | bt_ctf_field_string_destroy, |
8deee039 PP |
1654 | &bt_ctf_field_string_methods); |
1655 | string->common.spec.writer.serialize_func = | |
1656 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize; | |
1657 | BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p", | |
1658 | string, type); | |
1659 | } else { | |
1660 | BT_LOGE_STR("Failed to allocate one string field."); | |
1661 | } | |
1662 | ||
1663 | return (void *) string; | |
1664 | } | |
a6918753 PP |
1665 | |
1666 | static | |
1667 | void bt_ctf_field_enumeration_set_is_frozen_recursive( | |
086dc475 | 1668 | struct bt_ctf_field_common *field, bool is_frozen) |
a6918753 PP |
1669 | { |
1670 | struct bt_ctf_field_enumeration *enumeration = (void *) field; | |
1671 | ||
1672 | if (enumeration->container) { | |
086dc475 | 1673 | bt_ctf_field_common_set_is_frozen_recursive( |
a6918753 PP |
1674 | (void *) enumeration->container, is_frozen); |
1675 | } | |
1676 | ||
086dc475 | 1677 | bt_ctf_field_common_generic_set_is_frozen((void *) field, is_frozen); |
a6918753 PP |
1678 | } |
1679 | ||
1680 | static | |
086dc475 | 1681 | int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1682 | { |
1683 | int ret = -1; | |
1684 | struct bt_ctf_field_enumeration *enumeration = (void *) field; | |
1685 | ||
1686 | if (enumeration->container) { | |
086dc475 | 1687 | ret = bt_ctf_field_common_validate_recursive( |
a6918753 PP |
1688 | (void *) enumeration->container); |
1689 | } | |
1690 | ||
1691 | return ret; | |
1692 | } | |
1693 | ||
1694 | static | |
086dc475 | 1695 | bt_bool bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1696 | { |
1697 | bt_bool is_set = BT_FALSE; | |
1698 | struct bt_ctf_field_enumeration *enumeration = (void *) field; | |
1699 | ||
1700 | if (enumeration->container) { | |
086dc475 | 1701 | is_set = bt_ctf_field_common_is_set_recursive( |
a6918753 PP |
1702 | (void *) enumeration->container); |
1703 | } | |
1704 | ||
1705 | return is_set; | |
1706 | } | |
1707 | ||
1708 | static | |
086dc475 | 1709 | void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1710 | { |
1711 | struct bt_ctf_field_enumeration *enumeration = (void *) field; | |
1712 | ||
1713 | if (enumeration->container) { | |
086dc475 | 1714 | bt_ctf_field_common_reset_recursive( |
a6918753 PP |
1715 | (void *) enumeration->container); |
1716 | } | |
1717 | ||
086dc475 | 1718 | bt_ctf_field_common_generic_reset((void *) field); |
a6918753 PP |
1719 | } |
1720 | ||
1721 | static | |
1722 | void bt_ctf_field_variant_set_is_frozen_recursive( | |
086dc475 | 1723 | struct bt_ctf_field_common *field, bool is_frozen) |
a6918753 PP |
1724 | { |
1725 | struct bt_ctf_field_variant *variant = (void *) field; | |
1726 | ||
1727 | if (variant->tag) { | |
086dc475 | 1728 | bt_ctf_field_common_set_is_frozen_recursive( |
a6918753 PP |
1729 | (void *) variant->tag, is_frozen); |
1730 | } | |
1731 | ||
086dc475 | 1732 | bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field, |
a6918753 PP |
1733 | is_frozen); |
1734 | } | |
1735 | ||
1736 | static | |
086dc475 | 1737 | int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1738 | { |
1739 | int ret; | |
1740 | struct bt_ctf_field_variant *variant = (void *) field; | |
1741 | ||
1742 | if (variant->tag) { | |
086dc475 | 1743 | ret = bt_ctf_field_common_validate_recursive( |
a6918753 PP |
1744 | (void *) variant->tag); |
1745 | if (ret) { | |
1746 | goto end; | |
1747 | } | |
1748 | } | |
1749 | ||
086dc475 | 1750 | ret = bt_ctf_field_common_variant_validate_recursive((void *) field); |
a6918753 PP |
1751 | |
1752 | end: | |
1753 | return ret; | |
1754 | } | |
1755 | ||
1756 | static | |
086dc475 | 1757 | bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1758 | { |
1759 | bt_bool is_set; | |
1760 | struct bt_ctf_field_variant *variant = (void *) field; | |
1761 | ||
1762 | if (variant->tag) { | |
086dc475 | 1763 | is_set = bt_ctf_field_common_is_set_recursive( |
a6918753 PP |
1764 | (void *) variant->tag); |
1765 | if (is_set) { | |
1766 | goto end; | |
1767 | } | |
1768 | } | |
1769 | ||
086dc475 | 1770 | is_set = bt_ctf_field_common_variant_is_set_recursive((void *) field); |
a6918753 PP |
1771 | |
1772 | end: | |
1773 | return is_set; | |
1774 | } | |
1775 | ||
1776 | static | |
086dc475 | 1777 | void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field) |
a6918753 PP |
1778 | { |
1779 | struct bt_ctf_field_variant *variant = (void *) field; | |
1780 | ||
1781 | if (variant->tag) { | |
086dc475 | 1782 | bt_ctf_field_common_reset_recursive( |
a6918753 PP |
1783 | (void *) variant->tag); |
1784 | } | |
1785 | ||
086dc475 | 1786 | bt_ctf_field_common_variant_reset_recursive((void *) field); |
a6918753 PP |
1787 | } |
1788 | ||
1789 | BT_ASSERT_PRE_FUNC | |
1790 | static inline bool field_to_set_has_expected_type( | |
086dc475 PP |
1791 | struct bt_ctf_field_common *struct_field, |
1792 | const char *name, struct bt_ctf_field_common *value) | |
a6918753 PP |
1793 | { |
1794 | bool ret = true; | |
086dc475 | 1795 | struct bt_ctf_field_type_common *expected_field_type = NULL; |
a6918753 PP |
1796 | |
1797 | expected_field_type = | |
086dc475 | 1798 | bt_ctf_field_type_common_structure_borrow_field_type_by_name( |
a6918753 PP |
1799 | struct_field->type, name); |
1800 | ||
086dc475 | 1801 | if (bt_ctf_field_type_common_compare(expected_field_type, value->type)) { |
a6918753 | 1802 | BT_ASSERT_PRE_MSG("Value field's type is different from the expected field type: " |
086dc475 | 1803 | "value-ft-addr=%p, expected-ft-addr=%p", value->type, |
a6918753 PP |
1804 | expected_field_type); |
1805 | ret = false; | |
1806 | goto end; | |
1807 | } | |
1808 | ||
1809 | end: | |
1810 | return ret; | |
1811 | } | |
1812 | ||
1813 | BT_HIDDEN | |
1814 | int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field, | |
1815 | const char *name, struct bt_ctf_field *value) | |
1816 | { | |
1817 | int ret = 0; | |
1818 | GQuark field_quark; | |
086dc475 PP |
1819 | struct bt_ctf_field_common *common_field = (void *) field; |
1820 | struct bt_ctf_field_common_structure *structure = | |
1821 | BT_CTF_FROM_COMMON(common_field); | |
1822 | struct bt_ctf_field_common *common_value = (void *) value; | |
a6918753 PP |
1823 | size_t index; |
1824 | GHashTable *field_name_to_index; | |
086dc475 | 1825 | struct bt_ctf_field_type_common_structure *structure_ft; |
a6918753 PP |
1826 | |
1827 | BT_ASSERT_PRE_NON_NULL(field, "Parent field"); | |
1828 | BT_ASSERT_PRE_NON_NULL(name, "Field name"); | |
1829 | BT_ASSERT_PRE_NON_NULL(value, "Value field"); | |
086dc475 PP |
1830 | BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(common_field, |
1831 | BT_CTF_FIELD_TYPE_ID_STRUCT, "Parent field"); | |
a6918753 PP |
1832 | BT_ASSERT_PRE(field_to_set_has_expected_type(common_field, |
1833 | name, common_value), | |
1834 | "Value field's type is different from the expected field type."); | |
1835 | field_quark = g_quark_from_string(name); | |
086dc475 | 1836 | structure_ft = BT_CTF_FROM_COMMON(common_field->type); |
a6918753 PP |
1837 | field_name_to_index = structure_ft->field_name_to_index; |
1838 | if (!g_hash_table_lookup_extended(field_name_to_index, | |
1839 | GUINT_TO_POINTER(field_quark), NULL, | |
1840 | (gpointer *) &index)) { | |
1841 | BT_LOGV("Invalid parameter: no such field in structure field's type: " | |
1842 | "struct-field-addr=%p, struct-ft-addr=%p, " | |
1843 | "field-ft-addr=%p, name=\"%s\"", | |
1844 | field, common_field->type, common_value->type, name); | |
1845 | ret = -1; | |
1846 | goto end; | |
1847 | } | |
c2606e2f PP |
1848 | bt_ctf_object_get_ref(value); |
1849 | BT_CTF_OBJECT_MOVE_REF(structure->fields->pdata[index], value); | |
a6918753 PP |
1850 | |
1851 | end: | |
1852 | return ret; | |
1853 | } |