Fix multi-packet packet-based metadata
[babeltrace.git] / types / sequence.c
CommitLineData
2e7d72cf 1/*
ccd7e1c8 2 * sequence.c
2e7d72cf 3 *
ccd7e1c8 4 * BabelTrace - Sequence Type Converter
2e7d72cf 5 *
c054553d 6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2e7d72cf 7 *
ccd7e1c8
MD
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:
2e7d72cf 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
2e7d72cf
MD
17 */
18
19#include <babeltrace/compiler.h>
4c8bfb7e 20#include <babeltrace/format.h>
0f980a35 21#include <inttypes.h>
2e7d72cf 22
c054553d 23static
f6625916 24struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 25 struct definition_scope *parent_scope,
98df1c9f
MD
26 GQuark field_name, int index,
27 const char *root_name);
c054553d 28static
e1151715 29void _sequence_definition_free(struct definition *definition);
c054553d 30
c5e74408 31int sequence_rw(struct stream_pos *pos, struct definition *definition)
2e7d72cf 32{
d11e9c49 33 struct definition_sequence *sequence_definition =
e1151715 34 container_of(definition, struct definition_sequence, p);
d11e9c49
MD
35 const struct declaration_sequence *sequence_declaration =
36 sequence_definition->declaration;
0f980a35 37 uint64_t len, oldlen, i;
c5e74408 38 int ret;
2e7d72cf 39
98df1c9f 40 len = sequence_definition->length->value._unsigned;
0f980a35
MD
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 */
d11e9c49 49 oldlen = sequence_definition->elems->len;
0f980a35 50 if (oldlen < len)
b1a2f580 51 g_ptr_array_set_size(sequence_definition->elems, len);
0f980a35
MD
52
53 for (i = oldlen; i < len; i++) {
b1a2f580 54 struct definition **field;
0f980a35
MD
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);
2e7d72cf 62
b1a2f580
MD
63 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
64 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
d11e9c49 65 sequence_definition->scope,
98df1c9f 66 name, i, NULL);
b1a2f580 67 ret = generic_rw(pos, *field);
c5e74408
MD
68 if (ret)
69 return ret;
2e7d72cf 70 }
c5e74408 71 return 0;
2e7d72cf
MD
72}
73
c054553d 74static
f6625916 75void _sequence_declaration_free(struct declaration *declaration)
2e7d72cf 76{
f6625916
MD
77 struct declaration_sequence *sequence_declaration =
78 container_of(declaration, struct declaration_sequence, p);
c054553d 79
f6625916 80 free_declaration_scope(sequence_declaration->scope);
98df1c9f 81 g_array_free(sequence_declaration->length_name, TRUE);
f6625916
MD
82 declaration_unref(sequence_declaration->elem);
83 g_free(sequence_declaration);
2e7d72cf
MD
84}
85
f6625916 86struct declaration_sequence *
98df1c9f 87 sequence_declaration_new(const char *length,
f6625916
MD
88 struct declaration *elem_declaration,
89 struct declaration_scope *parent_scope)
2e7d72cf 90{
f6625916
MD
91 struct declaration_sequence *sequence_declaration;
92 struct declaration *declaration;
2e7d72cf 93
f6625916
MD
94 sequence_declaration = g_new(struct declaration_sequence, 1);
95 declaration = &sequence_declaration->p;
98df1c9f
MD
96
97 sequence_declaration->length_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
98 append_scope_path(length, sequence_declaration->length_name);
99
f6625916
MD
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;
98df1c9f 104 declaration->alignment = elem_declaration->alignment;
f6625916
MD
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;
2e7d72cf 110}
c054553d
MD
111
112static
f6625916 113struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5 114 struct definition_scope *parent_scope,
98df1c9f
MD
115 GQuark field_name, int index,
116 const char *root_name)
c054553d 117{
f6625916
MD
118 struct declaration_sequence *sequence_declaration =
119 container_of(declaration, struct declaration_sequence, p);
e1151715
MD
120 struct definition_sequence *sequence;
121 struct definition *len_parent;
98df1c9f 122 int ret;
c054553d 123
e1151715 124 sequence = g_new(struct definition_sequence, 1);
f6625916
MD
125 declaration_ref(&sequence_declaration->p);
126 sequence->p.declaration = declaration;
127 sequence->declaration = sequence_declaration;
c054553d 128 sequence->p.ref = 1;
98df1c9f
MD
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;
b1a2f580 134 sequence->p.name = field_name;
98df1c9f
MD
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 =
e1151715 148 container_of(len_parent, struct definition_integer, p);
98df1c9f
MD
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);
b1a2f580 154 sequence->elems = g_ptr_array_new();
c054553d 155 return &sequence->p;
98df1c9f
MD
156
157error:
158 free_definition_scope(sequence->scope);
159 declaration_unref(&sequence_declaration->p);
160 g_free(sequence);
161 return NULL;
c054553d
MD
162}
163
164static
e1151715 165void _sequence_definition_free(struct definition *definition)
c054553d 166{
e1151715
MD
167 struct definition_sequence *sequence =
168 container_of(definition, struct definition_sequence, p);
98df1c9f 169 struct definition *len_definition = &sequence->length->p;
0f980a35
MD
170 uint64_t i;
171
172 for (i = 0; i < sequence->elems->len; i++) {
b1a2f580 173 struct definition *field;
c054553d 174
b1a2f580
MD
175 field = g_ptr_array_index(sequence->elems, i);
176 field->declaration->definition_free(field);
0f980a35 177 }
b1a2f580 178 (void) g_ptr_array_free(sequence->elems, TRUE);
98df1c9f 179 definition_unref(len_definition);
e1151715 180 free_definition_scope(sequence->scope);
f6625916 181 declaration_unref(sequence->p.declaration);
c054553d
MD
182 g_free(sequence);
183}
0f980a35 184
3838df27
MD
185uint64_t sequence_len(struct definition_sequence *sequence)
186{
98df1c9f 187 return sequence->length->value._unsigned;
3838df27
MD
188}
189
0f980a35
MD
190struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
191{
98df1c9f 192 if (i >= sequence->length->value._unsigned)
0f980a35
MD
193 return NULL;
194 assert(i < sequence->elems->len);
b1a2f580 195 return g_ptr_array_index(sequence->elems, i);
0f980a35 196}
This page took 0.032081 seconds and 4 git commands to generate.