f4262cdb99b595a8119dccd2c59bab379c1d57dd
[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 int 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 int ret;
42
43 ret = generic_rw(pos, &sequence_definition->len->p);
44 if (ret)
45 return ret;
46 len = sequence_definition->len->value._unsigned;
47 /*
48 * Yes, large sequences could be _painfully slow_ to parse due
49 * to memory allocation for each event read. At least, never
50 * shrink the sequence. Note: the sequence GArray len should
51 * never be used as indicator of the current sequence length.
52 * One should always look at the sequence->len->value._unsigned
53 * value for that.
54 */
55 oldlen = sequence_definition->elems->len;
56 if (oldlen < len)
57 g_ptr_array_set_size(sequence_definition->elems, len);
58
59 for (i = oldlen; i < len; i++) {
60 struct definition **field;
61 GString *str;
62 GQuark name;
63
64 str = g_string_new("");
65 g_string_printf(str, "[%" PRIu64 "]", i);
66 (void) g_string_free(str, TRUE);
67 name = g_quark_from_string(str->str);
68
69 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
70 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
71 sequence_definition->scope,
72 name, i);
73 ret = generic_rw(pos, *field);
74 if (ret)
75 return ret;
76 }
77 return 0;
78 }
79
80 static
81 void _sequence_declaration_free(struct declaration *declaration)
82 {
83 struct declaration_sequence *sequence_declaration =
84 container_of(declaration, struct declaration_sequence, p);
85
86 free_declaration_scope(sequence_declaration->scope);
87 declaration_unref(&sequence_declaration->len_declaration->p);
88 declaration_unref(sequence_declaration->elem);
89 g_free(sequence_declaration);
90 }
91
92 struct declaration_sequence *
93 sequence_declaration_new(struct declaration_integer *len_declaration,
94 struct declaration *elem_declaration,
95 struct declaration_scope *parent_scope)
96 {
97 struct declaration_sequence *sequence_declaration;
98 struct declaration *declaration;
99
100 sequence_declaration = g_new(struct declaration_sequence, 1);
101 declaration = &sequence_declaration->p;
102 assert(!len_declaration->signedness);
103 declaration_ref(&len_declaration->p);
104 sequence_declaration->len_declaration = len_declaration;
105 declaration_ref(elem_declaration);
106 sequence_declaration->elem = elem_declaration;
107 sequence_declaration->scope = new_declaration_scope(parent_scope);
108 declaration->id = CTF_TYPE_SEQUENCE;
109 declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment);
110 declaration->declaration_free = _sequence_declaration_free;
111 declaration->definition_new = _sequence_definition_new;
112 declaration->definition_free = _sequence_definition_free;
113 declaration->ref = 1;
114 return sequence_declaration;
115 }
116
117 static
118 struct definition *_sequence_definition_new(struct declaration *declaration,
119 struct definition_scope *parent_scope,
120 GQuark field_name, int index)
121 {
122 struct declaration_sequence *sequence_declaration =
123 container_of(declaration, struct declaration_sequence, p);
124 struct definition_sequence *sequence;
125 struct definition *len_parent;
126
127 sequence = g_new(struct definition_sequence, 1);
128 declaration_ref(&sequence_declaration->p);
129 sequence->p.declaration = declaration;
130 sequence->declaration = sequence_declaration;
131 sequence->p.ref = 1;
132 sequence->p.index = index;
133 sequence->p.name = field_name;
134 sequence->p.path = new_definition_path(parent_scope, field_name);
135 sequence->scope = new_definition_scope(parent_scope, field_name);
136 len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p,
137 sequence->scope,
138 g_quark_from_static_string("length"), 0);
139 sequence->len =
140 container_of(len_parent, struct definition_integer, p);
141 sequence->elems = g_ptr_array_new();
142 return &sequence->p;
143 }
144
145 static
146 void _sequence_definition_free(struct definition *definition)
147 {
148 struct definition_sequence *sequence =
149 container_of(definition, struct definition_sequence, p);
150 struct definition *len_definition = &sequence->len->p;
151 uint64_t i;
152
153 for (i = 0; i < sequence->elems->len; i++) {
154 struct definition *field;
155
156 field = g_ptr_array_index(sequence->elems, i);
157 field->declaration->definition_free(field);
158 }
159 (void) g_ptr_array_free(sequence->elems, TRUE);
160 len_definition->declaration->definition_free(len_definition);
161 free_definition_scope(sequence->scope);
162 declaration_unref(sequence->p.declaration);
163 g_free(sequence);
164 }
165
166 uint64_t sequence_len(struct definition_sequence *sequence)
167 {
168 return sequence->len->value._unsigned;
169 }
170
171 struct definition *sequence_index(struct definition_sequence *sequence, uint64_t i)
172 {
173 if (i >= sequence->len->value._unsigned)
174 return NULL;
175 assert(i < sequence->elems->len);
176 return g_ptr_array_index(sequence->elems, i);
177 }
This page took 0.046509 seconds and 3 git commands to generate.