lib: make `bt_attributes_get_count()` return uint64_t
[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
350ad6c1 24#define BT_LOG_TAG "LIB/ATTRS"
c2d9d9cf 25#include "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 62 if (!attr_obj) {
870631a2 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
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
99b4b64b 80uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
44e0a4f5 81{
393729a6 82 return bt_value_array_get_length(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
98b15851
PP
93 BT_ASSERT_DBG(attr_obj);
94 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
05e21286 95 attr_field_obj = bt_value_array_borrow_element_by_index_const(
da91b29a 96 attr_obj, index);
44e0a4f5 97 if (!attr_field_obj) {
870631a2
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
da91b29a 104 attr_field_name_obj =
05e21286 105 bt_value_array_borrow_element_by_index_const(attr_field_obj,
da91b29a 106 BT_ATTR_NAME_INDEX);
44e0a4f5 107 if (!attr_field_name_obj) {
870631a2
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,
50842bdc 111 (uint64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
112 goto end;
113 }
114
05e21286 115 ret = bt_value_string_get(attr_field_name_obj);
44e0a4f5
JG
116
117end:
44e0a4f5
JG
118 return ret;
119}
120
121BT_HIDDEN
05e21286
PP
122struct bt_value *bt_attributes_borrow_field_value(
123 struct bt_value *attr_obj, uint64_t index)
44e0a4f5 124{
05e21286
PP
125 struct bt_value *value_obj = NULL;
126 struct bt_value *attr_field_obj = NULL;
44e0a4f5 127
98b15851
PP
128 BT_ASSERT_DBG(attr_obj);
129 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
da91b29a 130 attr_field_obj =
05e21286 131 bt_value_array_borrow_element_by_index(attr_obj, index);
44e0a4f5 132 if (!attr_field_obj) {
870631a2
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
05e21286 139 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 140 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1 141 if (!value_obj) {
870631a2
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,
50842bdc 145 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 146 }
44e0a4f5
JG
147
148end:
44e0a4f5
JG
149 return value_obj;
150}
151
152static
05e21286
PP
153struct bt_value *bt_attributes_borrow_field_by_name(
154 struct bt_value *attr_obj, const char *name)
44e0a4f5 155{
dcf0cc71
PP
156 uint64_t i;
157 int64_t attr_size;
05e21286
PP
158 struct bt_value *value_obj = NULL;
159 struct bt_value *attr_field_name_obj = NULL;
44e0a4f5 160
393729a6 161 attr_size = bt_value_array_get_length(attr_obj);
44e0a4f5 162 if (attr_size < 0) {
870631a2
PP
163 BT_LIB_LOGE_APPEND_CAUSE(
164 "Cannot get array value's size: %![value-]+v",
561ad8c1 165 attr_obj);
44e0a4f5
JG
166 goto error;
167 }
168
169 for (i = 0; i < attr_size; ++i) {
44e0a4f5
JG
170 const char *field_name;
171
05e21286 172 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 173 attr_obj, i);
44e0a4f5 174 if (!value_obj) {
870631a2
PP
175 BT_LIB_LOGE_APPEND_CAUSE(
176 "Cannot get attributes object's array value's element by index: "
177 "%![value-]+v, index=%" PRIu64, attr_obj, i);
44e0a4f5
JG
178 goto error;
179 }
180
da91b29a 181 attr_field_name_obj =
05e21286 182 bt_value_array_borrow_element_by_index(
da91b29a 183 value_obj, BT_ATTR_NAME_INDEX);
44e0a4f5 184 if (!attr_field_name_obj) {
870631a2
PP
185 BT_LIB_LOGE_APPEND_CAUSE(
186 "Cannot get attribute array value's element by index: "
187 "%![value-]+v, index=%" PRIu64,
094ff7c0 188 value_obj, (int64_t) BT_ATTR_NAME_INDEX);
44e0a4f5
JG
189 goto error;
190 }
191
05e21286 192 field_name = bt_value_string_get(attr_field_name_obj);
44e0a4f5 193
2242b43d 194 if (strcmp(field_name, name) == 0) {
44e0a4f5
JG
195 break;
196 }
197
094ff7c0 198 value_obj = NULL;
44e0a4f5
JG
199 }
200
201 return value_obj;
202
203error:
094ff7c0 204 value_obj = NULL;
44e0a4f5
JG
205 return value_obj;
206}
207
208BT_HIDDEN
05e21286
PP
209int bt_attributes_set_field_value(struct bt_value *attr_obj,
210 const char *name, struct bt_value *value_obj)
44e0a4f5
JG
211{
212 int ret = 0;
05e21286 213 struct bt_value *attr_field_obj = NULL;
44e0a4f5 214
870631a2
PP
215 BT_ASSERT(attr_obj);
216 BT_ASSERT(name);
217 BT_ASSERT(value_obj);
094ff7c0 218 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 219 if (attr_field_obj) {
05e21286 220 ret = bt_value_array_set_element_by_index(
da91b29a 221 attr_field_obj, BT_ATTR_VALUE_INDEX,
05e21286 222 value_obj);
094ff7c0 223 attr_field_obj = NULL;
44e0a4f5
JG
224 goto end;
225 }
226
05e21286 227 attr_field_obj = bt_value_array_create();
44e0a4f5 228 if (!attr_field_obj) {
870631a2 229 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
44e0a4f5
JG
230 ret = -1;
231 goto end;
232 }
233
05e21286 234 ret = bt_value_array_append_string_element(attr_field_obj,
da91b29a 235 name);
05e21286
PP
236 ret |= bt_value_array_append_element(attr_field_obj,
237 value_obj);
44e0a4f5 238 if (ret) {
870631a2
PP
239 BT_LIB_LOGE_APPEND_CAUSE(
240 "Cannot append elements to array value: %!+v",
561ad8c1 241 attr_field_obj);
44e0a4f5
JG
242 goto end;
243 }
244
05e21286
PP
245 ret = bt_value_array_append_element(attr_obj,
246 attr_field_obj);
561ad8c1 247 if (ret) {
870631a2
PP
248 BT_LIB_LOGE_APPEND_CAUSE(
249 "Cannot append element to array value: "
250 "%![array-value-]+v, %![element-value-]+v",
561ad8c1
PP
251 attr_obj, attr_field_obj);
252 }
44e0a4f5
JG
253
254end:
65300d60 255 bt_object_put_ref(attr_field_obj);
44e0a4f5
JG
256 return ret;
257}
258
259BT_HIDDEN
05e21286
PP
260struct bt_value *bt_attributes_borrow_field_value_by_name(
261 struct bt_value *attr_obj, const char *name)
44e0a4f5 262{
05e21286
PP
263 struct bt_value *value_obj = NULL;
264 struct bt_value *attr_field_obj = NULL;
44e0a4f5 265
98b15851
PP
266 BT_ASSERT_DBG(attr_obj);
267 BT_ASSERT_DBG(name);
094ff7c0 268 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
44e0a4f5 269 if (!attr_field_obj) {
561ad8c1
PP
270 BT_LOGD("Cannot find attributes object's field by name: "
271 "value-addr=%p, name=\"%s\"", attr_obj, name);
44e0a4f5
JG
272 goto end;
273 }
274
05e21286 275 value_obj = bt_value_array_borrow_element_by_index(
da91b29a 276 attr_field_obj, BT_ATTR_VALUE_INDEX);
561ad8c1 277 if (!value_obj) {
870631a2
PP
278 BT_LIB_LOGE_APPEND_CAUSE(
279 "Cannot get attribute array value's element by index: "
280 "%![value-]+v, index=%" PRIu64, attr_field_obj,
50842bdc 281 (uint64_t) BT_ATTR_VALUE_INDEX);
561ad8c1 282 }
44e0a4f5
JG
283
284end:
44e0a4f5
JG
285 return value_obj;
286}
287
288BT_HIDDEN
05e21286 289int bt_attributes_freeze(const struct bt_value *attr_obj)
44e0a4f5 290{
dcf0cc71
PP
291 uint64_t i;
292 int64_t count;
44e0a4f5
JG
293 int ret = 0;
294
870631a2 295 BT_ASSERT(attr_obj);
561ad8c1 296 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
393729a6 297 count = bt_value_array_get_length(attr_obj);
f6ccaed9 298 BT_ASSERT(count >= 0);
44e0a4f5
JG
299
300 /*
dac5c838
PP
301 * We do not freeze the array value object itself here, since
302 * internal stuff could need to modify/add attributes. Each
303 * attribute is frozen one by one.
44e0a4f5
JG
304 */
305 for (i = 0; i < count; ++i) {
05e21286 306 struct bt_value *obj = NULL;
44e0a4f5 307
05e21286
PP
308 obj = bt_attributes_borrow_field_value(
309 (void *) attr_obj, i);
44e0a4f5 310 if (!obj) {
870631a2
PP
311 BT_LIB_LOGE_APPEND_CAUSE(
312 "Cannot get attributes object's field value by index: "
313 "%![value-]+v, index=%" PRIu64,
561ad8c1 314 attr_obj, i);
44e0a4f5
JG
315 ret = -1;
316 goto end;
317 }
318
05e21286 319 bt_value_freeze(obj);
44e0a4f5
JG
320 }
321
322end:
44e0a4f5
JG
323 return ret;
324}
This page took 0.085093 seconds and 4 git commands to generate.