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