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