Fix missing header
[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 #ifndef max
24 #define max(a, b) ((a) < (b) ? (b) : (a))
25 #endif
26
27 static
28 struct definition *_sequence_definition_new(struct declaration *declaration,
29 struct definition_scope *parent_scope,
30 GQuark field_name, int index);
31 static
32 void _sequence_definition_free(struct definition *definition);
33
34 void sequence_rw(struct stream_pos *pos, struct definition *definition)
35 {
36 struct definition_sequence *sequence_definition =
37 container_of(definition, struct definition_sequence, p);
38 const struct declaration_sequence *sequence_declaration =
39 sequence_definition->declaration;
40 uint64_t len, oldlen, i;
41
42 generic_rw(pos, &sequence_definition->len->p);
43 len = sequence_definition->len->value._unsigned;
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 */
52 oldlen = sequence_definition->elems->len;
53 if (oldlen < len)
54 g_array_set_size(sequence_definition->elems, len);
55
56 for (i = oldlen; i < len; i++) {
57 struct field *field;
58 GString *str;
59 GQuark name;
60
61 str = g_string_new("");
62 g_string_printf(str, "[%" PRIu64 "]", i);
63 (void) g_string_free(str, TRUE);
64 name = g_quark_from_string(str->str);
65
66 field = &g_array_index(sequence_definition->elems, struct field, i);
67 field->name = name;
68 field->definition = sequence_declaration->elem->definition_new(sequence_declaration->elem,
69 sequence_definition->scope,
70 name, i);
71 generic_rw(pos, field->definition);
72 }
73 }
74
75 static
76 void _sequence_declaration_free(struct declaration *declaration)
77 {
78 struct declaration_sequence *sequence_declaration =
79 container_of(declaration, struct declaration_sequence, p);
80
81 free_declaration_scope(sequence_declaration->scope);
82 declaration_unref(&sequence_declaration->len_declaration->p);
83 declaration_unref(sequence_declaration->elem);
84 g_free(sequence_declaration);
85 }
86
87 struct declaration_sequence *
88 sequence_declaration_new(struct declaration_integer *len_declaration,
89 struct declaration *elem_declaration,
90 struct declaration_scope *parent_scope)
91 {
92 struct declaration_sequence *sequence_declaration;
93 struct declaration *declaration;
94
95 sequence_declaration = g_new(struct declaration_sequence, 1);
96 declaration = &sequence_declaration->p;
97 assert(!len_declaration->signedness);
98 declaration_ref(&len_declaration->p);
99 sequence_declaration->len_declaration = len_declaration;
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 = max(len_declaration->p.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 {
117 struct declaration_sequence *sequence_declaration =
118 container_of(declaration, struct declaration_sequence, p);
119 struct definition_sequence *sequence;
120 struct definition *len_parent;
121
122 sequence = g_new(struct definition_sequence, 1);
123 declaration_ref(&sequence_declaration->p);
124 sequence->p.declaration = declaration;
125 sequence->declaration = sequence_declaration;
126 sequence->p.ref = 1;
127 sequence->p.index = index;
128 sequence->scope = new_definition_scope(parent_scope, field_name);
129 len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p,
130 sequence->scope,
131 g_quark_from_static_string("length"), 0);
132 sequence->len =
133 container_of(len_parent, struct definition_integer, p);
134 sequence->elems = g_array_new(FALSE, TRUE, sizeof(struct field));
135 return &sequence->p;
136 }
137
138 static
139 void _sequence_definition_free(struct definition *definition)
140 {
141 struct definition_sequence *sequence =
142 container_of(definition, struct definition_sequence, p);
143 struct definition *len_definition = &sequence->len->p;
144 uint64_t i;
145
146 for (i = 0; i < sequence->elems->len; i++) {
147 struct field *field;
148
149 field = &g_array_index(sequence->elems, struct field, i);
150 field->definition->declaration->definition_free(field->definition);
151 }
152 (void) g_array_free(sequence->elems, TRUE);
153 len_definition->declaration->definition_free(len_definition);
154 free_definition_scope(sequence->scope);
155 declaration_unref(sequence->p.declaration);
156 g_free(sequence);
157 }
158
159 uint64_t sequence_len(struct definition_sequence *sequence)
160 {
161 return sequence->len->value._unsigned;
162 }
163
164 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
165 {
166 if (i >= sequence->len->value._unsigned)
167 return NULL;
168 assert(i < sequence->elems->len);
169 return g_array_index(sequence->elems, struct field, i).definition;
170 }
This page took 0.031677 seconds and 4 git commands to generate.