4 * BabelTrace - Sequence Type Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
26 struct definition
*_sequence_definition_new(struct declaration
*declaration
,
27 struct definition_scope
*parent_scope
,
28 GQuark field_name
, int index
,
29 const char *root_name
);
31 void _sequence_definition_free(struct definition
*definition
);
33 int sequence_rw(struct stream_pos
*pos
, struct definition
*definition
)
35 struct definition_sequence
*sequence_definition
=
36 container_of(definition
, struct definition_sequence
, p
);
37 const struct declaration_sequence
*sequence_declaration
=
38 sequence_definition
->declaration
;
39 uint64_t len
, oldlen
, i
;
42 len
= sequence_definition
->length
->value
._unsigned
;
44 * Yes, large sequences could be _painfully slow_ to parse due
45 * to memory allocation for each event read. At least, never
46 * shrink the sequence. Note: the sequence GArray len should
47 * never be used as indicator of the current sequence length.
48 * One should always look at the sequence->len->value._unsigned
51 oldlen
= sequence_definition
->elems
->len
;
53 g_ptr_array_set_size(sequence_definition
->elems
, len
);
55 for (i
= oldlen
; i
< len
; i
++) {
56 struct definition
**field
;
60 str
= g_string_new("");
61 g_string_printf(str
, "[%" PRIu64
"]", i
);
62 (void) g_string_free(str
, TRUE
);
63 name
= g_quark_from_string(str
->str
);
65 field
= (struct definition
**) &g_ptr_array_index(sequence_definition
->elems
, i
);
66 *field
= sequence_declaration
->elem
->definition_new(sequence_declaration
->elem
,
67 sequence_definition
->p
.scope
,
69 ret
= generic_rw(pos
, *field
);
77 void _sequence_declaration_free(struct declaration
*declaration
)
79 struct declaration_sequence
*sequence_declaration
=
80 container_of(declaration
, struct declaration_sequence
, p
);
82 free_declaration_scope(sequence_declaration
->scope
);
83 g_array_free(sequence_declaration
->length_name
, TRUE
);
84 declaration_unref(sequence_declaration
->elem
);
85 g_free(sequence_declaration
);
88 struct declaration_sequence
*
89 sequence_declaration_new(const char *length
,
90 struct declaration
*elem_declaration
,
91 struct declaration_scope
*parent_scope
)
93 struct declaration_sequence
*sequence_declaration
;
94 struct declaration
*declaration
;
96 sequence_declaration
= g_new(struct declaration_sequence
, 1);
97 declaration
= &sequence_declaration
->p
;
99 sequence_declaration
->length_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
100 append_scope_path(length
, sequence_declaration
->length_name
);
102 declaration_ref(elem_declaration
);
103 sequence_declaration
->elem
= elem_declaration
;
104 sequence_declaration
->scope
= new_declaration_scope(parent_scope
);
105 declaration
->id
= CTF_TYPE_SEQUENCE
;
106 declaration
->alignment
= elem_declaration
->alignment
;
107 declaration
->declaration_free
= _sequence_declaration_free
;
108 declaration
->definition_new
= _sequence_definition_new
;
109 declaration
->definition_free
= _sequence_definition_free
;
110 declaration
->ref
= 1;
111 return sequence_declaration
;
115 struct definition
*_sequence_definition_new(struct declaration
*declaration
,
116 struct definition_scope
*parent_scope
,
117 GQuark field_name
, int index
,
118 const char *root_name
)
120 struct declaration_sequence
*sequence_declaration
=
121 container_of(declaration
, struct declaration_sequence
, p
);
122 struct definition_sequence
*sequence
;
123 struct definition
*len_parent
;
126 sequence
= g_new(struct definition_sequence
, 1);
127 declaration_ref(&sequence_declaration
->p
);
128 sequence
->p
.declaration
= declaration
;
129 sequence
->declaration
= sequence_declaration
;
132 * Use INT_MAX order to ensure that all fields of the parent
133 * scope are seen as being prior to this scope.
135 sequence
->p
.index
= root_name
? INT_MAX
: index
;
136 sequence
->p
.name
= field_name
;
137 sequence
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
138 sequence
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
139 ret
= register_field_definition(field_name
, &sequence
->p
,
142 len_parent
= lookup_path_definition(sequence
->p
.scope
->scope_path
,
143 sequence_declaration
->length_name
,
146 printf("[error] Lookup for sequence length field failed.\n");
150 container_of(len_parent
, struct definition_integer
, p
);
151 if (sequence
->length
->declaration
->signedness
) {
152 printf("[error] Sequence length field should be unsigned.\n");
155 definition_ref(len_parent
);
157 sequence
->string
= NULL
;
158 sequence
->elems
= NULL
;
160 if (sequence_declaration
->elem
->id
== CTF_TYPE_INTEGER
) {
161 struct declaration_integer
*integer_declaration
=
162 container_of(sequence_declaration
->elem
, struct declaration_integer
, p
);
164 if (integer_declaration
->encoding
== CTF_STRING_UTF8
165 || integer_declaration
->encoding
== CTF_STRING_ASCII
) {
167 sequence
->string
= g_string_new("");
169 if (integer_declaration
->len
== CHAR_BIT
170 && integer_declaration
->p
.alignment
== CHAR_BIT
) {
176 sequence
->elems
= g_ptr_array_new();
180 free_definition_scope(sequence
->p
.scope
);
181 declaration_unref(&sequence_declaration
->p
);
187 void _sequence_definition_free(struct definition
*definition
)
189 struct definition_sequence
*sequence
=
190 container_of(definition
, struct definition_sequence
, p
);
191 struct definition
*len_definition
= &sequence
->length
->p
;
194 if (sequence
->string
)
195 (void) g_string_free(sequence
->string
, TRUE
);
196 if (sequence
->elems
) {
197 for (i
= 0; i
< sequence
->elems
->len
; i
++) {
198 struct definition
*field
;
200 field
= g_ptr_array_index(sequence
->elems
, i
);
201 field
->declaration
->definition_free(field
);
204 (void) g_ptr_array_free(sequence
->elems
, TRUE
);
205 definition_unref(len_definition
);
206 free_definition_scope(sequence
->p
.scope
);
207 declaration_unref(sequence
->p
.declaration
);
211 uint64_t sequence_len(struct definition_sequence
*sequence
)
213 return sequence
->length
->value
._unsigned
;
216 struct definition
*sequence_index(struct definition_sequence
*sequence
, uint64_t i
)
218 if (!sequence
->elems
)
220 if (i
>= sequence
->length
->value
._unsigned
)
222 assert(i
< sequence
->elems
->len
);
223 return g_ptr_array_index(sequence
->elems
, i
);