0bb141ce33e14cf3ba2ca12858e734535f3856a4
[babeltrace.git] / types / array.c
1 /*
2 * array.c
3 *
4 * BabelTrace - Array 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 static
24 struct definition *_array_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
27 static
28 void _array_definition_free(struct definition *definition);
29
30 int array_rw(struct stream_pos *pos, struct definition *definition)
31 {
32 struct definition_array *array_definition =
33 container_of(definition, struct definition_array, p);
34 const struct declaration_array *array_declaration =
35 array_definition->declaration;
36 uint64_t i;
37 int ret;
38
39 /* No need to align, because the first field will align itself. */
40 for (i = 0; i < array_declaration->len; i++) {
41 struct definition *field =
42 g_ptr_array_index(array_definition->elems, i);
43 ret = generic_rw(pos, field);
44 if (ret)
45 return ret;
46 }
47 return 0;
48 }
49
50 static
51 void _array_declaration_free(struct declaration *declaration)
52 {
53 struct declaration_array *array_declaration =
54 container_of(declaration, struct declaration_array, p);
55
56 free_declaration_scope(array_declaration->scope);
57 declaration_unref(array_declaration->elem);
58 g_free(array_declaration);
59 }
60
61 struct declaration_array *
62 array_declaration_new(size_t len,
63 struct declaration *elem_declaration,
64 struct declaration_scope *parent_scope)
65 {
66 struct declaration_array *array_declaration;
67 struct declaration *declaration;
68
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;
76 /* No need to align the array, the first element will align itself */
77 declaration->alignment = 1;
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;
83 }
84
85 static
86 struct definition *
87 _array_definition_new(struct declaration *declaration,
88 struct definition_scope *parent_scope,
89 GQuark field_name, int index)
90 {
91 struct declaration_array *array_declaration =
92 container_of(declaration, struct declaration_array, p);
93 struct definition_array *array;
94 uint64_t i;
95
96 array = g_new(struct definition_array, 1);
97 declaration_ref(&array_declaration->p);
98 array->p.declaration = declaration;
99 array->declaration = array_declaration;
100 array->p.ref = 1;
101 array->p.index = index;
102 array->p.name = field_name;
103 array->p.path = new_definition_path(parent_scope, field_name);
104 array->scope = new_definition_scope(parent_scope, field_name);
105 array->elems = g_ptr_array_sized_new(array_declaration->len);
106 g_ptr_array_set_size(array->elems, array_declaration->len);
107 for (i = 0; i < array_declaration->len; i++) {
108 struct definition **field;
109 GString *str;
110 GQuark name;
111
112 str = g_string_new("");
113 g_string_printf(str, "[%" PRIu64 "]", i);
114 name = g_quark_from_string(str->str);
115 (void) g_string_free(str, TRUE);
116
117 field = (struct definition **) &g_ptr_array_index(array->elems, i);
118 *field = array_declaration->elem->definition_new(array_declaration->elem,
119 array->scope,
120 name, i);
121 }
122 return &array->p;
123 }
124
125 static
126 void _array_definition_free(struct definition *definition)
127 {
128 struct definition_array *array =
129 container_of(definition, struct definition_array, p);
130 uint64_t i;
131
132 for (i = 0; i < array->elems->len; i++) {
133 struct definition *field;
134
135 field = g_ptr_array_index(array->elems, i);
136 field->declaration->definition_free(field);
137 }
138 (void) g_ptr_array_free(array->elems, TRUE);
139 free_definition_scope(array->scope);
140 declaration_unref(array->p.declaration);
141 g_free(array);
142 }
143
144 uint64_t array_len(struct definition_array *array)
145 {
146 return array->elems->len;
147 }
148
149 struct definition *array_index(struct definition_array *array, uint64_t i)
150 {
151 if (i >= array->elems->len)
152 return NULL;
153 return g_ptr_array_index(array->elems, i);
154 }
This page took 0.046049 seconds and 3 git commands to generate.