lib: rename plural file names to singular
[babeltrace.git] / 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"
40547d22 25#include <babeltrace/lib-logging-internal.h>
0f5e83e5 26
65300d60 27#include <babeltrace/object.h>
44e0a4f5 28#include <babeltrace/babeltrace-internal.h>
c6bd8523
PP
29#include <babeltrace/value.h>
30#include <babeltrace/value-const.h>
31#include <babeltrace/value-internal.h>
05e21286 32#include <babeltrace/trace-ir/attributes-internal.h>
561ad8c1 33#include <inttypes.h>
ee389f01 34#include <babeltrace/compat/string-internal.h>
f6ccaed9 35#include <babeltrace/assert-internal.h>
44e0a4f5 36
50842bdc
PP
37#define BT_ATTR_NAME_INDEX 0
38#define BT_ATTR_VALUE_INDEX 1
44e0a4f5
JG
39
40BT_HIDDEN
05e21286 41struct bt_value *bt_attributes_create(void)
44e0a4f5 42{
05e21286 43 struct bt_value *attr_obj;
561ad8c1 44
44e0a4f5 45 /*
dac5c838
PP
46 * Attributes: array value object of array value objects, each one
47 * containing two entries: a string value object (attributes
48 * field name), and a value object (attributes field value).
44e0a4f5
JG
49 *
50 * Example (JSON representation):
51 *
52 * [
53 * ["hostname", "eeppdesk"],
54 * ["sysname", "Linux"],
55 * ["tracer_major", 2],
56 * ["tracer_minor", 5]
57 * ]
58 */
561ad8c1 59 BT_LOGD_STR("Creating attributes object.");
05e21286 60 attr_obj = bt_value_array_create();
561ad8c1
PP
61 if (!attr_obj) {
62 BT_LOGE_STR("Failed to create array value.");
63 } else {
64 BT_LOGD("Created attributes object: addr=%p",
65 attr_obj);
66 }
67
68 return attr_obj;
44e0a4f5
JG
69}
70
71BT_HIDDEN
05e21286 72void bt_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 73{
561ad8c1 74 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
238b7404 75 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
44e0a4f5
JG
76}
77
78BT_HIDDEN
05e21286 79int64_t bt_attributes_get_count(const struct bt_value *attr_obj)
44e0a4f5 80{
05e21286 81 return bt_value_array_get_size(attr_obj);
44e0a4f5
JG
82}
83
84BT_HIDDEN
05e21286 85const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
dcf0cc71 86 uint64_t index)
44e0a4f5 87{
44e0a4f5 88 const char *ret = NULL;
05e21286
PP
89 const struct bt_value *attr_field_obj = NULL;
90 const struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 91
561ad8c1
PP
92 if (!attr_obj) {
93 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
94 goto end;
95 }
96
05e21286 97 if (index >= bt_value_array_get_size(attr_obj)) {
f6ccaed9
PP
98 BT_LOGW("Invalid parameter: index is out of bounds: "
99 "index=%" PRIu64 ", count=%" PRId64,
05e21286 100 index, bt_value_array_get_size(attr_obj));
f6ccaed9
PP
101 goto end;
102 }
103
05e21286 104 attr_field_obj = bt_value_array_borrow_element_by_index_const(
da91b29a 105 attr_obj, index);
44e0a4f5 106 if (!attr_field_obj) {
561ad8c1
PP
107 BT_LOGE("Cannot get attributes object's array value's element by index: "
108 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
109 goto end;
110 }
111
da91b29a 112 attr_field_name_obj =
05e21286 113 bt_value_array_borrow_element_by_index_const(attr_field_obj,
da91b29a 114 BT_ATTR_NAME_INDEX);
44e0a4f5 115 if (!attr_field_name_obj) {
561ad8c1
PP
116 BT_LOGE("Cannot get attribute array value's element by index: "
117 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 118 (uint64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
119 goto end;
120 }
121
05e21286 122 ret = bt_value_string_get(attr_field_name_obj);
44e0a4f5
JG
123
124end:
44e0a4f5
JG
125 return ret;
126}
127
128BT_HIDDEN
05e21286
PP
129struct bt_value *bt_attributes_borrow_field_value(
130 struct bt_value *attr_obj, uint64_t index)
44e0a4f5 131{
05e21286
PP
132 struct bt_value *value_obj = NULL;
133 struct bt_value *attr_field_obj = NULL;
44e0a4f5 134
561ad8c1
PP
135 if (!attr_obj) {
136 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
137 goto end;
138 }
139
05e21286 140 if (index >= bt_value_array_get_size(attr_obj)) {
f6ccaed9
PP
141 BT_LOGW("Invalid parameter: index is out of bounds: "
142 "index=%" PRIu64 ", count=%" PRId64,
05e21286 143 index, bt_value_array_get_size(attr_obj));
f6ccaed9
PP
144 goto end;
145 }
146
da91b29a 147 attr_field_obj =
05e21286 148 bt_value_array_borrow_element_by_index(attr_obj, index);
44e0a4f5 149 if (!attr_field_obj) {
561ad8c1
PP
150 BT_LOGE("Cannot get attributes object's array value's element by index: "
151 "value-addr=%p, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
152 goto end;
153 }
154
05e21286 155 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 156 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1
PP
157 if (!value_obj) {
158 BT_LOGE("Cannot get attribute array value's element by index: "
159 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 160 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 161 }
44e0a4f5
JG
162
163end:
44e0a4f5
JG
164 return value_obj;
165}
166
167static
05e21286
PP
168struct bt_value *bt_attributes_borrow_field_by_name(
169 struct bt_value *attr_obj, const char *name)
44e0a4f5 170{
dcf0cc71
PP
171 uint64_t i;
172 int64_t attr_size;
05e21286
PP
173 struct bt_value *value_obj = NULL;
174 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 175
05e21286 176 attr_size = bt_value_array_get_size(attr_obj);
44e0a4f5 177 if (attr_size < 0) {
561ad8c1
PP
178 BT_LOGE("Cannot get array value's size: value-addr=%p",
179 attr_obj);
44e0a4f5
JG
180 goto error;
181 }
182
183 for (i = 0; i < attr_size; ++i) {
44e0a4f5
JG
184 const char *field_name;
185
05e21286 186 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 187 attr_obj, i);
44e0a4f5 188 if (!value_obj) {
561ad8c1
PP
189 BT_LOGE("Cannot get attributes object's array value's element by index: "
190 "value-addr=%p, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
191 goto error;
192 }
193
da91b29a 194 attr_field_name_obj =
05e21286 195 bt_value_array_borrow_element_by_index(
da91b29a 196 value_obj, BT_ATTR_NAME_INDEX);
44e0a4f5 197 if (!attr_field_name_obj) {
561ad8c1
PP
198 BT_LOGE("Cannot get attribute array value's element by index: "
199 "value-addr=%p, index=%" PRIu64,
094ff7c0 200 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
201 goto error;
202 }
203
05e21286 204 field_name = bt_value_string_get(attr_field_name_obj);
44e0a4f5
JG
205
206 if (!strcmp(field_name, name)) {
44e0a4f5
JG
207 break;
208 }
209
094ff7c0 210 value_obj = NULL;
44e0a4f5
JG
211 }
212
213 return value_obj;
214
215error:
094ff7c0 216 value_obj = NULL;
44e0a4f5
JG
217 return value_obj;
218}
219
220BT_HIDDEN
05e21286
PP
221int bt_attributes_set_field_value(struct bt_value *attr_obj,
222 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
223{
224 int ret = 0;
05e21286 225 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
226
227 if (!attr_obj || !name || !value_obj) {
561ad8c1
PP
228 BT_LOGW("Invalid parameter: attributes object, name, or value object is NULL: "
229 "attr-value-addr=%p, name-addr=%p, value-addr=%p",
230 attr_obj, name, value_obj);
44e0a4f5
JG
231 ret = -1;
232 goto end;
233 }
234
094ff7c0 235 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 236 if (attr_field_obj) {
05e21286 237 ret = bt_value_array_set_element_by_index(
da91b29a 238 attr_field_obj, BT_ATTR_VALUE_INDEX,
05e21286 239 value_obj);
094ff7c0 240 attr_field_obj = NULL;
44e0a4f5
JG
241 goto end;
242 }
243
05e21286 244 attr_field_obj = bt_value_array_create();
44e0a4f5 245 if (!attr_field_obj) {
561ad8c1 246 BT_LOGE_STR("Failed to create empty array value.");
44e0a4f5
JG
247 ret = -1;
248 goto end;
249 }
250
05e21286 251 ret = bt_value_array_append_string_element(attr_field_obj,
da91b29a 252 name);
05e21286
PP
253 ret |= bt_value_array_append_element(attr_field_obj,
254 value_obj);
44e0a4f5 255 if (ret) {
561ad8c1
PP
256 BT_LOGE("Cannot append elements to array value: addr=%p",
257 attr_field_obj);
44e0a4f5
JG
258 goto end;
259 }
260
05e21286
PP
261 ret = bt_value_array_append_element(attr_obj,
262 attr_field_obj);
561ad8c1
PP
263 if (ret) {
264 BT_LOGE("Cannot append element to array value: "
265 "array-value-addr=%p, element-value-addr=%p",
266 attr_obj, attr_field_obj);
267 }
44e0a4f5
JG
268
269end:
65300d60 270 bt_object_put_ref(attr_field_obj);
44e0a4f5
JG
271 return ret;
272}
273
274BT_HIDDEN
05e21286
PP
275struct bt_value *bt_attributes_borrow_field_value_by_name(
276 struct bt_value *attr_obj, const char *name)
44e0a4f5 277{
05e21286
PP
278 struct bt_value *value_obj = NULL;
279 struct bt_value *attr_field_obj = NULL;
44e0a4f5
JG
280
281 if (!attr_obj || !name) {
561ad8c1
PP
282 BT_LOGW("Invalid parameter: attributes object or name is NULL: "
283 "value-addr=%p, name-addr=%p", attr_obj, name);
44e0a4f5
JG
284 goto end;
285 }
286
094ff7c0 287 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 288 if (!attr_field_obj) {
561ad8c1
PP
289 BT_LOGD("Cannot find attributes object's field by name: "
290 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
291 goto end;
292 }
293
05e21286 294 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 295 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1
PP
296 if (!value_obj) {
297 BT_LOGE("Cannot get attribute array value's element by index: "
298 "value-addr=%p, index=%" PRIu64, attr_field_obj,
50842bdc 299 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 300 }
44e0a4f5
JG
301
302end:
44e0a4f5
JG
303 return value_obj;
304}
305
306BT_HIDDEN
05e21286 307int bt_attributes_freeze(const struct bt_value *attr_obj)
44e0a4f5 308{
dcf0cc71
PP
309 uint64_t i;
310 int64_t count;
44e0a4f5
JG
311 int ret = 0;
312
313 if (!attr_obj) {
561ad8c1 314 BT_LOGW_STR("Invalid parameter: attributes object is NULL.");
44e0a4f5
JG
315 ret = -1;
316 goto end;
317 }
318
561ad8c1 319 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
05e21286 320 count = bt_value_array_get_size(attr_obj);
f6ccaed9 321 BT_ASSERT(count >= 0);
44e0a4f5
JG
322
323 /*
dac5c838
PP
324 * We do not freeze the array value object itself here, since
325 * internal stuff could need to modify/add attributes. Each
326 * attribute is frozen one by one.
44e0a4f5
JG
327 */
328 for (i = 0; i < count; ++i) {
05e21286 329 struct bt_value *obj = NULL;
44e0a4f5 330
05e21286
PP
331 obj = bt_attributes_borrow_field_value(
332 (void *) attr_obj, i);
44e0a4f5 333 if (!obj) {
561ad8c1
PP
334 BT_LOGE("Cannot get attributes object's field value by index: "
335 "value-addr=%p, index=%" PRIu64,
336 attr_obj, i);
44e0a4f5
JG
337 ret = -1;
338 goto end;
339 }
340
05e21286 341 bt_value_freeze(obj);
44e0a4f5
JG
342 }
343
344end:
44e0a4f5
JG
345 return ret;
346}
This page took 0.062701 seconds and 4 git commands to generate.