configure: re-enable '-Wunused-parameter'
[babeltrace.git] / src / ctf-writer / fields.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
8 */
9
10 #ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
11 #define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
12
13 #include <glib.h>
14 #include <inttypes.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <string.h>
19
20 #include <babeltrace2-ctf-writer/fields.h>
21 #include <babeltrace2/types.h>
22
23 #include "common/macros.h"
24 #include "common/common.h"
25 #include "ctfser/ctfser.h"
26
27 #include "assert-pre.h"
28 #include "field-types.h"
29 #include "object.h"
30 #include "utils.h"
31
32 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
33 BT_CTF_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
34 _name " has the wrong type ID: expected-type-id=%s, " \
35 "field-addr=%p", \
36 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
37
38 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
39 BT_CTF_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
40 _name " is not set: field-addr=%p", (_field))
41
42 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
43 BT_CTF_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
44
45 struct bt_ctf_field_common;
46
47 typedef void (*bt_ctf_field_common_method_set_is_frozen)(struct bt_ctf_field_common *,
48 bool);
49 typedef int (*bt_ctf_field_common_method_validate)(struct bt_ctf_field_common *);
50 typedef struct bt_ctf_field_common *(*bt_ctf_field_common_method_copy)(
51 struct bt_ctf_field_common *);
52 typedef bt_ctf_bool (*bt_ctf_field_common_method_is_set)(struct bt_ctf_field_common *);
53 typedef void (*bt_ctf_field_common_method_reset)(struct bt_ctf_field_common *);
54
55 struct bt_ctf_field_common_methods {
56 bt_ctf_field_common_method_set_is_frozen set_is_frozen;
57 bt_ctf_field_common_method_validate validate;
58 bt_ctf_field_common_method_copy copy;
59 bt_ctf_field_common_method_is_set is_set;
60 bt_ctf_field_common_method_reset reset;
61 };
62
63 struct bt_ctf_field_common {
64 struct bt_ctf_object base;
65 struct bt_ctf_field_type_common *type;
66 struct bt_ctf_field_common_methods *methods;
67 bool payload_set;
68 bool frozen;
69
70 /*
71 * Specialized data for either CTF IR or CTF writer APIs.
72 * See comment in `field-types.h` for more details.
73 */
74 union {
75 struct {
76 } ir;
77 struct {
78 void *serialize_func;
79 } writer;
80 } spec;
81 };
82
83 struct bt_ctf_field_common_integer {
84 struct bt_ctf_field_common common;
85 union {
86 int64_t signd;
87 uint64_t unsignd;
88 } payload;
89 };
90
91 struct bt_ctf_field_common_floating_point {
92 struct bt_ctf_field_common common;
93 double payload;
94 };
95
96 struct bt_ctf_field_common_structure {
97 struct bt_ctf_field_common common;
98
99 /* Array of `struct bt_ctf_field_common *`, owned by this */
100 GPtrArray *fields;
101 };
102
103 struct bt_ctf_field_common_variant {
104 struct bt_ctf_field_common common;
105
106 union {
107 uint64_t u;
108 int64_t i;
109 } tag_value;
110
111 /* Weak: belongs to `choices` below */
112 struct bt_ctf_field_common *current_field;
113
114 /* Array of `struct bt_ctf_field_common *`, owned by this */
115 GPtrArray *fields;
116 };
117
118 struct bt_ctf_field_common_array {
119 struct bt_ctf_field_common common;
120
121 /* Array of `struct bt_ctf_field_common *`, owned by this */
122 GPtrArray *elements;
123 };
124
125 struct bt_ctf_field_common_sequence {
126 struct bt_ctf_field_common common;
127
128 /*
129 * This is the true sequence field's length: its value can be
130 * less than `elements->len` below because we never shrink the
131 * array of elements to avoid reallocation.
132 */
133 uint64_t length;
134
135 /* Array of `struct bt_ctf_field_common *`, owned by this */
136 GPtrArray *elements;
137 };
138
139 struct bt_ctf_field_common_string {
140 struct bt_ctf_field_common common;
141 GArray *buf;
142 size_t size;
143 };
144
145 struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field);
146
147 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field,
148 struct bt_ctf_field_type_common *type,
149 bool is_shared, bt_ctf_object_release_func release_func,
150 struct bt_ctf_field_common_methods *methods,
151 bt_ctf_field_common_create_func field_create_func,
152 GDestroyNotify field_release_func);
153
154 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field,
155 struct bt_ctf_field_type_common *type,
156 bool is_shared, bt_ctf_object_release_func release_func,
157 struct bt_ctf_field_common_methods *methods,
158 bt_ctf_field_common_create_func field_create_func,
159 GDestroyNotify field_destroy_func);
160
161 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field,
162 struct bt_ctf_field_type_common *type,
163 bool is_shared, bt_ctf_object_release_func release_func,
164 struct bt_ctf_field_common_methods *methods,
165 GDestroyNotify field_destroy_func);
166
167 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field,
168 struct bt_ctf_field_type_common *type,
169 bool is_shared, bt_ctf_object_release_func release_func,
170 struct bt_ctf_field_common_methods *methods,
171 bt_ctf_field_common_create_func field_create_func,
172 GDestroyNotify field_release_func);
173
174 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
175 struct bt_ctf_field_type_common *type,
176 bool is_shared, bt_ctf_object_release_func release_func,
177 struct bt_ctf_field_common_methods *methods);
178
179 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field);
180
181 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field);
182
183 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field);
184
185 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field);
186
187 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field);
188
189 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field);
190
191 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field);
192
193 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field);
194
195 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field);
196
197 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field);
198
199 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field,
200 bool is_frozen);
201
202 void bt_ctf_field_common_structure_set_is_frozen_recursive(
203 struct bt_ctf_field_common *field, bool is_frozen);
204
205 void bt_ctf_field_common_variant_set_is_frozen_recursive(
206 struct bt_ctf_field_common *field, bool is_frozen);
207
208 void bt_ctf_field_common_array_set_is_frozen_recursive(
209 struct bt_ctf_field_common *field, bool is_frozen);
210
211 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
212 struct bt_ctf_field_common *field, bool is_frozen);
213
214 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field,
215 bool is_frozen);
216
217 bt_ctf_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field);
218
219 bt_ctf_bool bt_ctf_field_common_structure_is_set_recursive(
220 struct bt_ctf_field_common *field);
221
222 bt_ctf_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field);
223
224 bt_ctf_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field);
225
226 bt_ctf_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field);
227
228 #ifdef BT_DEV_MODE
229 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
230 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
231 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
232 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
233 # define bt_ctf_field_common_set _bt_ctf_field_common_set
234 #else
235 # define bt_ctf_field_common_validate_recursive(_field) (-1)
236 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
237 # define bt_ctf_field_common_is_set_recursive(_field) (BT_CTF_FALSE)
238 # define bt_ctf_field_common_reset_recursive(_field)
239 # define bt_ctf_field_common_set(_field, _val)
240 #endif
241
242 BT_ASSERT_DBG_FUNC
243 static inline bool field_type_common_has_known_id(
244 struct bt_ctf_field_type_common *ft)
245 {
246 return (int) ft->id > BT_CTF_FIELD_TYPE_ID_UNKNOWN &&
247 (int) ft->id < BT_CTF_FIELD_TYPE_ID_NR;
248 }
249
250 static inline
251 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common *field)
252 {
253 int ret = 0;
254
255 if (!field) {
256 BT_CTF_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
257 ret = -1;
258 goto end;
259 }
260
261 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
262
263 if (field->methods->validate) {
264 ret = field->methods->validate(field);
265 }
266
267 end:
268 return ret;
269 }
270
271 static inline
272 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common *field)
273 {
274 BT_ASSERT_DBG(field);
275 BT_ASSERT_DBG(field->methods->reset);
276 field->methods->reset(field);
277 }
278
279 static inline
280 void _bt_ctf_field_common_set(struct bt_ctf_field_common *field, bool value)
281 {
282 BT_ASSERT_DBG(field);
283 field->payload_set = value;
284 }
285
286 static inline
287 bt_ctf_bool _bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common *field)
288 {
289 bt_ctf_bool is_set = BT_CTF_FALSE;
290
291 if (!field) {
292 goto end;
293 }
294
295 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
296 BT_ASSERT_DBG(field->methods->is_set);
297 is_set = field->methods->is_set(field);
298
299 end:
300 return is_set;
301 }
302
303 static inline
304 void bt_ctf_field_common_initialize(struct bt_ctf_field_common *field,
305 struct bt_ctf_field_type_common *ft, bool is_shared,
306 bt_ctf_object_release_func release_func,
307 struct bt_ctf_field_common_methods *methods)
308 {
309 BT_ASSERT_DBG(field);
310 BT_ASSERT_DBG(ft);
311 bt_ctf_object_init(&field->base, is_shared, release_func);
312 field->methods = methods;
313 field->type = (void *) bt_ctf_object_get_ref(ft);
314 }
315
316 static inline
317 struct bt_ctf_field_type_common *bt_ctf_field_common_borrow_type(
318 struct bt_ctf_field_common *field)
319 {
320 struct bt_ctf_field_type_common *ret = NULL;
321
322 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
323 ret = field->type;
324 return ret;
325 }
326
327 static inline
328 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common *field)
329 {
330 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
331
332 BT_CTF_ASSERT_PRE_NON_NULL(field, "Sequence field");
333 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_SEQUENCE,
334 "Field");
335 return (int64_t) sequence->length;
336 }
337
338 static inline
339 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common *field,
340 uint64_t length, bt_ctf_field_common_create_func field_create_func)
341 {
342 int ret = 0;
343 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
344
345 BT_CTF_ASSERT_PRE_NON_NULL(field, "Sequence field");
346 BT_CTF_ASSERT_PRE(((int64_t) length) >= 0,
347 "Invalid sequence length (too large): length=%" PRId64,
348 length);
349 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "Sequence field");
350
351 if (G_UNLIKELY(length > sequence->elements->len)) {
352 /* Make more room */
353 struct bt_ctf_field_type_common_sequence *sequence_ft;
354 uint64_t cur_len = sequence->elements->len;
355 uint64_t i;
356
357 g_ptr_array_set_size(sequence->elements, length);
358 sequence_ft = BT_CTF_FROM_COMMON(sequence->common.type);
359
360 for (i = cur_len; i < sequence->elements->len; i++) {
361 struct bt_ctf_field_common *elem_field =
362 field_create_func(sequence_ft->element_ft);
363
364 if (!elem_field) {
365 ret = -1;
366 goto end;
367 }
368
369 BT_ASSERT_DBG(!sequence->elements->pdata[i]);
370 sequence->elements->pdata[i] = elem_field;
371 }
372 }
373
374 sequence->length = length;
375
376 end:
377 return ret;
378 }
379
380 static inline
381 struct bt_ctf_field_common *bt_ctf_field_common_structure_borrow_field_by_name(
382 struct bt_ctf_field_common *field, const char *name)
383 {
384 struct bt_ctf_field_common *ret = NULL;
385 GQuark field_quark;
386 struct bt_ctf_field_type_common_structure *structure_ft;
387 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
388 size_t index;
389 GHashTable *field_name_to_index;
390
391 BT_CTF_ASSERT_PRE_NON_NULL(field, "Structure field");
392 BT_CTF_ASSERT_PRE_NON_NULL(name, "Field name");
393 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
394 BT_CTF_FIELD_TYPE_ID_STRUCT, "Field");
395 structure_ft = BT_CTF_FROM_COMMON(field->type);
396 field_name_to_index = structure_ft->field_name_to_index;
397 field_quark = g_quark_from_string(name);
398 if (!g_hash_table_lookup_extended(field_name_to_index,
399 GUINT_TO_POINTER(field_quark),
400 NULL, (gpointer *) &index)) {
401 BT_LOGT("Invalid parameter: no such field in structure field's type: "
402 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
403 field, field->type, name);
404 goto error;
405 }
406
407 ret = structure->fields->pdata[index];
408 BT_ASSERT_DBG(ret);
409
410 error:
411 return ret;
412 }
413
414 static inline
415 struct bt_ctf_field_common *bt_ctf_field_common_structure_borrow_field_by_index(
416 struct bt_ctf_field_common *field, uint64_t index)
417 {
418 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
419
420 BT_CTF_ASSERT_PRE_NON_NULL(field, "Structure field");
421 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
422 BT_CTF_FIELD_TYPE_ID_STRUCT, "Field");
423 BT_CTF_ASSERT_PRE(index < structure->fields->len,
424 "Index is out of bound: struct-field-addr=%p, "
425 "index=%" PRIu64 ", count=%u", field, index,
426 structure->fields->len);
427 return structure->fields->pdata[index];
428 }
429
430 static inline
431 struct bt_ctf_field_common *bt_ctf_field_common_array_borrow_field(
432 struct bt_ctf_field_common *field, uint64_t index)
433 {
434 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
435
436 BT_CTF_ASSERT_PRE_NON_NULL(field, "Array field");
437 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_ARRAY,
438 "Field");
439 BT_CTF_ASSERT_PRE(index < array->elements->len,
440 "Index is out of bound: array-field-addr=%p, "
441 "index=%" PRIu64 ", count=%u", field,
442 index, array->elements->len);
443 return array->elements->pdata[(size_t) index];
444 }
445
446 static inline
447 struct bt_ctf_field_common *bt_ctf_field_common_sequence_borrow_field(
448 struct bt_ctf_field_common *field, uint64_t index)
449 {
450 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
451
452 BT_CTF_ASSERT_PRE_NON_NULL(field, "Sequence field");
453 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field, BT_CTF_FIELD_TYPE_ID_SEQUENCE,
454 "Field");
455 BT_CTF_ASSERT_PRE(index < sequence->length,
456 "Index is out of bound: seq-field-addr=%p, "
457 "index=%" PRIu64 ", count=%u", field, index,
458 sequence->elements->len);
459 return sequence->elements->pdata[(size_t) index];
460 }
461
462 static inline
463 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common *variant_field,
464 uint64_t tag_uval, bool is_signed)
465 {
466 int ret = 0;
467 int64_t choice_index;
468 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
469
470 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
471 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
472 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
473
474 /* Find matching index in variant field's type */
475 choice_index = bt_ctf_field_type_common_variant_find_choice_index(
476 variant_field->type, tag_uval, is_signed);
477 if (choice_index < 0) {
478 ret = -1;
479 goto end;
480 }
481
482 /* Select corresponding field */
483 BT_ASSERT_DBG(choice_index < variant->fields->len);
484 variant->current_field = variant->fields->pdata[choice_index];
485 variant->tag_value.u = tag_uval;
486
487 end:
488 return ret;
489 }
490
491 static inline
492 struct bt_ctf_field_common *bt_ctf_field_common_variant_borrow_current_field(
493 struct bt_ctf_field_common *variant_field)
494 {
495 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
496
497 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
498 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
499 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
500 BT_CTF_ASSERT_PRE(variant->current_field,
501 "Variant field has no current field: field-addr=%p", variant_field);
502 return variant->current_field;
503 }
504
505 static inline
506 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common *variant_field,
507 int64_t *tag)
508 {
509 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
510
511 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
512 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
513 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
514 BT_CTF_ASSERT_PRE(variant->current_field,
515 "Variant field has no current field: field-addr=%p", variant_field);
516 *tag = variant->tag_value.i;
517 return 0;
518 }
519
520 static inline
521 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common *variant_field,
522 uint64_t *tag)
523 {
524 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(variant_field);
525
526 BT_CTF_ASSERT_PRE_NON_NULL(variant_field, "Variant field");
527 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field,
528 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
529 BT_CTF_ASSERT_PRE(variant->current_field,
530 "Variant field has no current field: field-addr=%p", variant_field);
531 *tag = variant->tag_value.u;
532 return 0;
533 }
534
535 static inline
536 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common *field,
537 double *value)
538 {
539 struct bt_ctf_field_common_floating_point *floating_point =
540 BT_CTF_FROM_COMMON(field);
541
542 BT_CTF_ASSERT_PRE_NON_NULL(field, "Floating point number field");
543 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
544 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field");
545 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
546 BT_CTF_FIELD_TYPE_ID_FLOAT, "Field");
547 *value = floating_point->payload;
548 return 0;
549 }
550
551 static inline
552 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common *field,
553 double value)
554 {
555 struct bt_ctf_field_common_floating_point *floating_point =
556 BT_CTF_FROM_COMMON(field);
557
558 BT_CTF_ASSERT_PRE_NON_NULL(field, "Floating point number field");
559 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "Floating point number field");
560 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
561 BT_CTF_FIELD_TYPE_ID_FLOAT, "Field");
562 floating_point->payload = value;
563 bt_ctf_field_common_set(field, true);
564 return 0;
565 }
566
567 static inline
568 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common *field)
569 {
570 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
571
572 BT_CTF_ASSERT_PRE_NON_NULL(field, "String field");
573 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field");
574 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
575 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
576 return (const char *) string->buf->data;
577 }
578
579 static inline
580 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common *field)
581 {
582 struct bt_ctf_field_common_string *string_field = BT_CTF_FROM_COMMON(field);
583
584 BT_CTF_ASSERT_PRE_NON_NULL(field, "String field");
585 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
586 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
587 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
588 string_field->size = 0;
589 bt_ctf_field_common_set(field, true);
590 return 0;
591 }
592
593 static inline
594 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common *field,
595 const char *value, unsigned int length)
596 {
597 struct bt_ctf_field_common_string *string_field = BT_CTF_FROM_COMMON(field);
598 char *data;
599 size_t new_size;
600
601 BT_CTF_ASSERT_PRE_NON_NULL(field, "String field");
602 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
603 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
604 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
605 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
606
607 /* Make sure no null bytes are appended */
608 BT_CTF_ASSERT_PRE(!memchr(value, '\0', length),
609 "String value to append contains a null character: "
610 "partial-value=\"%.32s\", length=%u", value, length);
611
612 new_size = string_field->size + length;
613
614 if (G_UNLIKELY(new_size + 1 > string_field->buf->len)) {
615 g_array_set_size(string_field->buf, new_size + 1);
616 }
617
618 data = string_field->buf->data;
619 memcpy(data + string_field->size, value, length);
620 ((char *) string_field->buf->data)[new_size] = '\0';
621 string_field->size = new_size;
622 bt_ctf_field_common_set(field, true);
623 return 0;
624 }
625
626 static inline
627 int bt_ctf_field_common_string_append(struct bt_ctf_field_common *field,
628 const char *value)
629 {
630 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
631 return bt_ctf_field_common_string_append_len(field, value,
632 strlen(value));
633 }
634
635 static inline
636 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common *field,
637 const char *value)
638 {
639 BT_CTF_ASSERT_PRE_NON_NULL(field, "String field");
640 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
641 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field, "String field");
642 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field,
643 BT_CTF_FIELD_TYPE_ID_STRING, "Field");
644 bt_ctf_field_common_string_clear(field);
645 return bt_ctf_field_common_string_append_len(field,
646 value, strlen(value));
647 }
648
649 static inline
650 void bt_ctf_field_common_finalize(struct bt_ctf_field_common *field)
651 {
652 BT_ASSERT_DBG(field);
653 BT_LOGD_STR("Putting field's type.");
654 bt_ctf_object_put_ref(field->type);
655 }
656
657 static inline
658 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common *field)
659 {
660 BT_ASSERT_DBG(field);
661 BT_LOGD("Finalizing common integer field object: addr=%p", field);
662 bt_ctf_field_common_finalize(field);
663 }
664
665 static inline
666 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common *field)
667 {
668 BT_ASSERT_DBG(field);
669 BT_LOGD("Finalizing common floating point number field object: addr=%p", field);
670 bt_ctf_field_common_finalize(field);
671 }
672
673 static inline
674 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common *field)
675 {
676 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
677
678 BT_ASSERT_DBG(field);
679 BT_LOGD("Finalizing common structure field object: addr=%p", field);
680 bt_ctf_field_common_finalize(field);
681
682 if (structure->fields) {
683 g_ptr_array_free(structure->fields, TRUE);
684 }
685 }
686
687 static inline
688 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common *field)
689 {
690 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
691
692 BT_ASSERT_DBG(field);
693 BT_LOGD("Finalizing common variant field object: addr=%p", field);
694 bt_ctf_field_common_finalize(field);
695
696 if (variant->fields) {
697 g_ptr_array_free(variant->fields, TRUE);
698 }
699 }
700
701 static inline
702 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common *field)
703 {
704 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
705
706 BT_ASSERT_DBG(field);
707 BT_LOGD("Finalizing common array field object: addr=%p", field);
708 bt_ctf_field_common_finalize(field);
709
710 if (array->elements) {
711 g_ptr_array_free(array->elements, TRUE);
712 }
713 }
714
715 static inline
716 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common *field)
717 {
718 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
719
720 BT_ASSERT_DBG(field);
721 BT_LOGD("Finalizing common sequence field object: addr=%p", field);
722 bt_ctf_field_common_finalize(field);
723
724 if (sequence->elements) {
725 g_ptr_array_free(sequence->elements, TRUE);
726 }
727 }
728
729 static inline
730 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common *field)
731 {
732 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
733
734 BT_ASSERT_DBG(field);
735 BT_LOGD("Finalizing common string field object: addr=%p", field);
736 bt_ctf_field_common_finalize(field);
737
738 if (string->buf) {
739 g_array_free(string->buf, TRUE);
740 }
741 }
742
743 BT_CTF_ASSERT_PRE_FUNC
744 static inline bool value_is_in_range_signed(unsigned int size, int64_t value)
745 {
746 bool ret = true;
747 int64_t min_value, max_value;
748
749 min_value = -(1ULL << (size - 1));
750 max_value = (1ULL << (size - 1)) - 1;
751 if (value < min_value || value > max_value) {
752 BT_LOGF("Value is out of bounds: value=%" PRId64 ", "
753 "min-value=%" PRId64 ", max-value=%" PRId64,
754 value, min_value, max_value);
755 ret = false;
756 }
757
758 return ret;
759 }
760
761 BT_CTF_ASSERT_PRE_FUNC
762 static inline bool value_is_in_range_unsigned(unsigned int size, uint64_t value)
763 {
764 bool ret = true;
765 int64_t max_value;
766
767 max_value = (size == 64) ? UINT64_MAX : ((uint64_t) 1 << size) - 1;
768 if (value > max_value) {
769 BT_LOGF("Value is out of bounds: value=%" PRIu64 ", "
770 "max-value=%" PRIu64,
771 value, max_value);
772 ret = false;
773 }
774
775 return ret;
776 }
777
778 struct bt_ctf_field_enumeration {
779 struct bt_ctf_field_common common;
780 struct bt_ctf_field_common_integer *container;
781 };
782
783 struct bt_ctf_field_variant {
784 struct bt_ctf_field_common_variant common;
785 struct bt_ctf_field_enumeration *tag;
786 };
787
788 int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field,
789 struct bt_ctfser *ctfser,
790 enum bt_ctf_byte_order native_byte_order);
791
792 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
793 const char *name, struct bt_ctf_field *value);
794
795 struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container(
796 struct bt_ctf_field *field);
797
798 #ifndef BT_DEV_MODE
799 #define BT_FIELD_UNUSED_ATTR __attribute__((unused))
800 #else
801 #define BT_FIELD_UNUSED_ATTR
802 #endif
803
804 static inline
805 bt_ctf_bool bt_ctf_field_is_set_recursive(
806 struct bt_ctf_field *field BT_FIELD_UNUSED_ATTR)
807 {
808 return bt_ctf_field_common_is_set_recursive((void *) field);
809 }
810
811 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */
This page took 0.067884 seconds and 4 git commands to generate.