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