Commit | Line | Data |
---|---|---|
2e7d72cf | 1 | /* |
ccd7e1c8 | 2 | * sequence.c |
2e7d72cf | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Sequence Type Converter |
2e7d72cf | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2e7d72cf | 7 | * |
ccd7e1c8 MD |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
2e7d72cf | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
2e7d72cf MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
0f980a35 | 21 | #include <inttypes.h> |
2e7d72cf | 22 | |
be85c1c7 MD |
23 | #ifndef max |
24 | #define max(a, b) ((a) < (b) ? (b) : (a)) | |
25 | #endif | |
26 | ||
c054553d | 27 | static |
f6625916 | 28 | struct definition *_sequence_definition_new(struct declaration *declaration, |
05c749e5 MD |
29 | struct definition_scope *parent_scope, |
30 | GQuark field_name, int index); | |
c054553d | 31 | static |
e1151715 | 32 | void _sequence_definition_free(struct definition *definition); |
c054553d | 33 | |
2e7d72cf MD |
34 | void sequence_copy(struct stream_pos *dest, const struct format *fdest, |
35 | struct stream_pos *src, const struct format *fsrc, | |
e1151715 | 36 | struct definition *definition) |
2e7d72cf | 37 | { |
e1151715 MD |
38 | struct definition_sequence *sequence = |
39 | container_of(definition, struct definition_sequence, p); | |
f6625916 | 40 | struct declaration_sequence *sequence_declaration = sequence->declaration; |
0f980a35 | 41 | uint64_t len, oldlen, i; |
2e7d72cf | 42 | |
f6625916 | 43 | fsrc->sequence_begin(src, sequence_declaration); |
0f980a35 MD |
44 | if (fdest) |
45 | fdest->sequence_begin(dest, sequence_declaration); | |
2e7d72cf | 46 | |
f6625916 | 47 | sequence->len->p.declaration->copy(dest, fdest, src, fsrc, |
e19c3d69 | 48 | &sequence->len->p); |
0f980a35 MD |
49 | len = sequence->len->value._unsigned; |
50 | g_array_set_size(sequence->elems, len); | |
51 | /* | |
52 | * Yes, large sequences could be _painfully slow_ to parse due | |
53 | * to memory allocation for each event read. At least, never | |
54 | * shrink the sequence. Note: the sequence GArray len should | |
55 | * never be used as indicator of the current sequence length. | |
56 | * One should always look at the sequence->len->value._unsigned | |
57 | * value for that. | |
58 | */ | |
59 | oldlen = sequence->elems->len; | |
60 | if (oldlen < len) | |
61 | g_array_set_size(sequence->elems, len); | |
62 | ||
63 | for (i = oldlen; i < len; i++) { | |
64 | struct field *field; | |
65 | GString *str; | |
66 | GQuark name; | |
67 | ||
68 | str = g_string_new(""); | |
69 | g_string_printf(str, "[%" PRIu64 "]", i); | |
70 | (void) g_string_free(str, TRUE); | |
71 | name = g_quark_from_string(str->str); | |
2e7d72cf | 72 | |
0f980a35 MD |
73 | field = &g_array_index(sequence->elems, struct field, i); |
74 | field->name = name; | |
75 | field->definition = sequence_declaration->elem->definition_new(sequence_declaration->elem, | |
76 | sequence->scope, | |
77 | name, i); | |
78 | field->definition->declaration->copy(dest, fdest, src, fsrc, field->definition); | |
2e7d72cf | 79 | } |
f6625916 | 80 | fsrc->sequence_end(src, sequence_declaration); |
0f980a35 MD |
81 | if (fdest) |
82 | fdest->sequence_end(dest, sequence_declaration); | |
2e7d72cf MD |
83 | } |
84 | ||
c054553d | 85 | static |
f6625916 | 86 | void _sequence_declaration_free(struct declaration *declaration) |
2e7d72cf | 87 | { |
f6625916 MD |
88 | struct declaration_sequence *sequence_declaration = |
89 | container_of(declaration, struct declaration_sequence, p); | |
c054553d | 90 | |
f6625916 MD |
91 | free_declaration_scope(sequence_declaration->scope); |
92 | declaration_unref(&sequence_declaration->len_declaration->p); | |
93 | declaration_unref(sequence_declaration->elem); | |
94 | g_free(sequence_declaration); | |
2e7d72cf MD |
95 | } |
96 | ||
f6625916 | 97 | struct declaration_sequence * |
ab4cf058 | 98 | sequence_declaration_new(struct declaration_integer *len_declaration, |
f6625916 MD |
99 | struct declaration *elem_declaration, |
100 | struct declaration_scope *parent_scope) | |
2e7d72cf | 101 | { |
f6625916 MD |
102 | struct declaration_sequence *sequence_declaration; |
103 | struct declaration *declaration; | |
2e7d72cf | 104 | |
f6625916 MD |
105 | sequence_declaration = g_new(struct declaration_sequence, 1); |
106 | declaration = &sequence_declaration->p; | |
107 | assert(!len_declaration->signedness); | |
108 | declaration_ref(&len_declaration->p); | |
109 | sequence_declaration->len_declaration = len_declaration; | |
110 | declaration_ref(elem_declaration); | |
111 | sequence_declaration->elem = elem_declaration; | |
112 | sequence_declaration->scope = new_declaration_scope(parent_scope); | |
113 | declaration->id = CTF_TYPE_SEQUENCE; | |
f6625916 MD |
114 | declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment); |
115 | declaration->copy = sequence_copy; | |
116 | declaration->declaration_free = _sequence_declaration_free; | |
117 | declaration->definition_new = _sequence_definition_new; | |
118 | declaration->definition_free = _sequence_definition_free; | |
119 | declaration->ref = 1; | |
120 | return sequence_declaration; | |
2e7d72cf | 121 | } |
c054553d MD |
122 | |
123 | static | |
f6625916 | 124 | struct definition *_sequence_definition_new(struct declaration *declaration, |
05c749e5 MD |
125 | struct definition_scope *parent_scope, |
126 | GQuark field_name, int index) | |
c054553d | 127 | { |
f6625916 MD |
128 | struct declaration_sequence *sequence_declaration = |
129 | container_of(declaration, struct declaration_sequence, p); | |
e1151715 MD |
130 | struct definition_sequence *sequence; |
131 | struct definition *len_parent; | |
c054553d | 132 | |
e1151715 | 133 | sequence = g_new(struct definition_sequence, 1); |
f6625916 MD |
134 | declaration_ref(&sequence_declaration->p); |
135 | sequence->p.declaration = declaration; | |
136 | sequence->declaration = sequence_declaration; | |
c054553d | 137 | sequence->p.ref = 1; |
05c749e5 MD |
138 | sequence->p.index = index; |
139 | sequence->scope = new_definition_scope(parent_scope, field_name); | |
f6625916 | 140 | len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p, |
0f980a35 | 141 | sequence->scope, |
05c749e5 | 142 | g_quark_from_static_string("length"), 0); |
e19c3d69 | 143 | sequence->len = |
e1151715 | 144 | container_of(len_parent, struct definition_integer, p); |
0f980a35 | 145 | sequence->elems = g_array_new(FALSE, TRUE, sizeof(struct field)); |
c054553d MD |
146 | return &sequence->p; |
147 | } | |
148 | ||
149 | static | |
e1151715 | 150 | void _sequence_definition_free(struct definition *definition) |
c054553d | 151 | { |
e1151715 MD |
152 | struct definition_sequence *sequence = |
153 | container_of(definition, struct definition_sequence, p); | |
154 | struct definition *len_definition = &sequence->len->p; | |
0f980a35 MD |
155 | uint64_t i; |
156 | ||
157 | for (i = 0; i < sequence->elems->len; i++) { | |
158 | struct field *field; | |
c054553d | 159 | |
0f980a35 MD |
160 | field = &g_array_index(sequence->elems, struct field, i); |
161 | field->definition->declaration->definition_free(field->definition); | |
162 | } | |
163 | (void) g_array_free(sequence->elems, TRUE); | |
f6625916 | 164 | len_definition->declaration->definition_free(len_definition); |
e1151715 | 165 | free_definition_scope(sequence->scope); |
f6625916 | 166 | declaration_unref(sequence->p.declaration); |
c054553d MD |
167 | g_free(sequence); |
168 | } | |
0f980a35 MD |
169 | |
170 | struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i) | |
171 | { | |
172 | if (i >= sequence->len->value._unsigned) | |
173 | return NULL; | |
174 | assert(i < sequence->elems->len); | |
175 | return g_array_index(sequence->elems, struct field, i).definition; | |
176 | } |