Fix: assign the current clock for mmap traces
[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 <babeltrace/types.h>
24 #include <inttypes.h>
25
26 static
27 struct definition *_sequence_definition_new(struct declaration *declaration,
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index,
30 const char *root_name);
31 static
32 void _sequence_definition_free(struct definition *definition);
33
34 int sequence_rw(struct stream_pos *pos, struct definition *definition)
35 {
36 struct definition_sequence *sequence_definition =
37 container_of(definition, struct definition_sequence, p);
38 const struct declaration_sequence *sequence_declaration =
39 sequence_definition->declaration;
40 uint64_t len, oldlen, i;
41 int ret;
42
43 len = sequence_definition->length->value._unsigned;
44 /*
45 * Yes, large sequences could be _painfully slow_ to parse due
46 * to memory allocation for each event read. At least, never
47 * shrink the sequence. Note: the sequence GArray len should
48 * never be used as indicator of the current sequence length.
49 * One should always look at the sequence->len->value._unsigned
50 * value for that.
51 */
52 oldlen = sequence_definition->elems->len;
53 if (oldlen < len)
54 g_ptr_array_set_size(sequence_definition->elems, len);
55
56 for (i = oldlen; i < len; i++) {
57 struct definition **field;
58 GString *str;
59 GQuark name;
60
61 str = g_string_new("");
62 g_string_printf(str, "[%" PRIu64 "]", i);
63 name = g_quark_from_string(str->str);
64 (void) g_string_free(str, TRUE);
65
66 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
67 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
68 sequence_definition->p.scope,
69 name, i, NULL);
70 }
71 for (i = 0; i < len; i++) {
72 struct definition **field;
73
74 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
75 ret = generic_rw(pos, *field);
76 if (ret)
77 return ret;
78 }
79 return 0;
80 }
81
82 static
83 void _sequence_declaration_free(struct declaration *declaration)
84 {
85 struct declaration_sequence *sequence_declaration =
86 container_of(declaration, struct declaration_sequence, p);
87
88 free_declaration_scope(sequence_declaration->scope);
89 g_array_free(sequence_declaration->length_name, TRUE);
90 declaration_unref(sequence_declaration->elem);
91 g_free(sequence_declaration);
92 }
93
94 struct declaration_sequence *
95 sequence_declaration_new(const char *length,
96 struct declaration *elem_declaration,
97 struct declaration_scope *parent_scope)
98 {
99 struct declaration_sequence *sequence_declaration;
100 struct declaration *declaration;
101
102 sequence_declaration = g_new(struct declaration_sequence, 1);
103 declaration = &sequence_declaration->p;
104
105 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
106 append_scope_path(length, sequence_declaration->length_name);
107
108 declaration_ref(elem_declaration);
109 sequence_declaration->elem = elem_declaration;
110 sequence_declaration->scope = new_declaration_scope(parent_scope);
111 declaration->id = CTF_TYPE_SEQUENCE;
112 declaration->alignment = elem_declaration->alignment;
113 declaration->declaration_free = _sequence_declaration_free;
114 declaration->definition_new = _sequence_definition_new;
115 declaration->definition_free = _sequence_definition_free;
116 declaration->ref = 1;
117 return sequence_declaration;
118 }
119
120 static
121 struct definition *_sequence_definition_new(struct declaration *declaration,
122 struct definition_scope *parent_scope,
123 GQuark field_name, int index,
124 const char *root_name)
125 {
126 struct declaration_sequence *sequence_declaration =
127 container_of(declaration, struct declaration_sequence, p);
128 struct definition_sequence *sequence;
129 struct definition *len_parent;
130 int ret;
131
132 sequence = g_new(struct definition_sequence, 1);
133 declaration_ref(&sequence_declaration->p);
134 sequence->p.declaration = declaration;
135 sequence->declaration = sequence_declaration;
136 sequence->p.ref = 1;
137 /*
138 * Use INT_MAX order to ensure that all fields of the parent
139 * scope are seen as being prior to this scope.
140 */
141 sequence->p.index = root_name ? INT_MAX : index;
142 sequence->p.name = field_name;
143 sequence->p.path = new_definition_path(parent_scope, field_name, root_name);
144 sequence->p.scope = new_definition_scope(parent_scope, field_name, root_name);
145 ret = register_field_definition(field_name, &sequence->p,
146 parent_scope);
147 assert(!ret);
148 len_parent = lookup_path_definition(sequence->p.scope->scope_path,
149 sequence_declaration->length_name,
150 parent_scope);
151 if (!len_parent) {
152 printf("[error] Lookup for sequence length field failed.\n");
153 goto error;
154 }
155 sequence->length =
156 container_of(len_parent, struct definition_integer, p);
157 if (sequence->length->declaration->signedness) {
158 printf("[error] Sequence length field should be unsigned.\n");
159 goto error;
160 }
161 definition_ref(len_parent);
162
163 sequence->string = NULL;
164 sequence->elems = NULL;
165
166 if (sequence_declaration->elem->id == CTF_TYPE_INTEGER) {
167 struct declaration_integer *integer_declaration =
168 container_of(sequence_declaration->elem, struct declaration_integer, p);
169
170 if (integer_declaration->encoding == CTF_STRING_UTF8
171 || integer_declaration->encoding == CTF_STRING_ASCII) {
172
173 sequence->string = g_string_new("");
174
175 if (integer_declaration->len == CHAR_BIT
176 && integer_declaration->p.alignment == CHAR_BIT) {
177 return &sequence->p;
178 }
179 }
180 }
181
182 sequence->elems = g_ptr_array_new();
183 return &sequence->p;
184
185 error:
186 free_definition_scope(sequence->p.scope);
187 declaration_unref(&sequence_declaration->p);
188 g_free(sequence);
189 return NULL;
190 }
191
192 static
193 void _sequence_definition_free(struct definition *definition)
194 {
195 struct definition_sequence *sequence =
196 container_of(definition, struct definition_sequence, p);
197 struct definition *len_definition = &sequence->length->p;
198 uint64_t i;
199
200 if (sequence->string)
201 (void) g_string_free(sequence->string, TRUE);
202 if (sequence->elems) {
203 for (i = 0; i < sequence->elems->len; i++) {
204 struct definition *field;
205
206 field = g_ptr_array_index(sequence->elems, i);
207 field->declaration->definition_free(field);
208 }
209 }
210 (void) g_ptr_array_free(sequence->elems, TRUE);
211 definition_unref(len_definition);
212 free_definition_scope(sequence->p.scope);
213 declaration_unref(sequence->p.declaration);
214 g_free(sequence);
215 }
216
217 uint64_t sequence_len(struct definition_sequence *sequence)
218 {
219 return sequence->length->value._unsigned;
220 }
221
222 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
223 {
224 if (!sequence->elems)
225 return NULL;
226 if (i >= sequence->length->value._unsigned)
227 return NULL;
228 assert(i < sequence->elems->len);
229 return g_ptr_array_index(sequence->elems, i);
230 }
This page took 0.033079 seconds and 4 git commands to generate.