Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * event-fields.c | |
3 | * | |
4 | * Babeltrace CTF Writer | |
5 | * | |
6 | * Copyright 2013 EfficiOS Inc. | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/ctf-writer/event-fields.h> | |
30 | #include <babeltrace/ctf-writer/event-fields-internal.h> | |
31 | #include <babeltrace/ctf-writer/event-types-internal.h> | |
32 | #include <babeltrace/compiler.h> | |
33 | ||
34 | #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT) | |
35 | ||
36 | static | |
37 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); | |
38 | static | |
39 | struct bt_ctf_field *bt_ctf_field_enumeration_create( | |
40 | struct bt_ctf_field_type *); | |
41 | static | |
42 | struct bt_ctf_field *bt_ctf_field_floating_point_create( | |
43 | struct bt_ctf_field_type *); | |
44 | static | |
45 | struct bt_ctf_field *bt_ctf_field_structure_create( | |
46 | struct bt_ctf_field_type *); | |
47 | static | |
48 | struct bt_ctf_field *bt_ctf_field_variant_create( | |
49 | struct bt_ctf_field_type *); | |
50 | static | |
51 | struct bt_ctf_field *bt_ctf_field_array_create( | |
52 | struct bt_ctf_field_type *); | |
53 | static | |
54 | struct bt_ctf_field *bt_ctf_field_sequence_create( | |
55 | struct bt_ctf_field_type *); | |
56 | static | |
57 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); | |
58 | ||
59 | static | |
60 | void bt_ctf_field_destroy(struct bt_ctf_ref *); | |
61 | static | |
62 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *); | |
63 | static | |
64 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *); | |
65 | static | |
66 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *); | |
67 | static | |
68 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *); | |
69 | static | |
70 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *); | |
71 | static | |
72 | void bt_ctf_field_array_destroy(struct bt_ctf_field *); | |
73 | static | |
74 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *); | |
75 | static | |
76 | void bt_ctf_field_string_destroy(struct bt_ctf_field *); | |
77 | ||
78 | static | |
79 | int bt_ctf_field_generic_validate(struct bt_ctf_field *field); | |
80 | static | |
81 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field); | |
82 | static | |
83 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field); | |
84 | static | |
85 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field); | |
86 | static | |
87 | int bt_ctf_field_array_validate(struct bt_ctf_field *field); | |
88 | static | |
89 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field); | |
90 | ||
91 | static | |
92 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *, | |
93 | struct ctf_stream_pos *); | |
94 | static | |
95 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *, | |
96 | struct ctf_stream_pos *); | |
97 | static | |
98 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *, | |
99 | struct ctf_stream_pos *); | |
100 | static | |
101 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *, | |
102 | struct ctf_stream_pos *); | |
103 | static | |
104 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *, | |
105 | struct ctf_stream_pos *); | |
106 | static | |
107 | int bt_ctf_field_array_serialize(struct bt_ctf_field *, | |
108 | struct ctf_stream_pos *); | |
109 | static | |
110 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *, | |
111 | struct ctf_stream_pos *); | |
112 | static | |
113 | int bt_ctf_field_string_serialize(struct bt_ctf_field *, | |
114 | struct ctf_stream_pos *); | |
115 | ||
116 | static | |
117 | int increase_packet_size(struct ctf_stream_pos *pos); | |
118 | ||
119 | static | |
120 | struct bt_ctf_field *(*field_create_funcs[])( | |
121 | struct bt_ctf_field_type *) = { | |
122 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create, | |
123 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create, | |
124 | [CTF_TYPE_FLOAT] = | |
125 | bt_ctf_field_floating_point_create, | |
126 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create, | |
127 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create, | |
128 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_create, | |
129 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create, | |
130 | [CTF_TYPE_STRING] = bt_ctf_field_string_create, | |
131 | }; | |
132 | ||
133 | static | |
134 | void (*field_destroy_funcs[])(struct bt_ctf_field *) = { | |
135 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy, | |
136 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy, | |
137 | [CTF_TYPE_FLOAT] = | |
138 | bt_ctf_field_floating_point_destroy, | |
139 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy, | |
140 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy, | |
141 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy, | |
142 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy, | |
143 | [CTF_TYPE_STRING] = bt_ctf_field_string_destroy, | |
144 | }; | |
145 | ||
146 | static | |
147 | int (*field_validate_funcs[])(struct bt_ctf_field *) = { | |
148 | [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate, | |
149 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate, | |
150 | [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate, | |
151 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate, | |
152 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate, | |
153 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate, | |
154 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate, | |
155 | [CTF_TYPE_STRING] = bt_ctf_field_generic_validate, | |
156 | }; | |
157 | ||
158 | static | |
159 | int (*field_serialize_funcs[])(struct bt_ctf_field *, | |
160 | struct ctf_stream_pos *) = { | |
161 | [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize, | |
162 | [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize, | |
163 | [CTF_TYPE_FLOAT] = | |
164 | bt_ctf_field_floating_point_serialize, | |
165 | [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize, | |
166 | [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize, | |
167 | [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize, | |
168 | [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize, | |
169 | [CTF_TYPE_STRING] = bt_ctf_field_string_serialize, | |
170 | }; | |
171 | ||
172 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) | |
173 | { | |
174 | struct bt_ctf_field *field = NULL; | |
175 | enum ctf_type_id type_id; | |
176 | ||
177 | if (!type) { | |
178 | goto error; | |
179 | } | |
180 | ||
181 | type_id = bt_ctf_field_type_get_type_id(type); | |
9ce21c30 JG |
182 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES || |
183 | bt_ctf_field_type_validate(type)) { | |
273b65be JG |
184 | goto error; |
185 | } | |
186 | ||
187 | field = field_create_funcs[type_id](type); | |
188 | if (!field) { | |
189 | goto error; | |
190 | } | |
191 | ||
192 | /* The type's declaration can't change after this point */ | |
193 | bt_ctf_field_type_freeze(type); | |
194 | bt_ctf_field_type_get(type); | |
195 | bt_ctf_ref_init(&field->ref_count); | |
196 | field->type = type; | |
197 | error: | |
198 | return field; | |
199 | } | |
200 | ||
201 | void bt_ctf_field_get(struct bt_ctf_field *field) | |
202 | { | |
203 | if (field) { | |
204 | bt_ctf_ref_get(&field->ref_count); | |
205 | } | |
206 | } | |
207 | ||
208 | void bt_ctf_field_put(struct bt_ctf_field *field) | |
209 | { | |
210 | if (field) { | |
211 | bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy); | |
212 | } | |
213 | } | |
214 | ||
215 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, | |
216 | struct bt_ctf_field *length_field) | |
217 | { | |
218 | int ret = 0; | |
219 | struct bt_ctf_field_type_integer *length_type; | |
220 | struct bt_ctf_field_integer *length; | |
221 | struct bt_ctf_field_sequence *sequence; | |
222 | uint64_t sequence_length; | |
223 | ||
224 | if (!field || !length_field) { | |
225 | ret = -1; | |
226 | goto end; | |
227 | } | |
228 | if (bt_ctf_field_type_get_type_id(length_field->type) != | |
229 | CTF_TYPE_INTEGER) { | |
230 | ret = -1; | |
231 | goto end; | |
232 | } | |
233 | ||
234 | length_type = container_of(length_field->type, | |
235 | struct bt_ctf_field_type_integer, parent); | |
236 | if (length_type->declaration.signedness) { | |
237 | ret = -1; | |
238 | goto end; | |
239 | } | |
240 | ||
241 | length = container_of(length_field, struct bt_ctf_field_integer, | |
242 | parent); | |
243 | sequence_length = length->definition.value._unsigned; | |
244 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); | |
245 | if (sequence->elements) { | |
246 | g_ptr_array_free(sequence->elements, TRUE); | |
247 | bt_ctf_field_put(sequence->length); | |
248 | } | |
249 | ||
fe0fe95c | 250 | sequence->elements = g_ptr_array_sized_new((size_t)sequence_length); |
273b65be JG |
251 | if (!sequence->elements) { |
252 | ret = -1; | |
253 | goto end; | |
254 | } | |
255 | ||
fe0fe95c JG |
256 | g_ptr_array_set_free_func(sequence->elements, |
257 | (GDestroyNotify)bt_ctf_field_put); | |
273b65be JG |
258 | g_ptr_array_set_size(sequence->elements, (size_t)sequence_length); |
259 | bt_ctf_field_get(length_field); | |
260 | sequence->length = length_field; | |
261 | end: | |
262 | return ret; | |
263 | } | |
264 | ||
265 | struct bt_ctf_field *bt_ctf_field_structure_get_field( | |
266 | struct bt_ctf_field *field, const char *name) | |
267 | { | |
268 | struct bt_ctf_field *new_field = NULL; | |
269 | GQuark field_quark; | |
270 | struct bt_ctf_field_structure *structure; | |
271 | struct bt_ctf_field_type_structure *structure_type; | |
272 | struct bt_ctf_field_type *field_type; | |
273 | size_t index; | |
274 | ||
275 | if (!field || !name || | |
276 | bt_ctf_field_type_get_type_id(field->type) != | |
277 | CTF_TYPE_STRUCT) { | |
278 | goto error; | |
279 | } | |
280 | ||
281 | field_quark = g_quark_from_string(name); | |
282 | structure = container_of(field, struct bt_ctf_field_structure, parent); | |
283 | structure_type = container_of(field->type, | |
284 | struct bt_ctf_field_type_structure, parent); | |
285 | field_type = bt_ctf_field_type_structure_get_type(structure_type, name); | |
286 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, | |
287 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) { | |
288 | goto error; | |
289 | } | |
290 | ||
291 | if (structure->fields->pdata[index]) { | |
292 | new_field = structure->fields->pdata[index]; | |
293 | goto end; | |
294 | } | |
295 | ||
296 | new_field = bt_ctf_field_create(field_type); | |
297 | if (!new_field) { | |
298 | goto error; | |
299 | } | |
300 | ||
301 | structure->fields->pdata[index] = new_field; | |
302 | end: | |
303 | bt_ctf_field_get(new_field); | |
304 | error: | |
305 | return new_field; | |
306 | } | |
307 | ||
308 | BT_HIDDEN | |
309 | int bt_ctf_field_structure_set_field(struct bt_ctf_field *field, | |
310 | const char *name, struct bt_ctf_field *value) | |
311 | { | |
312 | int ret = 0; | |
313 | GQuark field_quark; | |
314 | struct bt_ctf_field_structure *structure; | |
315 | struct bt_ctf_field_type_structure *structure_type; | |
316 | struct bt_ctf_field_type *expected_field_type; | |
317 | size_t index; | |
318 | ||
319 | if (!field || !name || !value || | |
320 | bt_ctf_field_type_get_type_id(field->type) != | |
321 | CTF_TYPE_STRUCT) { | |
322 | ret = -1; | |
323 | goto end; | |
324 | } | |
325 | ||
326 | field_quark = g_quark_from_string(name); | |
327 | structure = container_of(field, struct bt_ctf_field_structure, parent); | |
328 | structure_type = container_of(field->type, | |
329 | struct bt_ctf_field_type_structure, parent); | |
330 | expected_field_type = bt_ctf_field_type_structure_get_type( | |
331 | structure_type, name); | |
332 | if (expected_field_type != value->type) { | |
333 | ret = -1; | |
334 | goto end; | |
335 | } | |
336 | ||
337 | if (!g_hash_table_lookup_extended(structure->field_name_to_index, | |
338 | GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) { | |
339 | goto end; | |
340 | } | |
341 | ||
342 | if (structure->fields->pdata[index]) { | |
343 | bt_ctf_field_put(structure->fields->pdata[index]); | |
344 | } | |
345 | ||
346 | structure->fields->pdata[index] = value; | |
347 | bt_ctf_field_get(value); | |
348 | end: | |
349 | return ret; | |
350 | } | |
351 | ||
352 | struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field, | |
353 | uint64_t index) | |
354 | { | |
355 | struct bt_ctf_field *new_field = NULL; | |
356 | struct bt_ctf_field_array *array; | |
357 | struct bt_ctf_field_type_array *array_type; | |
358 | struct bt_ctf_field_type *field_type; | |
359 | ||
360 | if (!field || bt_ctf_field_type_get_type_id(field->type) != | |
361 | CTF_TYPE_ARRAY) { | |
362 | goto end; | |
363 | } | |
364 | ||
365 | array = container_of(field, struct bt_ctf_field_array, parent); | |
366 | if (index >= array->elements->len) { | |
367 | goto end; | |
368 | } | |
369 | ||
370 | array_type = container_of(field->type, struct bt_ctf_field_type_array, | |
371 | parent); | |
372 | field_type = bt_ctf_field_type_array_get_element_type(array_type); | |
373 | if (array->elements->pdata[(size_t)index]) { | |
374 | new_field = array->elements->pdata[(size_t)index]; | |
375 | goto end; | |
376 | } | |
377 | ||
378 | new_field = bt_ctf_field_create(field_type); | |
379 | bt_ctf_field_get(new_field); | |
380 | array->elements->pdata[(size_t)index] = new_field; | |
381 | end: | |
382 | return new_field; | |
383 | } | |
384 | ||
385 | struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field, | |
386 | uint64_t index) | |
387 | { | |
388 | struct bt_ctf_field *new_field = NULL; | |
389 | struct bt_ctf_field_sequence *sequence; | |
390 | struct bt_ctf_field_type_sequence *sequence_type; | |
391 | struct bt_ctf_field_type *field_type; | |
392 | ||
393 | if (!field || bt_ctf_field_type_get_type_id(field->type) != | |
394 | CTF_TYPE_SEQUENCE) { | |
395 | goto end; | |
396 | } | |
397 | ||
398 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); | |
399 | if (!sequence->elements || sequence->elements->len <= index) { | |
400 | goto end; | |
401 | } | |
402 | ||
403 | sequence_type = container_of(field->type, | |
404 | struct bt_ctf_field_type_sequence, parent); | |
405 | field_type = bt_ctf_field_type_sequence_get_element_type(sequence_type); | |
406 | if (sequence->elements->pdata[(size_t)index]) { | |
407 | new_field = sequence->elements->pdata[(size_t)index]; | |
408 | goto end; | |
409 | } | |
410 | ||
411 | new_field = bt_ctf_field_create(field_type); | |
412 | bt_ctf_field_get(new_field); | |
413 | sequence->elements->pdata[(size_t)index] = new_field; | |
414 | end: | |
415 | return new_field; | |
416 | } | |
417 | ||
418 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, | |
419 | struct bt_ctf_field *tag_field) | |
420 | { | |
421 | struct bt_ctf_field *new_field = NULL; | |
422 | struct bt_ctf_field_variant *variant; | |
423 | struct bt_ctf_field_type_variant *variant_type; | |
424 | struct bt_ctf_field_type *field_type; | |
425 | struct bt_ctf_field *tag_enum = NULL; | |
426 | struct bt_ctf_field_integer *tag_enum_integer; | |
427 | int64_t tag_enum_value; | |
428 | ||
429 | if (!field || !tag_field || | |
430 | bt_ctf_field_type_get_type_id(field->type) != | |
431 | CTF_TYPE_VARIANT || | |
432 | bt_ctf_field_type_get_type_id(tag_field->type) != | |
433 | CTF_TYPE_ENUM) { | |
434 | goto end; | |
435 | } | |
436 | ||
437 | variant = container_of(field, struct bt_ctf_field_variant, parent); | |
438 | variant_type = container_of(field->type, | |
439 | struct bt_ctf_field_type_variant, parent); | |
440 | tag_enum = bt_ctf_field_enumeration_get_container(tag_field); | |
441 | if (!tag_enum) { | |
442 | goto end; | |
443 | } | |
444 | ||
445 | tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer, | |
446 | parent); | |
447 | ||
a53ed923 | 448 | if (bt_ctf_field_validate(tag_field) < 0) { |
273b65be JG |
449 | goto end; |
450 | } | |
451 | ||
452 | tag_enum_value = tag_enum_integer->definition.value._signed; | |
453 | field_type = bt_ctf_field_type_variant_get_field_type(variant_type, | |
454 | tag_enum_value); | |
455 | if (!field_type) { | |
456 | goto end; | |
457 | } | |
458 | ||
459 | new_field = bt_ctf_field_create(field_type); | |
460 | if (!new_field) { | |
461 | goto end; | |
462 | } | |
463 | ||
464 | bt_ctf_field_put(variant->tag); | |
465 | bt_ctf_field_put(variant->payload); | |
466 | bt_ctf_field_get(new_field); | |
467 | bt_ctf_field_get(tag_field); | |
468 | variant->tag = tag_field; | |
469 | variant->payload = new_field; | |
470 | end: | |
471 | bt_ctf_field_put(tag_enum); | |
472 | return new_field; | |
473 | } | |
474 | ||
475 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container( | |
476 | struct bt_ctf_field *field) | |
477 | { | |
478 | struct bt_ctf_field *container = NULL; | |
479 | struct bt_ctf_field_enumeration *enumeration; | |
480 | ||
481 | if (!field) { | |
482 | goto end; | |
483 | } | |
484 | ||
485 | enumeration = container_of(field, struct bt_ctf_field_enumeration, | |
486 | parent); | |
487 | if (!enumeration->payload) { | |
488 | struct bt_ctf_field_type_enumeration *enumeration_type = | |
489 | container_of(field->type, | |
490 | struct bt_ctf_field_type_enumeration, parent); | |
491 | enumeration->payload = | |
492 | bt_ctf_field_create(enumeration_type->container); | |
493 | } | |
494 | ||
495 | container = enumeration->payload; | |
496 | bt_ctf_field_get(container); | |
497 | end: | |
498 | return container; | |
499 | } | |
500 | ||
501 | int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field, | |
502 | int64_t value) | |
503 | { | |
504 | int ret = 0; | |
505 | struct bt_ctf_field_integer *integer; | |
506 | struct bt_ctf_field_type_integer *integer_type; | |
507 | unsigned int size; | |
508 | int64_t min_value, max_value; | |
509 | ||
510 | if (!field || | |
511 | bt_ctf_field_type_get_type_id(field->type) != | |
512 | CTF_TYPE_INTEGER) { | |
513 | ret = -1; | |
514 | goto end; | |
515 | } | |
516 | ||
517 | integer = container_of(field, struct bt_ctf_field_integer, parent); | |
518 | integer_type = container_of(field->type, | |
519 | struct bt_ctf_field_type_integer, parent); | |
520 | if (!integer_type->declaration.signedness) { | |
521 | ret = -1; | |
522 | goto end; | |
523 | } | |
524 | ||
525 | size = integer_type->declaration.len; | |
526 | min_value = -((int64_t)1 << (size - 1)); | |
527 | max_value = ((int64_t)1 << (size - 1)) - 1; | |
528 | if (value < min_value || value > max_value) { | |
529 | ret = -1; | |
530 | goto end; | |
531 | } | |
532 | ||
533 | integer->definition.value._signed = value; | |
534 | integer->parent.payload_set = 1; | |
535 | end: | |
536 | return ret; | |
537 | } | |
538 | ||
539 | int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field, | |
540 | uint64_t value) | |
541 | { | |
542 | int ret = 0; | |
543 | struct bt_ctf_field_integer *integer; | |
544 | struct bt_ctf_field_type_integer *integer_type; | |
545 | unsigned int size; | |
546 | uint64_t max_value; | |
547 | ||
548 | if (!field || | |
549 | bt_ctf_field_type_get_type_id(field->type) != | |
550 | CTF_TYPE_INTEGER) { | |
551 | ret = -1; | |
552 | goto end; | |
553 | } | |
554 | ||
555 | integer = container_of(field, struct bt_ctf_field_integer, parent); | |
556 | integer_type = container_of(field->type, | |
557 | struct bt_ctf_field_type_integer, parent); | |
558 | if (integer_type->declaration.signedness) { | |
559 | ret = -1; | |
560 | goto end; | |
561 | } | |
562 | ||
563 | size = integer_type->declaration.len; | |
564 | max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1; | |
565 | if (value > max_value) { | |
566 | ret = -1; | |
567 | goto end; | |
568 | } | |
569 | ||
570 | integer->definition.value._unsigned = value; | |
571 | integer->parent.payload_set = 1; | |
572 | end: | |
573 | return ret; | |
574 | } | |
575 | ||
576 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, | |
577 | double value) | |
578 | { | |
579 | int ret = 0; | |
580 | struct bt_ctf_field_floating_point *floating_point; | |
581 | ||
582 | if (!field || | |
583 | bt_ctf_field_type_get_type_id(field->type) != | |
584 | CTF_TYPE_FLOAT) { | |
585 | ret = -1; | |
586 | goto end; | |
587 | } | |
588 | floating_point = container_of(field, struct bt_ctf_field_floating_point, | |
589 | parent); | |
590 | floating_point->definition.value = value; | |
591 | floating_point->parent.payload_set = 1; | |
592 | end: | |
593 | return ret; | |
594 | } | |
595 | ||
596 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, | |
597 | const char *value) | |
598 | { | |
599 | int ret = 0; | |
600 | struct bt_ctf_field_string *string; | |
601 | ||
602 | if (!field || !value || | |
603 | bt_ctf_field_type_get_type_id(field->type) != | |
604 | CTF_TYPE_STRING) { | |
605 | ret = -1; | |
606 | goto end; | |
607 | } | |
608 | ||
609 | string = container_of(field, struct bt_ctf_field_string, parent); | |
610 | if (string->payload) { | |
611 | g_string_free(string->payload, TRUE); | |
612 | } | |
613 | ||
614 | string->payload = g_string_new(value); | |
615 | string->parent.payload_set = 1; | |
616 | end: | |
617 | return ret; | |
618 | } | |
619 | ||
620 | BT_HIDDEN | |
621 | int bt_ctf_field_validate(struct bt_ctf_field *field) | |
622 | { | |
623 | int ret = 0; | |
624 | enum ctf_type_id type_id; | |
625 | ||
626 | if (!field) { | |
627 | ret = -1; | |
628 | goto end; | |
629 | } | |
630 | ||
631 | type_id = bt_ctf_field_type_get_type_id(field->type); | |
632 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { | |
633 | ret = -1; | |
634 | goto end; | |
635 | } | |
636 | ||
637 | ret = field_validate_funcs[type_id](field); | |
638 | end: | |
639 | return ret; | |
640 | } | |
641 | ||
642 | BT_HIDDEN | |
643 | int bt_ctf_field_serialize(struct bt_ctf_field *field, | |
644 | struct ctf_stream_pos *pos) | |
645 | { | |
646 | int ret = 0; | |
647 | enum ctf_type_id type_id; | |
648 | ||
649 | if (!field || !pos) { | |
650 | ret = -1; | |
651 | goto end; | |
652 | } | |
653 | ||
654 | type_id = bt_ctf_field_type_get_type_id(field->type); | |
655 | if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) { | |
656 | ret = -1; | |
657 | goto end; | |
658 | } | |
659 | ||
660 | ret = field_serialize_funcs[type_id](field, pos); | |
661 | end: | |
662 | return ret; | |
663 | } | |
664 | ||
665 | static | |
666 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) | |
667 | { | |
668 | struct bt_ctf_field_type_integer *integer_type = container_of(type, | |
669 | struct bt_ctf_field_type_integer, parent); | |
670 | struct bt_ctf_field_integer *integer = g_new0( | |
671 | struct bt_ctf_field_integer, 1); | |
672 | ||
673 | if (integer) { | |
674 | integer->definition.declaration = &integer_type->declaration; | |
675 | } | |
676 | ||
677 | return integer ? &integer->parent : NULL; | |
678 | } | |
679 | ||
680 | static | |
681 | struct bt_ctf_field *bt_ctf_field_enumeration_create( | |
682 | struct bt_ctf_field_type *type) | |
683 | { | |
684 | struct bt_ctf_field_enumeration *enumeration = g_new0( | |
685 | struct bt_ctf_field_enumeration, 1); | |
686 | ||
687 | return enumeration ? &enumeration->parent : NULL; | |
688 | } | |
689 | ||
690 | static | |
691 | struct bt_ctf_field *bt_ctf_field_floating_point_create( | |
692 | struct bt_ctf_field_type *type) | |
693 | { | |
694 | struct bt_ctf_field_floating_point *floating_point; | |
695 | struct bt_ctf_field_type_floating_point *floating_point_type; | |
696 | ||
697 | floating_point = g_new0(struct bt_ctf_field_floating_point, 1); | |
698 | if (!floating_point) { | |
699 | goto end; | |
700 | } | |
701 | ||
702 | floating_point_type = container_of(type, | |
703 | struct bt_ctf_field_type_floating_point, parent); | |
704 | floating_point->definition.declaration = container_of( | |
705 | type->declaration, struct declaration_float, p); | |
706 | ||
707 | ||
708 | floating_point->definition.sign = &floating_point->sign; | |
709 | floating_point->sign.declaration = &floating_point_type->sign; | |
710 | floating_point->definition.sign->p.declaration = | |
711 | &floating_point_type->sign.p; | |
712 | ||
713 | floating_point->definition.mantissa = &floating_point->mantissa; | |
714 | floating_point->mantissa.declaration = &floating_point_type->mantissa; | |
715 | floating_point->definition.mantissa->p.declaration = | |
716 | &floating_point_type->mantissa.p; | |
717 | ||
718 | floating_point->definition.exp = &floating_point->exp; | |
719 | floating_point->exp.declaration = &floating_point_type->exp; | |
720 | floating_point->definition.exp->p.declaration = | |
721 | &floating_point_type->exp.p; | |
722 | ||
723 | end: | |
724 | return floating_point ? &floating_point->parent : NULL; | |
725 | } | |
726 | ||
727 | static | |
728 | struct bt_ctf_field *bt_ctf_field_structure_create( | |
729 | struct bt_ctf_field_type *type) | |
730 | { | |
731 | struct bt_ctf_field_type_structure *structure_type = container_of(type, | |
732 | struct bt_ctf_field_type_structure, parent); | |
733 | struct bt_ctf_field_structure *structure = g_new0( | |
734 | struct bt_ctf_field_structure, 1); | |
735 | struct bt_ctf_field *field = NULL; | |
736 | ||
737 | if (!structure || !structure_type->fields->len) { | |
738 | goto end; | |
739 | } | |
740 | ||
741 | structure->field_name_to_index = structure_type->field_name_to_index; | |
742 | structure->fields = g_ptr_array_new_with_free_func( | |
743 | (GDestroyNotify)bt_ctf_field_put); | |
744 | g_ptr_array_set_size(structure->fields, | |
745 | g_hash_table_size(structure->field_name_to_index)); | |
746 | field = &structure->parent; | |
747 | end: | |
748 | return field; | |
749 | } | |
750 | ||
751 | static | |
752 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) | |
753 | { | |
754 | struct bt_ctf_field_variant *variant = g_new0( | |
755 | struct bt_ctf_field_variant, 1); | |
756 | return variant ? &variant->parent : NULL; | |
757 | } | |
758 | ||
759 | static | |
760 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) | |
761 | { | |
762 | struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1); | |
763 | struct bt_ctf_field_type_array *array_type; | |
764 | unsigned int array_length; | |
765 | ||
766 | if (!array || !type) { | |
767 | goto error; | |
768 | } | |
769 | ||
770 | array_type = container_of(type, struct bt_ctf_field_type_array, parent); | |
771 | array_length = array_type->length; | |
fe0fe95c | 772 | array->elements = g_ptr_array_sized_new(array_length); |
273b65be JG |
773 | if (!array->elements) { |
774 | goto error; | |
775 | } | |
776 | ||
fe0fe95c JG |
777 | g_ptr_array_set_free_func(array->elements, |
778 | (GDestroyNotify)bt_ctf_field_put); | |
273b65be JG |
779 | g_ptr_array_set_size(array->elements, array_length); |
780 | return &array->parent; | |
781 | error: | |
782 | g_free(array); | |
783 | return NULL; | |
784 | } | |
785 | ||
786 | static | |
787 | struct bt_ctf_field *bt_ctf_field_sequence_create( | |
788 | struct bt_ctf_field_type *type) | |
789 | { | |
790 | struct bt_ctf_field_sequence *sequence = g_new0( | |
791 | struct bt_ctf_field_sequence, 1); | |
792 | return sequence ? &sequence->parent : NULL; | |
793 | } | |
794 | ||
795 | static | |
796 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) | |
797 | { | |
798 | struct bt_ctf_field_string *string = g_new0( | |
799 | struct bt_ctf_field_string, 1); | |
800 | return string ? &string->parent : NULL; | |
801 | } | |
802 | ||
803 | static | |
804 | void bt_ctf_field_destroy(struct bt_ctf_ref *ref) | |
805 | { | |
806 | struct bt_ctf_field *field; | |
807 | struct bt_ctf_field_type *type; | |
808 | enum ctf_type_id type_id; | |
809 | ||
810 | if (!ref) { | |
811 | return; | |
812 | } | |
813 | ||
814 | field = container_of(ref, struct bt_ctf_field, ref_count); | |
815 | type = field->type; | |
816 | type_id = bt_ctf_field_type_get_type_id(type); | |
817 | if (type_id <= CTF_TYPE_UNKNOWN || | |
818 | type_id >= NR_CTF_TYPES) { | |
819 | return; | |
820 | } | |
821 | ||
822 | field_destroy_funcs[type_id](field); | |
823 | if (type) { | |
824 | bt_ctf_field_type_put(type); | |
825 | } | |
826 | } | |
827 | ||
828 | static | |
829 | void bt_ctf_field_integer_destroy(struct bt_ctf_field *field) | |
830 | { | |
831 | struct bt_ctf_field_integer *integer; | |
832 | ||
833 | if (!field) { | |
834 | return; | |
835 | } | |
836 | ||
837 | integer = container_of(field, struct bt_ctf_field_integer, parent); | |
838 | g_free(integer); | |
839 | } | |
840 | ||
841 | static | |
842 | void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field) | |
843 | { | |
844 | struct bt_ctf_field_enumeration *enumeration; | |
845 | ||
846 | if (!field) { | |
847 | return; | |
848 | } | |
849 | ||
850 | enumeration = container_of(field, struct bt_ctf_field_enumeration, | |
851 | parent); | |
852 | bt_ctf_field_put(enumeration->payload); | |
853 | g_free(enumeration); | |
854 | } | |
855 | ||
856 | static | |
857 | void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field) | |
858 | { | |
859 | struct bt_ctf_field_floating_point *floating_point; | |
860 | ||
861 | if (!field) { | |
862 | return; | |
863 | } | |
864 | ||
865 | floating_point = container_of(field, struct bt_ctf_field_floating_point, | |
866 | parent); | |
867 | g_free(floating_point); | |
868 | } | |
869 | ||
870 | static | |
871 | void bt_ctf_field_structure_destroy(struct bt_ctf_field *field) | |
872 | { | |
873 | struct bt_ctf_field_structure *structure; | |
874 | ||
875 | if (!field) { | |
876 | return; | |
877 | } | |
878 | ||
879 | structure = container_of(field, struct bt_ctf_field_structure, parent); | |
880 | g_ptr_array_free(structure->fields, TRUE); | |
881 | g_free(structure); | |
882 | } | |
883 | ||
884 | static | |
885 | void bt_ctf_field_variant_destroy(struct bt_ctf_field *field) | |
886 | { | |
887 | struct bt_ctf_field_variant *variant; | |
888 | ||
889 | if (!field) { | |
890 | return; | |
891 | } | |
892 | ||
893 | variant = container_of(field, struct bt_ctf_field_variant, parent); | |
894 | bt_ctf_field_put(variant->tag); | |
895 | bt_ctf_field_put(variant->payload); | |
896 | g_free(variant); | |
897 | } | |
898 | ||
899 | static | |
900 | void bt_ctf_field_array_destroy(struct bt_ctf_field *field) | |
901 | { | |
902 | struct bt_ctf_field_array *array; | |
903 | ||
904 | if (!field) { | |
905 | return; | |
906 | } | |
907 | ||
908 | array = container_of(field, struct bt_ctf_field_array, parent); | |
909 | g_ptr_array_free(array->elements, TRUE); | |
910 | g_free(array); | |
911 | } | |
912 | ||
913 | static | |
914 | void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field) | |
915 | { | |
916 | struct bt_ctf_field_sequence *sequence; | |
917 | ||
918 | if (!field) { | |
919 | return; | |
920 | } | |
921 | ||
922 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); | |
923 | g_ptr_array_free(sequence->elements, TRUE); | |
924 | bt_ctf_field_put(sequence->length); | |
925 | g_free(sequence); | |
926 | } | |
927 | ||
928 | static | |
929 | void bt_ctf_field_string_destroy(struct bt_ctf_field *field) | |
930 | { | |
931 | struct bt_ctf_field_string *string; | |
932 | if (!field) { | |
933 | return; | |
934 | } | |
935 | ||
936 | string = container_of(field, struct bt_ctf_field_string, parent); | |
937 | g_string_free(string->payload, TRUE); | |
938 | g_free(string); | |
939 | } | |
940 | ||
941 | static | |
942 | int bt_ctf_field_generic_validate(struct bt_ctf_field *field) | |
943 | { | |
da2f6971 | 944 | return (field && field->payload_set) ? 0 : -1; |
273b65be JG |
945 | } |
946 | ||
947 | static | |
948 | int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field) | |
949 | { | |
950 | int ret; | |
951 | struct bt_ctf_field_enumeration *enumeration; | |
952 | ||
953 | if (!field) { | |
954 | ret = -1; | |
955 | goto end; | |
956 | } | |
957 | ||
958 | enumeration = container_of(field, struct bt_ctf_field_enumeration, | |
959 | parent); | |
960 | if (!enumeration->payload) { | |
961 | ret = -1; | |
962 | goto end; | |
963 | } | |
964 | ||
965 | ret = bt_ctf_field_validate(enumeration->payload); | |
966 | end: | |
967 | return ret; | |
968 | } | |
969 | ||
970 | static | |
971 | int bt_ctf_field_structure_validate(struct bt_ctf_field *field) | |
972 | { | |
973 | size_t i; | |
974 | int ret = 0; | |
975 | struct bt_ctf_field_structure *structure; | |
976 | ||
977 | if (!field) { | |
978 | ret = -1; | |
979 | goto end; | |
980 | } | |
981 | ||
982 | structure = container_of(field, struct bt_ctf_field_structure, parent); | |
983 | for (i = 0; i < structure->fields->len; i++) { | |
984 | ret = bt_ctf_field_validate(structure->fields->pdata[i]); | |
985 | if (ret) { | |
986 | goto end; | |
987 | } | |
988 | } | |
989 | end: | |
990 | return ret; | |
991 | } | |
992 | ||
993 | static | |
994 | int bt_ctf_field_variant_validate(struct bt_ctf_field *field) | |
995 | { | |
996 | int ret = 0; | |
997 | struct bt_ctf_field_variant *variant; | |
998 | ||
999 | if (!field) { | |
1000 | ret = -1; | |
1001 | goto end; | |
1002 | } | |
1003 | ||
1004 | variant = container_of(field, struct bt_ctf_field_variant, parent); | |
1005 | ret = bt_ctf_field_validate(variant->payload); | |
1006 | end: | |
1007 | return ret; | |
1008 | } | |
1009 | ||
1010 | static | |
1011 | int bt_ctf_field_array_validate(struct bt_ctf_field *field) | |
1012 | { | |
1013 | size_t i; | |
1014 | int ret = 0; | |
1015 | struct bt_ctf_field_array *array; | |
1016 | ||
1017 | if (!field) { | |
1018 | ret = -1; | |
1019 | goto end; | |
1020 | } | |
1021 | ||
1022 | array = container_of(field, struct bt_ctf_field_array, parent); | |
1023 | for (i = 0; i < array->elements->len; i++) { | |
1024 | ret = bt_ctf_field_validate(array->elements->pdata[i]); | |
1025 | if (ret) { | |
1026 | goto end; | |
1027 | } | |
1028 | } | |
1029 | end: | |
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | static | |
1034 | int bt_ctf_field_sequence_validate(struct bt_ctf_field *field) | |
1035 | { | |
1036 | size_t i; | |
1037 | int ret = 0; | |
1038 | struct bt_ctf_field_sequence *sequence; | |
1039 | ||
1040 | if (!field) { | |
1041 | ret = -1; | |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | sequence = container_of(field, struct bt_ctf_field_sequence, parent); | |
1046 | for (i = 0; i < sequence->elements->len; i++) { | |
1047 | ret = bt_ctf_field_validate(sequence->elements->pdata[i]); | |
1048 | if (ret) { | |
1049 | goto end; | |
1050 | } | |
1051 | } | |
1052 | end: | |
1053 | return ret; | |
1054 | } | |
1055 | ||
1056 | static | |
1057 | int bt_ctf_field_integer_serialize(struct bt_ctf_field *field, | |
1058 | struct ctf_stream_pos *pos) | |
1059 | { | |
1060 | int ret = 0; | |
1061 | struct bt_ctf_field_integer *integer = container_of(field, | |
1062 | struct bt_ctf_field_integer, parent); | |
1063 | ||
1064 | retry: | |
1065 | ret = ctf_integer_write(&pos->parent, &integer->definition.p); | |
1066 | if (ret == -EFAULT) { | |
1067 | /* | |
1068 | * The field is too large to fit in the current packet's | |
1069 | * remaining space. Bump the packet size and retry. | |
1070 | */ | |
1071 | ret = increase_packet_size(pos); | |
1072 | if (ret) { | |
1073 | goto end; | |
1074 | } | |
1075 | goto retry; | |
1076 | } | |
1077 | end: | |
1078 | return ret; | |
1079 | } | |
1080 | ||
1081 | static | |
1082 | int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field, | |
1083 | struct ctf_stream_pos *pos) | |
1084 | { | |
1085 | struct bt_ctf_field_enumeration *enumeration = container_of( | |
1086 | field, struct bt_ctf_field_enumeration, parent); | |
1087 | ||
1088 | return bt_ctf_field_serialize(enumeration->payload, pos); | |
1089 | } | |
1090 | ||
1091 | static | |
1092 | int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field, | |
1093 | struct ctf_stream_pos *pos) | |
1094 | { | |
1095 | int ret = 0; | |
1096 | struct bt_ctf_field_floating_point *floating_point = container_of(field, | |
1097 | struct bt_ctf_field_floating_point, parent); | |
1098 | ||
1099 | retry: | |
1100 | ret = ctf_float_write(&pos->parent, &floating_point->definition.p); | |
1101 | if (ret == -EFAULT) { | |
1102 | /* | |
1103 | * The field is too large to fit in the current packet's | |
1104 | * remaining space. Bump the packet size and retry. | |
1105 | */ | |
1106 | ret = increase_packet_size(pos); | |
1107 | if (ret) { | |
1108 | goto end; | |
1109 | } | |
1110 | goto retry; | |
1111 | } | |
1112 | end: | |
1113 | return ret; | |
1114 | } | |
1115 | ||
1116 | static | |
1117 | int bt_ctf_field_structure_serialize(struct bt_ctf_field *field, | |
1118 | struct ctf_stream_pos *pos) | |
1119 | { | |
1120 | size_t i; | |
1121 | int ret = 0; | |
1122 | struct bt_ctf_field_structure *structure = container_of( | |
1123 | field, struct bt_ctf_field_structure, parent); | |
1124 | ||
1125 | while (!ctf_pos_access_ok(pos, | |
1126 | offset_align(pos->offset, | |
1127 | field->type->declaration->alignment))) { | |
9f56e450 JG |
1128 | ret = increase_packet_size(pos); |
1129 | if (ret) { | |
1130 | goto end; | |
1131 | } | |
273b65be JG |
1132 | } |
1133 | ||
70fd5a51 MD |
1134 | if (!ctf_align_pos(pos, field->type->declaration->alignment)) { |
1135 | ret = -1; | |
1136 | goto end; | |
1137 | } | |
273b65be JG |
1138 | |
1139 | for (i = 0; i < structure->fields->len; i++) { | |
1140 | struct bt_ctf_field *field = g_ptr_array_index( | |
1141 | structure->fields, i); | |
1142 | ||
1143 | ret = bt_ctf_field_serialize(field, pos); | |
1144 | if (ret) { | |
1145 | break; | |
1146 | } | |
1147 | } | |
9f56e450 | 1148 | end: |
273b65be JG |
1149 | return ret; |
1150 | } | |
1151 | ||
1152 | static | |
1153 | int bt_ctf_field_variant_serialize(struct bt_ctf_field *field, | |
1154 | struct ctf_stream_pos *pos) | |
1155 | { | |
1156 | struct bt_ctf_field_variant *variant = container_of( | |
1157 | field, struct bt_ctf_field_variant, parent); | |
1158 | ||
1159 | return bt_ctf_field_serialize(variant->payload, pos); | |
1160 | } | |
1161 | ||
1162 | static | |
1163 | int bt_ctf_field_array_serialize(struct bt_ctf_field *field, | |
1164 | struct ctf_stream_pos *pos) | |
1165 | { | |
1166 | size_t i; | |
1167 | int ret = 0; | |
1168 | struct bt_ctf_field_array *array = container_of( | |
1169 | field, struct bt_ctf_field_array, parent); | |
1170 | ||
1171 | for (i = 0; i < array->elements->len; i++) { | |
1172 | ret = bt_ctf_field_serialize( | |
1173 | g_ptr_array_index(array->elements, i), pos); | |
1174 | if (ret) { | |
1175 | goto end; | |
1176 | } | |
1177 | } | |
1178 | end: | |
1179 | return ret; | |
1180 | } | |
1181 | ||
1182 | static | |
1183 | int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field, | |
1184 | struct ctf_stream_pos *pos) | |
1185 | { | |
1186 | size_t i; | |
1187 | int ret = 0; | |
1188 | struct bt_ctf_field_sequence *sequence = container_of( | |
1189 | field, struct bt_ctf_field_sequence, parent); | |
1190 | ||
1191 | for (i = 0; i < sequence->elements->len; i++) { | |
1192 | ret = bt_ctf_field_serialize( | |
1193 | g_ptr_array_index(sequence->elements, i), pos); | |
1194 | if (ret) { | |
1195 | goto end; | |
1196 | } | |
1197 | } | |
1198 | end: | |
1199 | return ret; | |
1200 | } | |
1201 | ||
1202 | static | |
1203 | int bt_ctf_field_string_serialize(struct bt_ctf_field *field, | |
1204 | struct ctf_stream_pos *pos) | |
1205 | { | |
1206 | size_t i; | |
1207 | int ret = 0; | |
1208 | struct bt_ctf_field_string *string = container_of(field, | |
1209 | struct bt_ctf_field_string, parent); | |
1210 | struct bt_ctf_field_type *character_type = | |
1211 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
1212 | struct bt_ctf_field *character = bt_ctf_field_create(character_type); | |
1213 | ||
1214 | for (i = 0; i < string->payload->len + 1; i++) { | |
1215 | ret = bt_ctf_field_unsigned_integer_set_value(character, | |
1216 | (uint64_t) string->payload->str[i]); | |
1217 | if (ret) { | |
1218 | goto end; | |
1219 | } | |
1220 | ||
1221 | ret = bt_ctf_field_integer_serialize(character, pos); | |
1222 | if (ret) { | |
1223 | goto end; | |
1224 | } | |
1225 | } | |
1226 | end: | |
1227 | bt_ctf_field_put(character); | |
1228 | bt_ctf_field_type_put(character_type); | |
1229 | return ret; | |
1230 | } | |
1231 | ||
1232 | static | |
1233 | int increase_packet_size(struct ctf_stream_pos *pos) | |
1234 | { | |
1235 | int ret; | |
1236 | ||
1237 | assert(pos); | |
1238 | ret = munmap_align(pos->base_mma); | |
1239 | if (ret) { | |
1240 | goto end; | |
1241 | } | |
1242 | ||
1243 | pos->packet_size += PACKET_LEN_INCREMENT; | |
1244 | ret = posix_fallocate(pos->fd, pos->mmap_offset, | |
1245 | pos->packet_size / CHAR_BIT); | |
1246 | if (ret) { | |
1247 | goto end; | |
1248 | } | |
1249 | ||
1250 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, | |
1251 | pos->flags, pos->fd, pos->mmap_offset); | |
1252 | if (pos->base_mma == MAP_FAILED) { | |
1253 | ret = -1; | |
1254 | } | |
1255 | end: | |
1256 | return ret; | |
1257 | } |