Update sequence (type specifier -> field ref), fix definition lookup
[babeltrace.git] / types / sequence.c
1 /*
2 * sequence.c
3 *
4 * BabelTrace - Sequence Type Converter
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
21 #include <inttypes.h>
22
23 static
24 struct definition *_sequence_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index,
27 const char *root_name);
28 static
29 void _sequence_definition_free(struct definition *definition);
30
31 int sequence_rw(struct stream_pos *pos, struct definition *definition)
32 {
33 struct definition_sequence *sequence_definition =
34 container_of(definition, struct definition_sequence, p);
35 const struct declaration_sequence *sequence_declaration =
36 sequence_definition->declaration;
37 uint64_t len, oldlen, i;
38 int ret;
39
40 len = sequence_definition->length->value._unsigned;
41 /*
42 * Yes, large sequences could be _painfully slow_ to parse due
43 * to memory allocation for each event read. At least, never
44 * shrink the sequence. Note: the sequence GArray len should
45 * never be used as indicator of the current sequence length.
46 * One should always look at the sequence->len->value._unsigned
47 * value for that.
48 */
49 oldlen = sequence_definition->elems->len;
50 if (oldlen < len)
51 g_ptr_array_set_size(sequence_definition->elems, len);
52
53 for (i = oldlen; i < len; i++) {
54 struct definition **field;
55 GString *str;
56 GQuark name;
57
58 str = g_string_new("");
59 g_string_printf(str, "[%" PRIu64 "]", i);
60 (void) g_string_free(str, TRUE);
61 name = g_quark_from_string(str->str);
62
63 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
64 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
65 sequence_definition->scope,
66 name, i, NULL);
67 ret = generic_rw(pos, *field);
68 if (ret)
69 return ret;
70 }
71 return 0;
72 }
73
74 static
75 void _sequence_declaration_free(struct declaration *declaration)
76 {
77 struct declaration_sequence *sequence_declaration =
78 container_of(declaration, struct declaration_sequence, p);
79
80 free_declaration_scope(sequence_declaration->scope);
81 g_array_free(sequence_declaration->length_name, TRUE);
82 declaration_unref(sequence_declaration->elem);
83 g_free(sequence_declaration);
84 }
85
86 struct declaration_sequence *
87 sequence_declaration_new(const char *length,
88 struct declaration *elem_declaration,
89 struct declaration_scope *parent_scope)
90 {
91 struct declaration_sequence *sequence_declaration;
92 struct declaration *declaration;
93
94 sequence_declaration = g_new(struct declaration_sequence, 1);
95 declaration = &sequence_declaration->p;
96
97 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
98 append_scope_path(length, sequence_declaration->length_name);
99
100 declaration_ref(elem_declaration);
101 sequence_declaration->elem = elem_declaration;
102 sequence_declaration->scope = new_declaration_scope(parent_scope);
103 declaration->id = CTF_TYPE_SEQUENCE;
104 declaration->alignment = elem_declaration->alignment;
105 declaration->declaration_free = _sequence_declaration_free;
106 declaration->definition_new = _sequence_definition_new;
107 declaration->definition_free = _sequence_definition_free;
108 declaration->ref = 1;
109 return sequence_declaration;
110 }
111
112 static
113 struct definition *_sequence_definition_new(struct declaration *declaration,
114 struct definition_scope *parent_scope,
115 GQuark field_name, int index,
116 const char *root_name)
117 {
118 struct declaration_sequence *sequence_declaration =
119 container_of(declaration, struct declaration_sequence, p);
120 struct definition_sequence *sequence;
121 struct definition *len_parent;
122 int ret;
123
124 sequence = g_new(struct definition_sequence, 1);
125 declaration_ref(&sequence_declaration->p);
126 sequence->p.declaration = declaration;
127 sequence->declaration = sequence_declaration;
128 sequence->p.ref = 1;
129 /*
130 * Use INT_MAX order to ensure that all fields of the parent
131 * scope are seen as being prior to this scope.
132 */
133 sequence->p.index = root_name ? INT_MAX : index;
134 sequence->p.name = field_name;
135 sequence->p.path = new_definition_path(parent_scope, field_name, root_name);
136 sequence->scope = new_definition_scope(parent_scope, field_name, root_name);
137 ret = register_field_definition(field_name, &sequence->p,
138 parent_scope);
139 assert(!ret);
140 len_parent = lookup_definition(sequence->scope->scope_path,
141 sequence_declaration->length_name,
142 parent_scope);
143 if (!len_parent) {
144 printf("[error] Lookup for sequence length field failed.\n");
145 goto error;
146 }
147 sequence->length =
148 container_of(len_parent, struct definition_integer, p);
149 if (sequence->length->declaration->signedness) {
150 printf("[error] Sequence length field should be unsigned.\n");
151 goto error;
152 }
153 definition_ref(len_parent);
154 sequence->elems = g_ptr_array_new();
155 return &sequence->p;
156
157 error:
158 free_definition_scope(sequence->scope);
159 declaration_unref(&sequence_declaration->p);
160 g_free(sequence);
161 return NULL;
162 }
163
164 static
165 void _sequence_definition_free(struct definition *definition)
166 {
167 struct definition_sequence *sequence =
168 container_of(definition, struct definition_sequence, p);
169 struct definition *len_definition = &sequence->length->p;
170 uint64_t i;
171
172 for (i = 0; i < sequence->elems->len; i++) {
173 struct definition *field;
174
175 field = g_ptr_array_index(sequence->elems, i);
176 field->declaration->definition_free(field);
177 }
178 (void) g_ptr_array_free(sequence->elems, TRUE);
179 definition_unref(len_definition);
180 free_definition_scope(sequence->scope);
181 declaration_unref(sequence->p.declaration);
182 g_free(sequence);
183 }
184
185 uint64_t sequence_len(struct definition_sequence *sequence)
186 {
187 return sequence->length->value._unsigned;
188 }
189
190 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
191 {
192 if (i >= sequence->length->value._unsigned)
193 return NULL;
194 assert(i < sequence->elems->len);
195 return g_ptr_array_index(sequence->elems, i);
196 }
This page took 0.034046 seconds and 5 git commands to generate.