Fix -Wmissing-prototypes/-Wmissing-declarations warnings
[babeltrace.git] / src / ctf-writer / attributes.c
1 /*
2 * attributes.c
3 *
4 * Babeltrace CTF writer - Attributes
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #define BT_LOG_TAG "CTF-WRITER/ATTRS"
29 #include "logging.h"
30
31 #include "attributes.h"
32
33 #include "common/assert.h"
34 #include "common/macros.h"
35 #include "compat/string.h"
36 #include <babeltrace2-ctf-writer/object.h>
37 #include <inttypes.h>
38
39 #include "values.h"
40
41 #define BT_CTF_ATTR_NAME_INDEX 0
42 #define BT_CTF_ATTR_VALUE_INDEX 1
43
44 BT_HIDDEN
45 struct bt_ctf_private_value *bt_ctf_attributes_create(void)
46 {
47 struct bt_ctf_private_value *attr_obj;
48
49 /*
50 * Attributes: array value object of array value objects, each one
51 * containing two entries: a string value object (attributes
52 * field name), and a value object (attributes field value).
53 *
54 * Example (JSON representation):
55 *
56 * [
57 * ["hostname", "eeppdesk"],
58 * ["sysname", "Linux"],
59 * ["tracer_major", 2],
60 * ["tracer_minor", 5]
61 * ]
62 */
63 BT_LOGD_STR("Creating attributes object.");
64 attr_obj = bt_ctf_private_value_array_create();
65 if (!attr_obj) {
66 BT_LOGE_STR("Failed to create array value.");
67 } else {
68 BT_LOGD("Created attributes object: addr=%p",
69 attr_obj);
70 }
71
72 return attr_obj;
73 }
74
75 BT_HIDDEN
76 void bt_ctf_attributes_destroy(struct bt_ctf_private_value *attr_obj)
77 {
78 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
79 bt_ctf_object_put_ref(attr_obj);
80 }
81
82 BT_HIDDEN
83 int64_t bt_ctf_attributes_get_count(struct bt_ctf_private_value *attr_obj)
84 {
85 return bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
86 }
87
88 BT_HIDDEN
89 const char *bt_ctf_attributes_get_field_name(struct bt_ctf_private_value *attr_obj,
90 uint64_t index)
91 {
92 const char *ret = NULL;
93 struct bt_ctf_private_value *attr_field_obj = NULL;
94 struct bt_ctf_private_value *attr_field_name_obj = NULL;
95
96 if (!attr_obj) {
97 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
98 goto end;
99 }
100
101 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
102 BT_LOGW("Invalid parameter: index is out of bounds: "
103 "index=%" PRIu64 ", count=%" PRId64,
104 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
105 goto end;
106 }
107
108 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
109 attr_obj, index);
110 if (!attr_field_obj) {
111 BT_LOGE("Cannot get attributes object's array value's element by index: "
112 "value-addr=%p, index=%" PRIu64, attr_obj, index);
113 goto end;
114 }
115
116 attr_field_name_obj =
117 bt_ctf_private_value_array_borrow_element_by_index(
118 attr_field_obj, BT_CTF_ATTR_NAME_INDEX);
119 if (!attr_field_name_obj) {
120 BT_LOGE("Cannot get attribute array value's element by index: "
121 "value-addr=%p, index=%" PRIu64, attr_field_obj,
122 (uint64_t) BT_CTF_ATTR_NAME_INDEX);
123 goto end;
124 }
125
126 ret = bt_ctf_value_string_get(
127 bt_ctf_private_value_as_value(attr_field_name_obj));
128
129 end:
130 return ret;
131 }
132
133 BT_HIDDEN
134 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value(struct bt_ctf_private_value *attr_obj,
135 uint64_t index)
136 {
137 struct bt_ctf_private_value *value_obj = NULL;
138 struct bt_ctf_private_value *attr_field_obj = NULL;
139
140 if (!attr_obj) {
141 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
142 goto end;
143 }
144
145 if (index >= bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj))) {
146 BT_LOGW("Invalid parameter: index is out of bounds: "
147 "index=%" PRIu64 ", count=%" PRId64,
148 index, bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj)));
149 goto end;
150 }
151
152 attr_field_obj = bt_ctf_private_value_array_borrow_element_by_index(
153 attr_obj, index);
154 if (!attr_field_obj) {
155 BT_LOGE("Cannot get attributes object's array value's element by index: "
156 "value-addr=%p, index=%" PRIu64, attr_obj, index);
157 goto end;
158 }
159
160 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
161 BT_CTF_ATTR_VALUE_INDEX);
162 if (!value_obj) {
163 BT_LOGE("Cannot get attribute array value's element by index: "
164 "value-addr=%p, index=%" PRIu64, attr_field_obj,
165 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
166 }
167
168 end:
169 return value_obj;
170 }
171
172 static
173 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_by_name(
174 struct bt_ctf_private_value *attr_obj, const char *name)
175 {
176 uint64_t i;
177 int64_t attr_size;
178 struct bt_ctf_private_value *value_obj = NULL;
179 struct bt_ctf_private_value *attr_field_name_obj = NULL;
180
181 attr_size = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
182 if (attr_size < 0) {
183 BT_LOGE("Cannot get array value's size: value-addr=%p",
184 attr_obj);
185 goto error;
186 }
187
188 for (i = 0; i < attr_size; ++i) {
189 const char *field_name;
190
191 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_obj, i);
192 if (!value_obj) {
193 BT_LOGE("Cannot get attributes object's array value's element by index: "
194 "value-addr=%p, index=%" PRIu64, attr_obj, i);
195 goto error;
196 }
197
198 attr_field_name_obj = bt_ctf_private_value_array_borrow_element_by_index(value_obj,
199 BT_CTF_ATTR_NAME_INDEX);
200 if (!attr_field_name_obj) {
201 BT_LOGE("Cannot get attribute array value's element by index: "
202 "value-addr=%p, index=%" PRIu64,
203 value_obj, (int64_t) BT_CTF_ATTR_NAME_INDEX);
204 goto error;
205 }
206
207 field_name = bt_ctf_value_string_get(
208 bt_ctf_private_value_as_value(attr_field_name_obj));
209
210 if (strcmp(field_name, name) == 0) {
211 break;
212 }
213
214 value_obj = NULL;
215 }
216
217 return value_obj;
218
219 error:
220 value_obj = NULL;
221 return value_obj;
222 }
223
224 BT_HIDDEN
225 int bt_ctf_attributes_set_field_value(struct bt_ctf_private_value *attr_obj,
226 const char *name, struct bt_ctf_private_value *value_obj)
227 {
228 int ret = 0;
229 struct bt_ctf_private_value *attr_field_obj = NULL;
230
231 if (!attr_obj || !name || !value_obj) {
232 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
233 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
234 attr_obj, name, value_obj);
235 ret = -1;
236 goto end;
237 }
238
239 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
240 if (attr_field_obj) {
241 ret = bt_ctf_private_value_array_set_element_by_index(
242 attr_field_obj, BT_CTF_ATTR_VALUE_INDEX,
243 bt_ctf_private_value_as_value(value_obj));
244 attr_field_obj = NULL;
245 goto end;
246 }
247
248 attr_field_obj = bt_ctf_private_value_array_create();
249 if (!attr_field_obj) {
250 BT_LOGE_STR("Failed to create empty array value.");
251 ret = -1;
252 goto end;
253 }
254
255 ret = bt_ctf_private_value_array_append_string_element(attr_field_obj, name);
256 ret |= bt_ctf_private_value_array_append_element(attr_field_obj,
257 bt_ctf_private_value_as_value(value_obj));
258 if (ret) {
259 BT_LOGE("Cannot append elements to array value: addr=%p",
260 attr_field_obj);
261 goto end;
262 }
263
264 ret = bt_ctf_private_value_array_append_element(attr_obj,
265 bt_ctf_private_value_as_value(attr_field_obj));
266 if (ret) {
267 BT_LOGE("Cannot append element to array value: "
268 "array-value-addr=%p, element-value-addr=%p",
269 attr_obj, attr_field_obj);
270 }
271
272 end:
273 bt_ctf_object_put_ref(attr_field_obj);
274 return ret;
275 }
276
277 BT_HIDDEN
278 struct bt_ctf_private_value *bt_ctf_attributes_borrow_field_value_by_name(
279 struct bt_ctf_private_value *attr_obj, const char *name)
280 {
281 struct bt_ctf_private_value *value_obj = NULL;
282 struct bt_ctf_private_value *attr_field_obj = NULL;
283
284 if (!attr_obj || !name) {
285 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
286 "value-addr=%p, name-addr=%p", attr_obj, name);
287 goto end;
288 }
289
290 attr_field_obj = bt_ctf_attributes_borrow_field_by_name(attr_obj, name);
291 if (!attr_field_obj) {
292 BT_LOGD("Cannot find attributes object's field by name: "
293 "value-addr=%p, name=\"%s\"", attr_obj, name);
294 goto end;
295 }
296
297 value_obj = bt_ctf_private_value_array_borrow_element_by_index(attr_field_obj,
298 BT_CTF_ATTR_VALUE_INDEX);
299 if (!value_obj) {
300 BT_LOGE("Cannot get attribute array value's element by index: "
301 "value-addr=%p, index=%" PRIu64, attr_field_obj,
302 (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
303 }
304
305 end:
306 return value_obj;
307 }
308
309 BT_HIDDEN
310 int bt_ctf_attributes_freeze(struct bt_ctf_private_value *attr_obj)
311 {
312 uint64_t i;
313 int64_t count;
314 int ret = 0;
315
316 if (!attr_obj) {
317 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
318 ret = -1;
319 goto end;
320 }
321
322 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
323 count = bt_ctf_value_array_get_length(bt_ctf_private_value_as_value(attr_obj));
324 BT_ASSERT_DBG(count >= 0);
325
326 /*
327 * We do not freeze the array value object itself here, since
328 * internal stuff could need to modify/add attributes. Each
329 * attribute is frozen one by one.
330 */
331 for (i = 0; i < count; ++i) {
332 struct bt_ctf_private_value *obj = NULL;
333
334 obj = bt_ctf_attributes_borrow_field_value(attr_obj, i);
335 if (!obj) {
336 BT_LOGE("Cannot get attributes object's field value by index: "
337 "value-addr=%p, index=%" PRIu64,
338 attr_obj, i);
339 ret = -1;
340 goto end;
341 }
342
343 bt_ctf_value_freeze(bt_ctf_private_value_as_value(obj));
344 }
345
346 end:
347 return ret;
348 }
This page took 0.036758 seconds and 4 git commands to generate.