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  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  */
28 
29 #include <babeltrace/compiler.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/types.h>
32 #include <inttypes.h>
33 
34 static
35 struct bt_definition *_sequence_definition_new(struct bt_declaration *declaration,
36 					struct definition_scope *parent_scope,
37 					GQuark field_name, int index,
38 					const char *root_name);
39 static
40 void _sequence_definition_free(struct bt_definition *definition);
41 
bt_sequence_rw(struct bt_stream_pos * pos,struct bt_definition * definition)42 int bt_sequence_rw(struct bt_stream_pos *pos, struct bt_definition *definition)
43 {
44 	struct definition_sequence *sequence_definition =
45 		container_of(definition, struct definition_sequence, p);
46 	const struct declaration_sequence *sequence_declaration =
47 		sequence_definition->declaration;
48 	uint64_t len, oldlen, i;
49 	int ret;
50 
51 	len = sequence_definition->length->value._unsigned;
52 	/*
53 	 * Yes, large sequences could be _painfully slow_ to parse due
54 	 * to memory allocation for each event read. At least, never
55 	 * shrink the sequence. Note: the sequence GArray len should
56 	 * never be used as indicator of the current sequence length.
57 	 * One should always look at the sequence->len->value._unsigned
58 	 * value for that.
59 	 */
60 	oldlen = sequence_definition->elems->len;
61 	if (oldlen < len)
62 		g_ptr_array_set_size(sequence_definition->elems, len);
63 
64 	for (i = oldlen; i < len; i++) {
65 		struct bt_definition **field;
66 		GString *str;
67 		GQuark name;
68 
69 		str = g_string_new("");
70 		g_string_printf(str, "[%" PRIu64 "]", i);
71 		name = g_quark_from_string(str->str);
72 		(void) g_string_free(str, TRUE);
73 
74 		field = (struct bt_definition **) &g_ptr_array_index(sequence_definition->elems, i);
75 		*field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
76 					  sequence_definition->p.scope,
77 					  name, i, NULL);
78 	}
79 	for (i = 0; i < len; i++) {
80 		struct bt_definition **field;
81 
82 		field = (struct bt_definition **) &g_ptr_array_index(sequence_definition->elems, i);
83 		ret = generic_rw(pos, *field);
84 		if (ret)
85 			return ret;
86 	}
87 	return 0;
88 }
89 
90 static
_sequence_declaration_free(struct bt_declaration * declaration)91 void _sequence_declaration_free(struct bt_declaration *declaration)
92 {
93 	struct declaration_sequence *sequence_declaration =
94 		container_of(declaration, struct declaration_sequence, p);
95 
96 	bt_free_declaration_scope(sequence_declaration->scope);
97 	g_array_free(sequence_declaration->length_name, TRUE);
98 	bt_declaration_unref(sequence_declaration->elem);
99 	g_free(sequence_declaration);
100 }
101 
102 struct declaration_sequence *
bt_sequence_declaration_new(const char * length,struct bt_declaration * elem_declaration,struct declaration_scope * parent_scope)103 	bt_sequence_declaration_new(const char *length,
104 			  struct bt_declaration *elem_declaration,
105 			  struct declaration_scope *parent_scope)
106 {
107 	struct declaration_sequence *sequence_declaration;
108 	struct bt_declaration *declaration;
109 
110 	sequence_declaration = g_new(struct declaration_sequence, 1);
111 	declaration = &sequence_declaration->p;
112 
113 	sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
114 	bt_append_scope_path(length, sequence_declaration->length_name);
115 
116 	bt_declaration_ref(elem_declaration);
117 	sequence_declaration->elem = elem_declaration;
118 	sequence_declaration->scope = bt_new_declaration_scope(parent_scope);
119 	declaration->id = CTF_TYPE_SEQUENCE;
120 	declaration->alignment = elem_declaration->alignment;
121 	declaration->declaration_free = _sequence_declaration_free;
122 	declaration->definition_new = _sequence_definition_new;
123 	declaration->definition_free = _sequence_definition_free;
124 	declaration->ref = 1;
125 	return sequence_declaration;
126 }
127 
128 static
_sequence_definition_new(struct bt_declaration * declaration,struct definition_scope * parent_scope,GQuark field_name,int index,const char * root_name)129 struct bt_definition *_sequence_definition_new(struct bt_declaration *declaration,
130 				struct definition_scope *parent_scope,
131 				GQuark field_name, int index,
132 				const char *root_name)
133 {
134 	struct declaration_sequence *sequence_declaration =
135 		container_of(declaration, struct declaration_sequence, p);
136 	struct definition_sequence *sequence;
137 	struct bt_definition *len_parent;
138 	int ret;
139 
140 	sequence = g_new(struct definition_sequence, 1);
141 	bt_declaration_ref(&sequence_declaration->p);
142 	sequence->p.declaration = declaration;
143 	sequence->declaration = sequence_declaration;
144 	sequence->p.ref = 1;
145 	/*
146 	 * Use INT_MAX order to ensure that all fields of the parent
147 	 * scope are seen as being prior to this scope.
148 	 */
149 	sequence->p.index = root_name ? INT_MAX : index;
150 	sequence->p.name = field_name;
151 	sequence->p.path = bt_new_definition_path(parent_scope, field_name, root_name);
152 	sequence->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name);
153 	ret = bt_register_field_definition(field_name, &sequence->p,
154 					parent_scope);
155 	assert(!ret);
156 	len_parent = bt_lookup_path_definition(sequence->p.scope->scope_path,
157 					    sequence_declaration->length_name,
158 					    parent_scope);
159 	if (!len_parent) {
160 		printf("[error] Lookup for sequence length field failed.\n");
161 		goto error;
162 	}
163 	sequence->length =
164 		container_of(len_parent, struct definition_integer, p);
165 	if (sequence->length->declaration->signedness) {
166 		printf("[error] Sequence length field should be unsigned.\n");
167 		goto error;
168 	}
169 	bt_definition_ref(len_parent);
170 
171 	sequence->string = NULL;
172 	sequence->elems = NULL;
173 
174 	if (sequence_declaration->elem->id == CTF_TYPE_INTEGER &&
175 			bt_int_is_char(sequence_declaration->elem)) {
176 		sequence->string = g_string_new("");
177 	}
178 
179 	sequence->elems = g_ptr_array_new();
180 	return &sequence->p;
181 
182 error:
183 	bt_free_definition_scope(sequence->p.scope);
184 	bt_declaration_unref(&sequence_declaration->p);
185 	g_free(sequence);
186 	return NULL;
187 }
188 
189 static
_sequence_definition_free(struct bt_definition * definition)190 void _sequence_definition_free(struct bt_definition *definition)
191 {
192 	struct definition_sequence *sequence =
193 		container_of(definition, struct definition_sequence, p);
194 	struct bt_definition *len_definition = &sequence->length->p;
195 	uint64_t i;
196 
197 	if (sequence->string)
198 		(void) g_string_free(sequence->string, TRUE);
199 	if (sequence->elems) {
200 		for (i = 0; i < sequence->elems->len; i++) {
201 			struct bt_definition *field;
202 
203 			field = g_ptr_array_index(sequence->elems, i);
204 			field->declaration->definition_free(field);
205 		}
206 		(void) g_ptr_array_free(sequence->elems, TRUE);
207 	}
208 	bt_definition_unref(len_definition);
209 	bt_free_definition_scope(sequence->p.scope);
210 	bt_declaration_unref(sequence->p.declaration);
211 	g_free(sequence);
212 }
213 
bt_sequence_len(struct definition_sequence * sequence)214 uint64_t bt_sequence_len(struct definition_sequence *sequence)
215 {
216 	return sequence->length->value._unsigned;
217 }
218 
bt_sequence_index(struct definition_sequence * sequence,uint64_t i)219 struct bt_definition *bt_sequence_index(struct definition_sequence *sequence, uint64_t i)
220 {
221 	if (!sequence->elems)
222 		return NULL;
223 	if (i >= sequence->length->value._unsigned)
224 		return NULL;
225 	assert(i < sequence->elems->len);
226 	return g_ptr_array_index(sequence->elems, i);
227 }
228