Remove trace-collection.h from include_headers
[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>
3122e6f0 23#include <babeltrace/types.h>
0f980a35 24#include <inttypes.h>
2e7d72cf 25
c054553d 26static
f6625916 27struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 28 struct definition_scope *parent_scope,
98df1c9f
MD
29 GQuark field_name, int index,
30 const char *root_name);
c054553d 31static
e1151715 32void _sequence_definition_free(struct definition *definition);
c054553d 33
c5e74408 34int sequence_rw(struct stream_pos *pos, struct definition *definition)
2e7d72cf 35{
d11e9c49 36 struct definition_sequence *sequence_definition =
e1151715 37 container_of(definition, struct definition_sequence, p);
d11e9c49
MD
38 const struct declaration_sequence *sequence_declaration =
39 sequence_definition->declaration;
0f980a35 40 uint64_t len, oldlen, i;
c5e74408 41 int ret;
2e7d72cf 42
98df1c9f 43 len = sequence_definition->length->value._unsigned;
0f980a35
MD
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 */
d11e9c49 52 oldlen = sequence_definition->elems->len;
0f980a35 53 if (oldlen < len)
b1a2f580 54 g_ptr_array_set_size(sequence_definition->elems, len);
0f980a35
MD
55
56 for (i = oldlen; i < len; i++) {
b1a2f580 57 struct definition **field;
0f980a35
MD
58 GString *str;
59 GQuark name;
60
61 str = g_string_new("");
62 g_string_printf(str, "[%" PRIu64 "]", i);
0f980a35 63 name = g_quark_from_string(str->str);
518de31c 64 (void) g_string_free(str, TRUE);
2e7d72cf 65
b1a2f580
MD
66 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
67 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
a35173fe 68 sequence_definition->p.scope,
98df1c9f 69 name, i, NULL);
1e36e6c2
MD
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);
b1a2f580 75 ret = generic_rw(pos, *field);
c5e74408
MD
76 if (ret)
77 return ret;
2e7d72cf 78 }
c5e74408 79 return 0;
2e7d72cf
MD
80}
81
c054553d 82static
f6625916 83void _sequence_declaration_free(struct declaration *declaration)
2e7d72cf 84{
f6625916
MD
85 struct declaration_sequence *sequence_declaration =
86 container_of(declaration, struct declaration_sequence, p);
c054553d 87
f6625916 88 free_declaration_scope(sequence_declaration->scope);
98df1c9f 89 g_array_free(sequence_declaration->length_name, TRUE);
f6625916
MD
90 declaration_unref(sequence_declaration->elem);
91 g_free(sequence_declaration);
2e7d72cf
MD
92}
93
f6625916 94struct declaration_sequence *
98df1c9f 95 sequence_declaration_new(const char *length,
f6625916
MD
96 struct declaration *elem_declaration,
97 struct declaration_scope *parent_scope)
2e7d72cf 98{
f6625916
MD
99 struct declaration_sequence *sequence_declaration;
100 struct declaration *declaration;
2e7d72cf 101
f6625916
MD
102 sequence_declaration = g_new(struct declaration_sequence, 1);
103 declaration = &sequence_declaration->p;
98df1c9f
MD
104
105 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
106 append_scope_path(length, sequence_declaration->length_name);
107
f6625916
MD
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;
98df1c9f 112 declaration->alignment = elem_declaration->alignment;
f6625916
MD
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;
2e7d72cf 118}
c054553d
MD
119
120static
f6625916 121struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 122 struct definition_scope *parent_scope,
98df1c9f
MD
123 GQuark field_name, int index,
124 const char *root_name)
c054553d 125{
f6625916
MD
126 struct declaration_sequence *sequence_declaration =
127 container_of(declaration, struct declaration_sequence, p);
e1151715
MD
128 struct definition_sequence *sequence;
129 struct definition *len_parent;
98df1c9f 130 int ret;
c054553d 131
e1151715 132 sequence = g_new(struct definition_sequence, 1);
f6625916
MD
133 declaration_ref(&sequence_declaration->p);
134 sequence->p.declaration = declaration;
135 sequence->declaration = sequence_declaration;
c054553d 136 sequence->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 142 sequence->p.name = field_name;
98df1c9f 143 sequence->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 144 sequence->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
145 ret = register_field_definition(field_name, &sequence->p,
146 parent_scope);
147 assert(!ret);
a35173fe
MD
148 len_parent = lookup_path_definition(sequence->p.scope->scope_path,
149 sequence_declaration->length_name,
150 parent_scope);
98df1c9f
MD
151 if (!len_parent) {
152 printf("[error] Lookup for sequence length field failed.\n");
153 goto error;
154 }
155 sequence->length =
e1151715 156 container_of(len_parent, struct definition_integer, p);
98df1c9f
MD
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);
81dee1bb
MD
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
b1a2f580 182 sequence->elems = g_ptr_array_new();
c054553d 183 return &sequence->p;
98df1c9f
MD
184
185error:
a35173fe 186 free_definition_scope(sequence->p.scope);
98df1c9f
MD
187 declaration_unref(&sequence_declaration->p);
188 g_free(sequence);
189 return NULL;
c054553d
MD
190}
191
192static
e1151715 193void _sequence_definition_free(struct definition *definition)
c054553d 194{
e1151715
MD
195 struct definition_sequence *sequence =
196 container_of(definition, struct definition_sequence, p);
98df1c9f 197 struct definition *len_definition = &sequence->length->p;
0f980a35
MD
198 uint64_t i;
199
81dee1bb
MD
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;
c054553d 205
81dee1bb
MD
206 field = g_ptr_array_index(sequence->elems, i);
207 field->declaration->definition_free(field);
208 }
0f980a35 209 }
b1a2f580 210 (void) g_ptr_array_free(sequence->elems, TRUE);
98df1c9f 211 definition_unref(len_definition);
a35173fe 212 free_definition_scope(sequence->p.scope);
f6625916 213 declaration_unref(sequence->p.declaration);
c054553d
MD
214 g_free(sequence);
215}
0f980a35 216
3838df27
MD
217uint64_t sequence_len(struct definition_sequence *sequence)
218{
98df1c9f 219 return sequence->length->value._unsigned;
3838df27
MD
220}
221
0f980a35
MD
222struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
223{
81dee1bb
MD
224 if (!sequence->elems)
225 return NULL;
98df1c9f 226 if (i >= sequence->length->value._unsigned)
0f980a35
MD
227 return NULL;
228 assert(i < sequence->elems->len);
b1a2f580 229 return g_ptr_array_index(sequence->elems, i);
0f980a35 230}
This page took 0.055772 seconds and 4 git commands to generate.