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