lib: remove unused includes
[babeltrace.git] / src / lib / trace-ir / attributes.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/ATTRS"
9 #include "lib/logging.h"
10
11 #include <babeltrace2/value.h>
12 #include "lib/object.h"
13 #include <babeltrace2/value.h>
14 #include "lib/value.h"
15 #include "attributes.h"
16 #include <inttypes.h>
17 #include "compat/string.h"
18 #include "common/assert.h"
19
20 #define BT_ATTR_NAME_INDEX 0
21 #define BT_ATTR_VALUE_INDEX 1
22
23 struct bt_value *bt_attributes_create(void)
24 {
25 struct bt_value *attr_obj;
26
27 /*
28 * Attributes: array value object of array value objects, each one
29 * containing two entries: a string value object (attributes
30 * field name), and a value object (attributes field value).
31 *
32 * Example (JSON representation):
33 *
34 * [
35 * ["hostname", "eeppdesk"],
36 * ["sysname", "Linux"],
37 * ["tracer_major", 2],
38 * ["tracer_minor", 5]
39 * ]
40 */
41 BT_LOGD_STR("Creating attributes object.");
42 attr_obj = bt_value_array_create();
43 if (!attr_obj) {
44 BT_LIB_LOGE_APPEND_CAUSE("Failed to create array value.");
45 } else {
46 BT_LOGD("Created attributes object: addr=%p",
47 attr_obj);
48 }
49
50 return attr_obj;
51 }
52
53 void bt_attributes_destroy(struct bt_value *attr_obj)
54 {
55 BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
56 BT_OBJECT_PUT_REF_AND_RESET(attr_obj);
57 }
58
59 uint64_t bt_attributes_get_count(const struct bt_value *attr_obj)
60 {
61 return bt_value_array_get_length(attr_obj);
62 }
63
64 const char *bt_attributes_get_field_name(const struct bt_value *attr_obj,
65 uint64_t index)
66 {
67 const struct bt_value *attr_field_obj = NULL;
68 const struct bt_value *attr_field_name_obj = NULL;
69
70 BT_ASSERT_DBG(attr_obj);
71 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
72 attr_field_obj = bt_value_array_borrow_element_by_index_const(
73 attr_obj, index);
74
75 attr_field_name_obj =
76 bt_value_array_borrow_element_by_index_const(attr_field_obj,
77 BT_ATTR_NAME_INDEX);
78
79 return bt_value_string_get(attr_field_name_obj);
80 }
81
82 struct bt_value *bt_attributes_borrow_field_value(
83 struct bt_value *attr_obj, uint64_t index)
84 {
85 struct bt_value *attr_field_obj = NULL;
86
87 BT_ASSERT_DBG(attr_obj);
88 BT_ASSERT_DBG(index < bt_value_array_get_length(attr_obj));
89
90 attr_field_obj =
91 bt_value_array_borrow_element_by_index(attr_obj, index);
92
93 return bt_value_array_borrow_element_by_index( attr_field_obj,
94 BT_ATTR_VALUE_INDEX);
95 }
96
97 static
98 struct bt_value *bt_attributes_borrow_field_by_name(
99 struct bt_value *attr_obj, const char *name)
100 {
101 uint64_t i, attr_size;
102 struct bt_value *value_obj = NULL;
103 struct bt_value *attr_field_name_obj = NULL;
104
105 attr_size = bt_value_array_get_length(attr_obj);
106 for (i = 0; i < attr_size; ++i) {
107 const char *field_name;
108
109 value_obj = bt_value_array_borrow_element_by_index(
110 attr_obj, i);
111
112 attr_field_name_obj =
113 bt_value_array_borrow_element_by_index(
114 value_obj, BT_ATTR_NAME_INDEX);
115
116 field_name = bt_value_string_get(attr_field_name_obj);
117
118 if (strcmp(field_name, name) == 0) {
119 break;
120 }
121
122 value_obj = NULL;
123 }
124
125 return value_obj;
126 }
127
128 int bt_attributes_set_field_value(struct bt_value *attr_obj,
129 const char *name, struct bt_value *value_obj)
130 {
131 int ret = 0;
132 struct bt_value *attr_field_obj = NULL;
133
134 BT_ASSERT(attr_obj);
135 BT_ASSERT(name);
136 BT_ASSERT(value_obj);
137 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
138 if (attr_field_obj) {
139 ret = bt_value_array_set_element_by_index(
140 attr_field_obj, BT_ATTR_VALUE_INDEX,
141 value_obj);
142 attr_field_obj = NULL;
143 goto end;
144 }
145
146 attr_field_obj = bt_value_array_create();
147 if (!attr_field_obj) {
148 BT_LIB_LOGE_APPEND_CAUSE("Failed to create empty array value.");
149 ret = -1;
150 goto end;
151 }
152
153 ret = bt_value_array_append_string_element(attr_field_obj,
154 name);
155 ret |= bt_value_array_append_element(attr_field_obj,
156 value_obj);
157 if (ret) {
158 BT_LIB_LOGE_APPEND_CAUSE(
159 "Cannot append elements to array value: %!+v",
160 attr_field_obj);
161 goto end;
162 }
163
164 ret = bt_value_array_append_element(attr_obj,
165 attr_field_obj);
166 if (ret) {
167 BT_LIB_LOGE_APPEND_CAUSE(
168 "Cannot append element to array value: "
169 "%![array-value-]+v, %![element-value-]+v",
170 attr_obj, attr_field_obj);
171 }
172
173 end:
174 bt_object_put_ref(attr_field_obj);
175 return ret;
176 }
177
178 struct bt_value *bt_attributes_borrow_field_value_by_name(
179 struct bt_value *attr_obj, const char *name)
180 {
181 struct bt_value *value_obj = NULL;
182 struct bt_value *attr_field_obj = NULL;
183
184 BT_ASSERT_DBG(attr_obj);
185 BT_ASSERT_DBG(name);
186 attr_field_obj = bt_attributes_borrow_field_by_name(attr_obj, name);
187 if (!attr_field_obj) {
188 BT_LOGD("Cannot find attributes object's field by name: "
189 "value-addr=%p, name=\"%s\"", attr_obj, name);
190 goto end;
191 }
192
193 value_obj = bt_value_array_borrow_element_by_index(
194 attr_field_obj, BT_ATTR_VALUE_INDEX);
195
196 end:
197 return value_obj;
198 }
199
200 int bt_attributes_freeze(const struct bt_value *attr_obj)
201 {
202 uint64_t i, count;
203 int ret = 0;
204
205 BT_ASSERT(attr_obj);
206 BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
207
208 count = bt_value_array_get_length(attr_obj);
209
210 /*
211 * We do not freeze the array value object itself here, since
212 * internal stuff could need to modify/add attributes. Each
213 * attribute is frozen one by one.
214 */
215 for (i = 0; i < count; ++i) {
216 struct bt_value *obj = NULL;
217
218 obj = bt_attributes_borrow_field_value(
219 (void *) attr_obj, i);
220 if (!obj) {
221 BT_LIB_LOGE_APPEND_CAUSE(
222 "Cannot get attributes object's field value by index: "
223 "%![value-]+v, index=%" PRIu64,
224 attr_obj, i);
225 ret = -1;
226 goto end;
227 }
228
229 bt_value_freeze(obj);
230 }
231
232 end:
233 return ret;
234 }
This page took 0.034585 seconds and 4 git commands to generate.