Create copy of integer declaration before applying base-16 for pointers
[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
be85c1c7
MD
23#ifndef max
24#define max(a, b) ((a) < (b) ? (b) : (a))
25#endif
26
c054553d 27static
f6625916 28struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5
MD
29 struct definition_scope *parent_scope,
30 GQuark field_name, int index);
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
c5e74408
MD
43 ret = generic_rw(pos, &sequence_definition->len->p);
44 if (ret)
45 return ret;
d11e9c49 46 len = sequence_definition->len->value._unsigned;
0f980a35
MD
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 */
d11e9c49 55 oldlen = sequence_definition->elems->len;
0f980a35 56 if (oldlen < len)
b1a2f580 57 g_ptr_array_set_size(sequence_definition->elems, len);
0f980a35
MD
58
59 for (i = oldlen; i < len; i++) {
b1a2f580 60 struct definition **field;
0f980a35
MD
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);
2e7d72cf 68
b1a2f580
MD
69 field = (struct definition **) &g_ptr_array_index(sequence_definition->elems, i);
70 *field = sequence_declaration->elem->definition_new(sequence_declaration->elem,
d11e9c49 71 sequence_definition->scope,
0f980a35 72 name, i);
b1a2f580 73 ret = generic_rw(pos, *field);
c5e74408
MD
74 if (ret)
75 return ret;
2e7d72cf 76 }
c5e74408 77 return 0;
2e7d72cf
MD
78}
79
c054553d 80static
f6625916 81void _sequence_declaration_free(struct declaration *declaration)
2e7d72cf 82{
f6625916
MD
83 struct declaration_sequence *sequence_declaration =
84 container_of(declaration, struct declaration_sequence, p);
c054553d 85
f6625916
MD
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);
2e7d72cf
MD
90}
91
f6625916 92struct declaration_sequence *
ab4cf058 93 sequence_declaration_new(struct declaration_integer *len_declaration,
f6625916
MD
94 struct declaration *elem_declaration,
95 struct declaration_scope *parent_scope)
2e7d72cf 96{
f6625916
MD
97 struct declaration_sequence *sequence_declaration;
98 struct declaration *declaration;
2e7d72cf 99
f6625916
MD
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;
f6625916 109 declaration->alignment = max(len_declaration->p.alignment, elem_declaration->alignment);
f6625916
MD
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;
2e7d72cf 115}
c054553d
MD
116
117static
f6625916 118struct definition *_sequence_definition_new(struct declaration *declaration,
05c749e5
MD
119 struct definition_scope *parent_scope,
120 GQuark field_name, int index)
c054553d 121{
f6625916
MD
122 struct declaration_sequence *sequence_declaration =
123 container_of(declaration, struct declaration_sequence, p);
e1151715
MD
124 struct definition_sequence *sequence;
125 struct definition *len_parent;
c054553d 126
e1151715 127 sequence = g_new(struct definition_sequence, 1);
f6625916
MD
128 declaration_ref(&sequence_declaration->p);
129 sequence->p.declaration = declaration;
130 sequence->declaration = sequence_declaration;
c054553d 131 sequence->p.ref = 1;
05c749e5 132 sequence->p.index = index;
b1a2f580 133 sequence->p.name = field_name;
31262354 134 sequence->p.path = new_definition_path(parent_scope, field_name);
05c749e5 135 sequence->scope = new_definition_scope(parent_scope, field_name);
f6625916 136 len_parent = sequence_declaration->len_declaration->p.definition_new(&sequence_declaration->len_declaration->p,
0f980a35 137 sequence->scope,
05c749e5 138 g_quark_from_static_string("length"), 0);
e19c3d69 139 sequence->len =
e1151715 140 container_of(len_parent, struct definition_integer, p);
b1a2f580 141 sequence->elems = g_ptr_array_new();
c054553d
MD
142 return &sequence->p;
143}
144
145static
e1151715 146void _sequence_definition_free(struct definition *definition)
c054553d 147{
e1151715
MD
148 struct definition_sequence *sequence =
149 container_of(definition, struct definition_sequence, p);
150 struct definition *len_definition = &sequence->len->p;
0f980a35
MD
151 uint64_t i;
152
153 for (i = 0; i < sequence->elems->len; i++) {
b1a2f580 154 struct definition *field;
c054553d 155
b1a2f580
MD
156 field = g_ptr_array_index(sequence->elems, i);
157 field->declaration->definition_free(field);
0f980a35 158 }
b1a2f580 159 (void) g_ptr_array_free(sequence->elems, TRUE);
f6625916 160 len_definition->declaration->definition_free(len_definition);
e1151715 161 free_definition_scope(sequence->scope);
f6625916 162 declaration_unref(sequence->p.declaration);
c054553d
MD
163 g_free(sequence);
164}
0f980a35 165
3838df27
MD
166uint64_t sequence_len(struct definition_sequence *sequence)
167{
168 return sequence->len->value._unsigned;
169}
170
0f980a35
MD
171struct 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);
b1a2f580 176 return g_ptr_array_index(sequence->elems, i);
0f980a35 177}
This page took 0.030994 seconds and 4 git commands to generate.