Commit | Line | Data |
---|---|---|
d06d03db | 1 | /* |
ccd7e1c8 | 2 | * array.c |
d06d03db | 3 | * |
ccd7e1c8 | 4 | * BabelTrace - Array Type Converter |
d06d03db | 5 | * |
c054553d | 6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
d06d03db | 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: | |
d06d03db | 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. | |
d06d03db MD |
17 | */ |
18 | ||
19 | #include <babeltrace/compiler.h> | |
4c8bfb7e | 20 | #include <babeltrace/format.h> |
0f980a35 | 21 | #include <inttypes.h> |
d06d03db | 22 | |
c054553d | 23 | static |
f6625916 | 24 | struct definition *_array_definition_new(struct declaration *declaration, |
05c749e5 MD |
25 | struct definition_scope *parent_scope, |
26 | GQuark field_name, int index); | |
c054553d | 27 | static |
e1151715 | 28 | void _array_definition_free(struct definition *definition); |
c054553d | 29 | |
d11e9c49 | 30 | void array_rw(struct stream_pos *pos, struct definition *definition) |
d06d03db | 31 | { |
d11e9c49 | 32 | struct definition_array *array_definition = |
e1151715 | 33 | container_of(definition, struct definition_array, p); |
d11e9c49 MD |
34 | const struct declaration_array *array_declaration = |
35 | array_definition->declaration; | |
c054553d | 36 | uint64_t i; |
d06d03db | 37 | |
d11e9c49 | 38 | /* No need to align, because the first field will align itself. */ |
f6625916 | 39 | for (i = 0; i < array_declaration->len; i++) { |
0f980a35 | 40 | struct definition *elem = |
d11e9c49 MD |
41 | g_array_index(array_definition->elems, struct field, i).definition; |
42 | generic_rw(pos, elem); | |
d06d03db | 43 | } |
d06d03db MD |
44 | } |
45 | ||
c054553d | 46 | static |
f6625916 | 47 | void _array_declaration_free(struct declaration *declaration) |
d06d03db | 48 | { |
f6625916 MD |
49 | struct declaration_array *array_declaration = |
50 | container_of(declaration, struct declaration_array, p); | |
c054553d | 51 | |
f6625916 MD |
52 | free_declaration_scope(array_declaration->scope); |
53 | declaration_unref(array_declaration->elem); | |
54 | g_free(array_declaration); | |
d06d03db MD |
55 | } |
56 | ||
f6625916 | 57 | struct declaration_array * |
ab4cf058 | 58 | array_declaration_new(size_t len, |
f6625916 MD |
59 | struct declaration *elem_declaration, |
60 | struct declaration_scope *parent_scope) | |
d06d03db | 61 | { |
f6625916 MD |
62 | struct declaration_array *array_declaration; |
63 | struct declaration *declaration; | |
d06d03db | 64 | |
f6625916 MD |
65 | array_declaration = g_new(struct declaration_array, 1); |
66 | declaration = &array_declaration->p; | |
67 | array_declaration->len = len; | |
68 | declaration_ref(elem_declaration); | |
69 | array_declaration->elem = elem_declaration; | |
70 | array_declaration->scope = new_declaration_scope(parent_scope); | |
71 | declaration->id = CTF_TYPE_ARRAY; | |
d06d03db | 72 | /* No need to align the array, the first element will align itself */ |
f6625916 | 73 | declaration->alignment = 1; |
f6625916 MD |
74 | declaration->declaration_free = _array_declaration_free; |
75 | declaration->definition_new = _array_definition_new; | |
76 | declaration->definition_free = _array_definition_free; | |
77 | declaration->ref = 1; | |
78 | return array_declaration; | |
d06d03db | 79 | } |
c054553d MD |
80 | |
81 | static | |
e1151715 | 82 | struct definition * |
f6625916 | 83 | _array_definition_new(struct declaration *declaration, |
05c749e5 MD |
84 | struct definition_scope *parent_scope, |
85 | GQuark field_name, int index) | |
c054553d | 86 | { |
f6625916 MD |
87 | struct declaration_array *array_declaration = |
88 | container_of(declaration, struct declaration_array, p); | |
e1151715 | 89 | struct definition_array *array; |
0f980a35 | 90 | uint64_t i; |
c054553d | 91 | |
e1151715 | 92 | array = g_new(struct definition_array, 1); |
f6625916 MD |
93 | declaration_ref(&array_declaration->p); |
94 | array->p.declaration = declaration; | |
95 | array->declaration = array_declaration; | |
c054553d | 96 | array->p.ref = 1; |
05c749e5 MD |
97 | array->p.index = index; |
98 | array->scope = new_definition_scope(parent_scope, field_name); | |
0f980a35 MD |
99 | array->elems = g_array_sized_new(FALSE, TRUE, sizeof(struct field), |
100 | array_declaration->len); | |
101 | g_array_set_size(array->elems, array_declaration->len); | |
102 | for (i = 0; i < array_declaration->len; i++) { | |
103 | struct field *field; | |
104 | GString *str; | |
105 | GQuark name; | |
106 | ||
107 | str = g_string_new(""); | |
108 | g_string_printf(str, "[%" PRIu64 "]", i); | |
109 | name = g_quark_from_string(str->str); | |
110 | (void) g_string_free(str, TRUE); | |
111 | ||
112 | field = &g_array_index(array->elems, struct field, i); | |
113 | field->name = name; | |
114 | field->definition = array_declaration->elem->definition_new(array_declaration->elem, | |
115 | array->scope, | |
116 | name, i); | |
117 | } | |
c054553d MD |
118 | return &array->p; |
119 | } | |
120 | ||
121 | static | |
e1151715 | 122 | void _array_definition_free(struct definition *definition) |
c054553d | 123 | { |
e1151715 MD |
124 | struct definition_array *array = |
125 | container_of(definition, struct definition_array, p); | |
0f980a35 | 126 | uint64_t i; |
c054553d | 127 | |
0f980a35 MD |
128 | for (i = 0; i < array->elems->len; i++) { |
129 | struct field *field; | |
130 | ||
131 | field = &g_array_index(array->elems, struct field, i); | |
132 | field->definition->declaration->definition_free(field->definition); | |
133 | } | |
134 | (void) g_array_free(array->elems, TRUE); | |
e1151715 | 135 | free_definition_scope(array->scope); |
f6625916 | 136 | declaration_unref(array->p.declaration); |
c054553d MD |
137 | g_free(array); |
138 | } | |
0f980a35 | 139 | |
3838df27 MD |
140 | uint64_t array_len(struct definition_array *array) |
141 | { | |
142 | return array->elems->len; | |
143 | } | |
144 | ||
0f980a35 MD |
145 | struct definition *array_index(struct definition_array *array, uint64_t i) |
146 | { | |
147 | if (i >= array->elems->len) | |
148 | return NULL; | |
149 | return g_array_index(array->elems, struct field, i).definition; | |
150 | } |