Add shared copyright to EfficiOS Inc. and Linux Foundation
[babeltrace.git] / types / array.c
CommitLineData
d06d03db 1/*
ccd7e1c8 2 * array.c
d06d03db 3 *
ccd7e1c8 4 * BabelTrace - Array Type Converter
d06d03db 5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d06d03db 9 *
ccd7e1c8
MD
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
d06d03db 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
d06d03db
MD
19 */
20
21#include <babeltrace/compiler.h>
4c8bfb7e 22#include <babeltrace/format.h>
0f980a35 23#include <inttypes.h>
d06d03db 24
c054553d 25static
f6625916 26struct definition *_array_definition_new(struct declaration *declaration,
05c749e5 27 struct definition_scope *parent_scope,
98df1c9f 28 GQuark field_name, int index, const char *root_name);
c054553d 29static
e1151715 30void _array_definition_free(struct definition *definition);
c054553d 31
c5e74408 32int array_rw(struct stream_pos *pos, struct definition *definition)
d06d03db 33{
d11e9c49 34 struct definition_array *array_definition =
e1151715 35 container_of(definition, struct definition_array, p);
d11e9c49
MD
36 const struct declaration_array *array_declaration =
37 array_definition->declaration;
c054553d 38 uint64_t i;
c5e74408 39 int ret;
d06d03db 40
d11e9c49 41 /* No need to align, because the first field will align itself. */
f6625916 42 for (i = 0; i < array_declaration->len; i++) {
b1a2f580
MD
43 struct definition *field =
44 g_ptr_array_index(array_definition->elems, i);
45 ret = generic_rw(pos, field);
c5e74408
MD
46 if (ret)
47 return ret;
d06d03db 48 }
c5e74408 49 return 0;
d06d03db
MD
50}
51
c054553d 52static
f6625916 53void _array_declaration_free(struct declaration *declaration)
d06d03db 54{
f6625916
MD
55 struct declaration_array *array_declaration =
56 container_of(declaration, struct declaration_array, p);
c054553d 57
f6625916
MD
58 free_declaration_scope(array_declaration->scope);
59 declaration_unref(array_declaration->elem);
60 g_free(array_declaration);
d06d03db
MD
61}
62
f6625916 63struct declaration_array *
ab4cf058 64 array_declaration_new(size_t len,
f6625916
MD
65 struct declaration *elem_declaration,
66 struct declaration_scope *parent_scope)
d06d03db 67{
f6625916
MD
68 struct declaration_array *array_declaration;
69 struct declaration *declaration;
d06d03db 70
f6625916
MD
71 array_declaration = g_new(struct declaration_array, 1);
72 declaration = &array_declaration->p;
73 array_declaration->len = len;
74 declaration_ref(elem_declaration);
75 array_declaration->elem = elem_declaration;
76 array_declaration->scope = new_declaration_scope(parent_scope);
77 declaration->id = CTF_TYPE_ARRAY;
d06d03db 78 /* No need to align the array, the first element will align itself */
f6625916 79 declaration->alignment = 1;
f6625916
MD
80 declaration->declaration_free = _array_declaration_free;
81 declaration->definition_new = _array_definition_new;
82 declaration->definition_free = _array_definition_free;
83 declaration->ref = 1;
84 return array_declaration;
d06d03db 85}
c054553d
MD
86
87static
e1151715 88struct definition *
f6625916 89 _array_definition_new(struct declaration *declaration,
05c749e5 90 struct definition_scope *parent_scope,
98df1c9f 91 GQuark field_name, int index, const char *root_name)
c054553d 92{
f6625916
MD
93 struct declaration_array *array_declaration =
94 container_of(declaration, struct declaration_array, p);
e1151715 95 struct definition_array *array;
98df1c9f
MD
96 int ret;
97 int i;
c054553d 98
e1151715 99 array = g_new(struct definition_array, 1);
f6625916
MD
100 declaration_ref(&array_declaration->p);
101 array->p.declaration = declaration;
102 array->declaration = array_declaration;
c054553d 103 array->p.ref = 1;
98df1c9f
MD
104 /*
105 * Use INT_MAX order to ensure that all fields of the parent
106 * scope are seen as being prior to this scope.
107 */
108 array->p.index = root_name ? INT_MAX : index;
b1a2f580 109 array->p.name = field_name;
98df1c9f 110 array->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 111 array->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
112 ret = register_field_definition(field_name, &array->p,
113 parent_scope);
114 assert(!ret);
81dee1bb
MD
115 array->string = NULL;
116 array->elems = NULL;
117
118 if (array_declaration->elem->id == CTF_TYPE_INTEGER) {
119 struct declaration_integer *integer_declaration =
120 container_of(array_declaration->elem, struct declaration_integer, p);
121
122 if (integer_declaration->encoding == CTF_STRING_UTF8
123 || integer_declaration->encoding == CTF_STRING_ASCII) {
124
125 array->string = g_string_new("");
126
127 if (integer_declaration->len == CHAR_BIT
128 && integer_declaration->p.alignment == CHAR_BIT) {
129 return &array->p;
130 }
131 }
132 }
133
b1a2f580
MD
134 array->elems = g_ptr_array_sized_new(array_declaration->len);
135 g_ptr_array_set_size(array->elems, array_declaration->len);
0f980a35 136 for (i = 0; i < array_declaration->len; i++) {
b1a2f580 137 struct definition **field;
0f980a35
MD
138 GString *str;
139 GQuark name;
140
141 str = g_string_new("");
98df1c9f 142 g_string_printf(str, "[%u]", (unsigned int) i);
0f980a35
MD
143 name = g_quark_from_string(str->str);
144 (void) g_string_free(str, TRUE);
145
b1a2f580
MD
146 field = (struct definition **) &g_ptr_array_index(array->elems, i);
147 *field = array_declaration->elem->definition_new(array_declaration->elem,
a35173fe 148 array->p.scope,
98df1c9f
MD
149 name, i, NULL);
150 if (!*field)
151 goto error;
0f980a35 152 }
81dee1bb 153
c054553d 154 return &array->p;
98df1c9f
MD
155
156error:
157 for (i--; i >= 0; i--) {
158 struct definition *field;
159
160 field = g_ptr_array_index(array->elems, i);
161 field->declaration->definition_free(field);
162 }
163 (void) g_ptr_array_free(array->elems, TRUE);
a35173fe 164 free_definition_scope(array->p.scope);
98df1c9f
MD
165 declaration_unref(array->p.declaration);
166 g_free(array);
167 return NULL;
c054553d
MD
168}
169
170static
e1151715 171void _array_definition_free(struct definition *definition)
c054553d 172{
e1151715
MD
173 struct definition_array *array =
174 container_of(definition, struct definition_array, p);
0f980a35 175 uint64_t i;
c054553d 176
81dee1bb
MD
177 if (array->string)
178 (void) g_string_free(array->string, TRUE);
179 if (array->elems) {
180 for (i = 0; i < array->elems->len; i++) {
181 struct definition *field;
0f980a35 182
81dee1bb
MD
183 field = g_ptr_array_index(array->elems, i);
184 field->declaration->definition_free(field);
185 }
186 (void) g_ptr_array_free(array->elems, TRUE);
0f980a35 187 }
a35173fe 188 free_definition_scope(array->p.scope);
f6625916 189 declaration_unref(array->p.declaration);
c054553d
MD
190 g_free(array);
191}
0f980a35 192
3838df27
MD
193uint64_t array_len(struct definition_array *array)
194{
81dee1bb
MD
195 if (!array->elems)
196 return array->string->len;
3838df27
MD
197 return array->elems->len;
198}
199
0f980a35
MD
200struct definition *array_index(struct definition_array *array, uint64_t i)
201{
81dee1bb
MD
202 if (!array->elems)
203 return NULL;
0f980a35
MD
204 if (i >= array->elems->len)
205 return NULL;
b1a2f580 206 return g_ptr_array_index(array->elems, i);
0f980a35 207}
This page took 0.032294 seconds and 4 git commands to generate.