Fix: usage of `bt_value_array_get_length()`
[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
b03487ab 24#define BT_LOG_TAG "LIB/ATTRS"
1633ef46 25#include "lib/logging.h"
0f5e83e5 26
85e7137b 27#include "common/macros.h"
71c5da58 28#include <babeltrace2/value.h>
57952005
MJ
29#include "lib/assert-pre.h"
30#include "lib/object.h"
71c5da58 31#include <babeltrace2/value-const.h>
57952005
MJ
32#include "lib/value.h"
33#include "attributes.h"
561ad8c1 34#include <inttypes.h>
57952005
MJ
35#include "compat/string.h"
36#include "common/assert.h"
44e0a4f5 37
839d52a5
PP
38#define BT_ATTR_NAME_INDEX 0
39#define BT_ATTR_VALUE_INDEX 1
44e0a4f5
JG
40
41BT_HIDDEN
ce141536 42struct bt_value *bt_attributes_create(void)
44e0a4f5 43{
ce141536 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.");
ce141536 61 attr_obj = bt_value_array_create();
561ad8c1 62 if (!attr_obj) {
a8f90e5d 63 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
561ad8c1
PP
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
ce141536 73void bt_attributes_destroy(struct bt_value *attr_obj)
44e0a4f5 74{
561ad8c1 75 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
1248f5ea 76 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
44e0a4f5
JG
77}
78
79BT_HIDDEN
6e30bfda 80uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
44e0a4f5 81{
1270d0e9 82 return bt_value_array_get_length(attr_obj);
44e0a4f5
JG
83}
84
85BT_HIDDEN
ce141536 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;
ce141536
PP
90 const struct bt_value *attr_field_obj = NULL;
91 const struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 92
ec4a3354
PP
93 BT_ASSERT_DBG(attr_obj);
94 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
ce141536 95 attr_field_obj = bt_value_array_borrow_element_by_index_const(
17582c6d 96 attr_obj, index);
44e0a4f5 97 if (!attr_field_obj) {
a8f90e5d
PP
98 BT_LIB_LOGE_APPEND_CAUSE(
99 "Cannot borrow attributes object's array value's element by index: "
100 "%![value-]+v, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
101 goto end;
102 }
103
17582c6d 104 attr_field_name_obj =
ce141536 105 bt_value_array_borrow_element_by_index_const(attr_field_obj,
17582c6d 106 BT_ATTR_NAME_INDEX);
44e0a4f5 107 if (!attr_field_name_obj) {
a8f90e5d
PP
108 BT_LIB_LOGE_APPEND_CAUSE(
109 "Cannot get attribute array value's element by index: "
110 "%![value-]+v, index=%" PRIu64, attr_field_obj,
839d52a5 111 (uint64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
112 goto end;
113 }
114
ce141536 115 ret = bt_value_string_get(attr_field_name_obj);
44e0a4f5
JG
116
117end:
44e0a4f5
JG
118 return ret;
119}
120
121BT_HIDDEN
ce141536
PP
122struct bt_value *bt_attributes_borrow_field_value(
123 struct bt_value *attr_obj, uint64_t index)
44e0a4f5 124{
ce141536
PP
125 struct bt_value *value_obj = NULL;
126 struct bt_value *attr_field_obj = NULL;
44e0a4f5 127
ec4a3354
PP
128 BT_ASSERT_DBG(attr_obj);
129 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
17582c6d 130 attr_field_obj =
ce141536 131 bt_value_array_borrow_element_by_index(attr_obj, index);
44e0a4f5 132 if (!attr_field_obj) {
a8f90e5d
PP
133 BT_LIB_LOGE_APPEND_CAUSE(
134 "Cannot get attributes object's array value's element by index: "
135 "%![value-]+v, index=%" PRIu64, attr_obj, index);
44e0a4f5
JG
136 goto end;
137 }
138
ce141536 139 value_obj = bt_value_array_borrow_element_by_index(
17582c6d 140 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1 141 if (!value_obj) {
a8f90e5d
PP
142 BT_LIB_LOGE_APPEND_CAUSE(
143 "Cannot get attribute array value's element by index: "
144 "%![value-]+v, index=%" PRIu64, attr_field_obj,
839d52a5 145 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 146 }
44e0a4f5
JG
147
148end:
44e0a4f5
JG
149 return value_obj;
150}
151
152static
ce141536
PP
153struct bt_value *bt_attributes_borrow_field_by_name(
154 struct bt_value *attr_obj, const char *name)
44e0a4f5 155{
b602018b 156 uint64_t i, attr_size;
ce141536
PP
157 struct bt_value *value_obj = NULL;
158 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 159
1270d0e9 160 attr_size = bt_value_array_get_length(attr_obj);
44e0a4f5 161 for (i = 0; i < attr_size; ++i) {
44e0a4f5
JG
162 const char *field_name;
163
ce141536 164 value_obj = bt_value_array_borrow_element_by_index(
17582c6d 165 attr_obj, i);
44e0a4f5 166 if (!value_obj) {
a8f90e5d
PP
167 BT_LIB_LOGE_APPEND_CAUSE(
168 "Cannot get attributes object's array value's element by index: "
169 "%![value-]+v, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
170 goto error;
171 }
172
17582c6d 173 attr_field_name_obj =
ce141536 174 bt_value_array_borrow_element_by_index(
17582c6d 175 value_obj, BT_ATTR_NAME_INDEX);
44e0a4f5 176 if (!attr_field_name_obj) {
a8f90e5d
PP
177 BT_LIB_LOGE_APPEND_CAUSE(
178 "Cannot get attribute array value's element by index: "
179 "%![value-]+v, index=%" PRIu64,
5fe68922 180 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
181 goto error;
182 }
183
ce141536 184 field_name = bt_value_string_get(attr_field_name_obj);
44e0a4f5 185
acfa8112 186 if (strcmp(field_name, name) == 0) {
44e0a4f5
JG
187 break;
188 }
189
5fe68922 190 value_obj = NULL;
44e0a4f5
JG
191 }
192
193 return value_obj;
194
195error:
5fe68922 196 value_obj = NULL;
44e0a4f5
JG
197 return value_obj;
198}
199
200BT_HIDDEN
ce141536
PP
201int bt_attributes_set_field_value(struct bt_value *attr_obj,
202 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
203{
204 int ret = 0;
ce141536 205 struct bt_value *attr_field_obj = NULL;
44e0a4f5 206
a8f90e5d
PP
207 BT_ASSERT(attr_obj);
208 BT_ASSERT(name);
209 BT_ASSERT(value_obj);
5fe68922 210 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 211 if (attr_field_obj) {
ce141536 212 ret = bt_value_array_set_element_by_index(
17582c6d 213 attr_field_obj, BT_ATTR_VALUE_INDEX,
ce141536 214 value_obj);
5fe68922 215 attr_field_obj = NULL;
44e0a4f5
JG
216 goto end;
217 }
218
ce141536 219 attr_field_obj = bt_value_array_create();
44e0a4f5 220 if (!attr_field_obj) {
a8f90e5d 221 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
44e0a4f5
JG
222 ret = -1;
223 goto end;
224 }
225
ce141536 226 ret = bt_value_array_append_string_element(attr_field_obj,
17582c6d 227 name);
ce141536
PP
228 ret |= bt_value_array_append_element(attr_field_obj,
229 value_obj);
44e0a4f5 230 if (ret) {
a8f90e5d
PP
231 BT_LIB_LOGE_APPEND_CAUSE(
232 "Cannot append elements to array value: %!+v",
561ad8c1 233 attr_field_obj);
44e0a4f5
JG
234 goto end;
235 }
236
ce141536
PP
237 ret = bt_value_array_append_element(attr_obj,
238 attr_field_obj);
561ad8c1 239 if (ret) {
a8f90e5d
PP
240 BT_LIB_LOGE_APPEND_CAUSE(
241 "Cannot append element to array value: "
242 "%![array-value-]+v, %![element-value-]+v",
561ad8c1
PP
243 attr_obj, attr_field_obj);
244 }
44e0a4f5
JG
245
246end:
8138bfe1 247 bt_object_put_ref(attr_field_obj);
44e0a4f5
JG
248 return ret;
249}
250
251BT_HIDDEN
ce141536
PP
252struct bt_value *bt_attributes_borrow_field_value_by_name(
253 struct bt_value *attr_obj, const char *name)
44e0a4f5 254{
ce141536
PP
255 struct bt_value *value_obj = NULL;
256 struct bt_value *attr_field_obj = NULL;
44e0a4f5 257
ec4a3354
PP
258 BT_ASSERT_DBG(attr_obj);
259 BT_ASSERT_DBG(name);
5fe68922 260 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 261 if (!attr_field_obj) {
561ad8c1
PP
262 BT_LOGD("Cannot find attributes object's field by name: "
263 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
264 goto end;
265 }
266
ce141536 267 value_obj = bt_value_array_borrow_element_by_index(
17582c6d 268 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1 269 if (!value_obj) {
a8f90e5d
PP
270 BT_LIB_LOGE_APPEND_CAUSE(
271 "Cannot get attribute array value's element by index: "
272 "%![value-]+v, index=%" PRIu64, attr_field_obj,
839d52a5 273 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 274 }
44e0a4f5
JG
275
276end:
44e0a4f5
JG
277 return value_obj;
278}
279
280BT_HIDDEN
ce141536 281int bt_attributes_freeze(const struct bt_value *attr_obj)
44e0a4f5 282{
b602018b 283 uint64_t i, count;
44e0a4f5
JG
284 int ret = 0;
285
a8f90e5d 286 BT_ASSERT(attr_obj);
561ad8c1 287 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
b602018b 288
1270d0e9 289 count = bt_value_array_get_length(attr_obj);
44e0a4f5
JG
290
291 /*
dac5c838
PP
292 * We do not freeze the array value object itself here, since
293 * internal stuff could need to modify/add attributes. Each
294 * attribute is frozen one by one.
44e0a4f5
JG
295 */
296 for (i = 0; i < count; ++i) {
ce141536 297 struct bt_value *obj = NULL;
44e0a4f5 298
ce141536
PP
299 obj = bt_attributes_borrow_field_value(
300 (void *) attr_obj, i);
44e0a4f5 301 if (!obj) {
a8f90e5d
PP
302 BT_LIB_LOGE_APPEND_CAUSE(
303 "Cannot get attributes object's field value by index: "
304 "%![value-]+v, index=%" PRIu64,
561ad8c1 305 attr_obj, i);
44e0a4f5
JG
306 ret = -1;
307 goto end;
308 }
309
ce141536 310 bt_value_freeze(obj);
44e0a4f5
JG
311 }
312
313end:
44e0a4f5
JG
314 return ret;
315}
This page took 0.081426 seconds and 4 git commands to generate.