Fix multi-packet packet-based metadata
[babeltrace.git] / types / array.c
CommitLineData
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 23static
f6625916 24struct definition *_array_definition_new(struct declaration *declaration,
05c749e5 25 struct definition_scope *parent_scope,
98df1c9f 26 GQuark field_name, int index, const char *root_name);
c054553d 27static
e1151715 28void _array_definition_free(struct definition *definition);
c054553d 29
c5e74408 30int 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;
c5e74408 37 int ret;
d06d03db 38
d11e9c49 39 /* No need to align, because the first field will align itself. */
f6625916 40 for (i = 0; i < array_declaration->len; i++) {
b1a2f580
MD
41 struct definition *field =
42 g_ptr_array_index(array_definition->elems, i);
43 ret = generic_rw(pos, field);
c5e74408
MD
44 if (ret)
45 return ret;
d06d03db 46 }
c5e74408 47 return 0;
d06d03db
MD
48}
49
c054553d 50static
f6625916 51void _array_declaration_free(struct declaration *declaration)
d06d03db 52{
f6625916
MD
53 struct declaration_array *array_declaration =
54 container_of(declaration, struct declaration_array, p);
c054553d 55
f6625916
MD
56 free_declaration_scope(array_declaration->scope);
57 declaration_unref(array_declaration->elem);
58 g_free(array_declaration);
d06d03db
MD
59}
60
f6625916 61struct declaration_array *
ab4cf058 62 array_declaration_new(size_t len,
f6625916
MD
63 struct declaration *elem_declaration,
64 struct declaration_scope *parent_scope)
d06d03db 65{
f6625916
MD
66 struct declaration_array *array_declaration;
67 struct declaration *declaration;
d06d03db 68
f6625916
MD
69 array_declaration = g_new(struct declaration_array, 1);
70 declaration = &array_declaration->p;
71 array_declaration->len = len;
72 declaration_ref(elem_declaration);
73 array_declaration->elem = elem_declaration;
74 array_declaration->scope = new_declaration_scope(parent_scope);
75 declaration->id = CTF_TYPE_ARRAY;
d06d03db 76 /* No need to align the array, the first element will align itself */
f6625916 77 declaration->alignment = 1;
f6625916
MD
78 declaration->declaration_free = _array_declaration_free;
79 declaration->definition_new = _array_definition_new;
80 declaration->definition_free = _array_definition_free;
81 declaration->ref = 1;
82 return array_declaration;
d06d03db 83}
c054553d
MD
84
85static
e1151715 86struct definition *
f6625916 87 _array_definition_new(struct declaration *declaration,
05c749e5 88 struct definition_scope *parent_scope,
98df1c9f 89 GQuark field_name, int index, const char *root_name)
c054553d 90{
f6625916
MD
91 struct declaration_array *array_declaration =
92 container_of(declaration, struct declaration_array, p);
e1151715 93 struct definition_array *array;
98df1c9f
MD
94 int ret;
95 int i;
c054553d 96
e1151715 97 array = g_new(struct definition_array, 1);
f6625916
MD
98 declaration_ref(&array_declaration->p);
99 array->p.declaration = declaration;
100 array->declaration = array_declaration;
c054553d 101 array->p.ref = 1;
98df1c9f
MD
102 /*
103 * Use INT_MAX order to ensure that all fields of the parent
104 * scope are seen as being prior to this scope.
105 */
106 array->p.index = root_name ? INT_MAX : index;
b1a2f580 107 array->p.name = field_name;
98df1c9f
MD
108 array->p.path = new_definition_path(parent_scope, field_name, root_name);
109 array->scope = new_definition_scope(parent_scope, field_name, root_name);
110 ret = register_field_definition(field_name, &array->p,
111 parent_scope);
112 assert(!ret);
b1a2f580
MD
113 array->elems = g_ptr_array_sized_new(array_declaration->len);
114 g_ptr_array_set_size(array->elems, array_declaration->len);
0f980a35 115 for (i = 0; i < array_declaration->len; i++) {
b1a2f580 116 struct definition **field;
0f980a35
MD
117 GString *str;
118 GQuark name;
119
120 str = g_string_new("");
98df1c9f 121 g_string_printf(str, "[%u]", (unsigned int) i);
0f980a35
MD
122 name = g_quark_from_string(str->str);
123 (void) g_string_free(str, TRUE);
124
b1a2f580
MD
125 field = (struct definition **) &g_ptr_array_index(array->elems, i);
126 *field = array_declaration->elem->definition_new(array_declaration->elem,
0f980a35 127 array->scope,
98df1c9f
MD
128 name, i, NULL);
129 if (!*field)
130 goto error;
0f980a35 131 }
c054553d 132 return &array->p;
98df1c9f
MD
133
134error:
135 for (i--; i >= 0; i--) {
136 struct definition *field;
137
138 field = g_ptr_array_index(array->elems, i);
139 field->declaration->definition_free(field);
140 }
141 (void) g_ptr_array_free(array->elems, TRUE);
142 free_definition_scope(array->scope);
143 declaration_unref(array->p.declaration);
144 g_free(array);
145 return NULL;
c054553d
MD
146}
147
148static
e1151715 149void _array_definition_free(struct definition *definition)
c054553d 150{
e1151715
MD
151 struct definition_array *array =
152 container_of(definition, struct definition_array, p);
0f980a35 153 uint64_t i;
c054553d 154
0f980a35 155 for (i = 0; i < array->elems->len; i++) {
b1a2f580 156 struct definition *field;
0f980a35 157
b1a2f580
MD
158 field = g_ptr_array_index(array->elems, i);
159 field->declaration->definition_free(field);
0f980a35 160 }
b1a2f580 161 (void) g_ptr_array_free(array->elems, TRUE);
e1151715 162 free_definition_scope(array->scope);
f6625916 163 declaration_unref(array->p.declaration);
c054553d
MD
164 g_free(array);
165}
0f980a35 166
3838df27
MD
167uint64_t array_len(struct definition_array *array)
168{
169 return array->elems->len;
170}
171
0f980a35
MD
172struct definition *array_index(struct definition_array *array, uint64_t i)
173{
174 if (i >= array->elems->len)
175 return NULL;
b1a2f580 176 return g_ptr_array_index(array->elems, i);
0f980a35 177}
This page took 0.05357 seconds and 4 git commands to generate.