Add shared copyright to EfficiOS Inc. and Linux Foundation
[babeltrace.git] / types / sequence.c
CommitLineData
2e7d72cf 1/*
ccd7e1c8 2 * sequence.c
2e7d72cf 3 *
ccd7e1c8 4 * BabelTrace - Sequence Type Converter
2e7d72cf 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2e7d72cf 9 *
ccd7e1c8
MD
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:
2e7d72cf 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
2e7d72cf
MD
19 */
20
21#include <babeltrace/compiler.h>
4c8bfb7e 22#include <babeltrace/format.h>
0f980a35 23#include <inttypes.h>
2e7d72cf 24
c054553d 25static
f6625916 26struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 27 struct definition_scope *parent_scope,
98df1c9f
MD
28 GQuark field_name, int index,
29 const char *root_name);
c054553d 30static
e1151715 31void _sequence_definition_free(struct definition *definition);
c054553d 32
c5e74408 33int sequence_rw(struct stream_pos *pos, struct definition *definition)
2e7d72cf 34{
d11e9c49 35 struct definition_sequence *sequence_definition =
e1151715 36 container_of(definition, struct definition_sequence, p);
d11e9c49
MD
37 const struct declaration_sequence *sequence_declaration =
38 sequence_definition->declaration;
0f980a35 39 uint64_t len, oldlen, i;
c5e74408 40 int ret;
2e7d72cf 41
98df1c9f 42 len = sequence_definition->length->value._unsigned;
0f980a35
MD
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 */
d11e9c49 51 oldlen = sequence_definition->elems->len;
0f980a35 52 if (oldlen < len)
b1a2f580 53 g_ptr_array_set_size(sequence_definition->elems, len);
0f980a35
MD
54
55 for (i = oldlen; i < len; i++) {
b1a2f580 56 struct definition **field;
0f980a35
MD
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);
2e7d72cf 64
b1a2f580
MD
65 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
66 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
a35173fe 67 sequence_definition->p.scope,
98df1c9f 68 name, i, NULL);
b1a2f580 69 ret = generic_rw(pos, *field);
c5e74408
MD
70 if (ret)
71 return ret;
2e7d72cf 72 }
c5e74408 73 return 0;
2e7d72cf
MD
74}
75
c054553d 76static
f6625916 77void _sequence_declaration_free(struct declaration *declaration)
2e7d72cf 78{
f6625916
MD
79 struct declaration_sequence *sequence_declaration =
80 container_of(declaration, struct declaration_sequence, p);
c054553d 81
f6625916 82 free_declaration_scope(sequence_declaration->scope);
98df1c9f 83 g_array_free(sequence_declaration->length_name, TRUE);
f6625916
MD
84 declaration_unref(sequence_declaration->elem);
85 g_free(sequence_declaration);
2e7d72cf
MD
86}
87
f6625916 88struct declaration_sequence *
98df1c9f 89 sequence_declaration_new(const char *length,
f6625916
MD
90 struct declaration *elem_declaration,
91 struct declaration_scope *parent_scope)
2e7d72cf 92{
f6625916
MD
93 struct declaration_sequence *sequence_declaration;
94 struct declaration *declaration;
2e7d72cf 95
f6625916
MD
96 sequence_declaration = g_new(struct declaration_sequence, 1);
97 declaration = &sequence_declaration->p;
98df1c9f
MD
98
99 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
100 append_scope_path(length, sequence_declaration->length_name);
101
f6625916
MD
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;
98df1c9f 106 declaration->alignment = elem_declaration->alignment;
f6625916
MD
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;
2e7d72cf 112}
c054553d
MD
113
114static
f6625916 115struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 116 struct definition_scope *parent_scope,
98df1c9f
MD
117 GQuark field_name, int index,
118 const char *root_name)
c054553d 119{
f6625916
MD
120 struct declaration_sequence *sequence_declaration =
121 container_of(declaration, struct declaration_sequence, p);
e1151715
MD
122 struct definition_sequence *sequence;
123 struct definition *len_parent;
98df1c9f 124 int ret;
c054553d 125
e1151715 126 sequence = g_new(struct definition_sequence, 1);
f6625916
MD
127 declaration_ref(&sequence_declaration->p);
128 sequence->p.declaration = declaration;
129 sequence->declaration = sequence_declaration;
c054553d 130 sequence->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 136 sequence->p.name = field_name;
98df1c9f 137 sequence->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 138 sequence->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
139 ret = register_field_definition(field_name, &sequence->p,
140 parent_scope);
141 assert(!ret);
a35173fe
MD
142 len_parent = lookup_path_definition(sequence->p.scope->scope_path,
143 sequence_declaration->length_name,
144 parent_scope);
98df1c9f
MD
145 if (!len_parent) {
146 printf("[error] Lookup for sequence length field failed.\n");
147 goto error;
148 }
149 sequence->length =
e1151715 150 container_of(len_parent, struct definition_integer, p);
98df1c9f
MD
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);
81dee1bb
MD
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
b1a2f580 176 sequence->elems = g_ptr_array_new();
c054553d 177 return &sequence->p;
98df1c9f
MD
178
179error:
a35173fe 180 free_definition_scope(sequence->p.scope);
98df1c9f
MD
181 declaration_unref(&sequence_declaration->p);
182 g_free(sequence);
183 return NULL;
c054553d
MD
184}
185
186static
e1151715 187void _sequence_definition_free(struct definition *definition)
c054553d 188{
e1151715
MD
189 struct definition_sequence *sequence =
190 container_of(definition, struct definition_sequence, p);
98df1c9f 191 struct definition *len_definition = &sequence->length->p;
0f980a35
MD
192 uint64_t i;
193
81dee1bb
MD
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;
c054553d 199
81dee1bb
MD
200 field = g_ptr_array_index(sequence->elems, i);
201 field->declaration->definition_free(field);
202 }
0f980a35 203 }
b1a2f580 204 (void) g_ptr_array_free(sequence->elems, TRUE);
98df1c9f 205 definition_unref(len_definition);
a35173fe 206 free_definition_scope(sequence->p.scope);
f6625916 207 declaration_unref(sequence->p.declaration);
c054553d
MD
208 g_free(sequence);
209}
0f980a35 210
3838df27
MD
211uint64_t sequence_len(struct definition_sequence *sequence)
212{
98df1c9f 213 return sequence->length->value._unsigned;
3838df27
MD
214}
215
0f980a35
MD
216struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
217{
81dee1bb
MD
218 if (!sequence->elems)
219 return NULL;
98df1c9f 220 if (i >= sequence->length->value._unsigned)
0f980a35
MD
221 return NULL;
222 assert(i < sequence->elems->len);
b1a2f580 223 return g_ptr_array_index(sequence->elems, i);
0f980a35 224}
This page took 0.033382 seconds and 4 git commands to generate.