ed719bc5d553ef3b34d5125c38dcd771076ec1a9
[babeltrace.git] / types / sequence.c
1 /*
2 * sequence.c
3 *
4 * BabelTrace - Sequence Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
23 #include <inttypes.h>
24
25 static
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);
30 static
31 void _sequence_definition_free(struct definition *definition);
32
33 int sequence_rw(struct stream_pos *pos, struct definition *definition)
34 {
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;
40 int ret;
41
42 len = sequence_definition->length->value._unsigned;
43 /*
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
49 * value for that.
50 */
51 oldlen = sequence_definition->elems->len;
52 if (oldlen < len)
53 g_ptr_array_set_size(sequence_definition->elems, len);
54
55 for (i = oldlen; i < len; i++) {
56 struct definition **field;
57 GString *str;
58 GQuark name;
59
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);
64
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,
68 name, i, NULL);
69 ret = generic_rw(pos, *field);
70 if (ret)
71 return ret;
72 }
73 return 0;
74 }
75
76 static
77 void _sequence_declaration_free(struct declaration *declaration)
78 {
79 struct declaration_sequence *sequence_declaration =
80 container_of(declaration, struct declaration_sequence, p);
81
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);
86 }
87
88 struct declaration_sequence *
89 sequence_declaration_new(const char *length,
90 struct declaration *elem_declaration,
91 struct declaration_scope *parent_scope)
92 {
93 struct declaration_sequence *sequence_declaration;
94 struct declaration *declaration;
95
96 sequence_declaration = g_new(struct declaration_sequence, 1);
97 declaration = &sequence_declaration->p;
98
99 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
100 append_scope_path(length, sequence_declaration->length_name);
101
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;
112 }
113
114 static
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)
119 {
120 struct declaration_sequence *sequence_declaration =
121 container_of(declaration, struct declaration_sequence, p);
122 struct definition_sequence *sequence;
123 struct definition *len_parent;
124 int ret;
125
126 sequence = g_new(struct definition_sequence, 1);
127 declaration_ref(&sequence_declaration->p);
128 sequence->p.declaration = declaration;
129 sequence->declaration = sequence_declaration;
130 sequence->p.ref = 1;
131 /*
132 * Use INT_MAX order to ensure that all fields of the parent
133 * scope are seen as being prior to this scope.
134 */
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,
140 parent_scope);
141 assert(!ret);
142 len_parent = lookup_path_definition(sequence->p.scope->scope_path,
143 sequence_declaration->length_name,
144 parent_scope);
145 if (!len_parent) {
146 printf("[error] Lookup for sequence length field failed.\n");
147 goto error;
148 }
149 sequence->length =
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");
153 goto error;
154 }
155 definition_ref(len_parent);
156
157 sequence->string = NULL;
158 sequence->elems = NULL;
159
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);
163
164 if (integer_declaration->encoding == CTF_STRING_UTF8
165 || integer_declaration->encoding == CTF_STRING_ASCII) {
166
167 sequence->string = g_string_new("");
168
169 if (integer_declaration->len == CHAR_BIT
170 && integer_declaration->p.alignment == CHAR_BIT) {
171 return &sequence->p;
172 }
173 }
174 }
175
176 sequence->elems = g_ptr_array_new();
177 return &sequence->p;
178
179 error:
180 free_definition_scope(sequence->p.scope);
181 declaration_unref(&sequence_declaration->p);
182 g_free(sequence);
183 return NULL;
184 }
185
186 static
187 void _sequence_definition_free(struct definition *definition)
188 {
189 struct definition_sequence *sequence =
190 container_of(definition, struct definition_sequence, p);
191 struct definition *len_definition = &sequence->length->p;
192 uint64_t i;
193
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;
199
200 field = g_ptr_array_index(sequence->elems, i);
201 field->declaration->definition_free(field);
202 }
203 }
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);
208 g_free(sequence);
209 }
210
211 uint64_t sequence_len(struct definition_sequence *sequence)
212 {
213 return sequence->length->value._unsigned;
214 }
215
216 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
217 {
218 if (!sequence->elems)
219 return NULL;
220 if (i >= sequence->length->value._unsigned)
221 return NULL;
222 assert(i < sequence->elems->len);
223 return g_ptr_array_index(sequence->elems, i);
224 }
This page took 0.032485 seconds and 3 git commands to generate.