assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[babeltrace.git] / lib / ctf-ir / field-path.c
CommitLineData
b011f6b0
PP
1/*
2 * field-path.c
3 *
4 * Babeltrace CTF IR - Field path
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
1e6cfa95
PP
28#define BT_LOG_TAG "FIELD-PATH"
29#include <babeltrace/lib-logging-internal.h>
30
2e33ac5a 31#include <babeltrace/ctf-ir/field-types.h>
331b8d44 32#include <babeltrace/ctf-ir/field-types-internal.h>
b011f6b0
PP
33#include <babeltrace/ctf-ir/field-path-internal.h>
34#include <babeltrace/ctf-ir/field-path.h>
35#include <limits.h>
544d0515 36#include <stdint.h>
1e6cfa95 37#include <inttypes.h>
f6ccaed9 38#include <babeltrace/assert-internal.h>
b011f6b0
PP
39#include <glib.h>
40
41static
42void field_path_destroy(struct bt_object *obj)
43{
50842bdc 44 struct bt_field_path *field_path = (struct bt_field_path *) obj;
b011f6b0 45
1e6cfa95
PP
46 BT_LOGD("Destroying field path: addr=%p", obj);
47
b011f6b0
PP
48 if (!field_path) {
49 return;
50 }
51
52 if (field_path->indexes) {
53 g_array_free(field_path->indexes, TRUE);
54 }
55 g_free(field_path);
56}
57
58BT_HIDDEN
50842bdc 59struct bt_field_path *bt_field_path_create(void)
b011f6b0 60{
50842bdc 61 struct bt_field_path *field_path = NULL;
b011f6b0 62
1e6cfa95
PP
63 BT_LOGD_STR("Creating empty field path object.");
64
50842bdc 65 field_path = g_new0(struct bt_field_path, 1);
b011f6b0 66 if (!field_path) {
1e6cfa95 67 BT_LOGE_STR("Failed to allocate one field path.");
b011f6b0
PP
68 goto error;
69 }
70
3fea54f6 71 bt_object_init_shared(&field_path->base, field_path_destroy);
50842bdc 72 field_path->root = BT_SCOPE_UNKNOWN;
b011f6b0
PP
73 field_path->indexes = g_array_new(TRUE, FALSE, sizeof(int));
74 if (!field_path->indexes) {
1e6cfa95 75 BT_LOGE_STR("Failed to allocate a GArray.");
b011f6b0
PP
76 goto error;
77 }
78
1e6cfa95 79 BT_LOGD("Created empty field path object: addr=%p", field_path);
b011f6b0
PP
80 return field_path;
81
82error:
83 BT_PUT(field_path);
84 return NULL;
85}
86
87BT_HIDDEN
50842bdc 88void bt_field_path_clear(struct bt_field_path *field_path)
b011f6b0
PP
89{
90 if (field_path->indexes->len > 0) {
91 g_array_remove_range(field_path->indexes, 0,
92 field_path->indexes->len);
93 }
94}
95
96BT_HIDDEN
50842bdc
PP
97struct bt_field_path *bt_field_path_copy(
98 struct bt_field_path *path)
b011f6b0 99{
50842bdc 100 struct bt_field_path *new_path;
b011f6b0 101
f6ccaed9 102 BT_ASSERT(path);
1e6cfa95
PP
103 BT_LOGD("Copying field path: addr=%p, index-count=%u",
104 path, path->indexes->len);
50842bdc 105 new_path = bt_field_path_create();
b011f6b0 106 if (!new_path) {
1e6cfa95 107 BT_LOGE_STR("Cannot create empty field path.");
b011f6b0
PP
108 goto end;
109 }
110
111 new_path->root = path->root;
112 g_array_insert_vals(new_path->indexes, 0,
113 path->indexes->data, path->indexes->len);
1e6cfa95
PP
114 BT_LOGD("Copied field path: original-addr=%p, copy-addr=%p",
115 path, new_path);
b011f6b0
PP
116end:
117 return new_path;
118}
119
50842bdc
PP
120enum bt_scope bt_field_path_get_root_scope(
121 const struct bt_field_path *field_path)
b011f6b0 122{
50842bdc 123 enum bt_scope scope = BT_SCOPE_UNKNOWN;
b011f6b0
PP
124
125 if (!field_path) {
1e6cfa95 126 BT_LOGW_STR("Invalid parameter: field path is NULL.");
b011f6b0
PP
127 goto end;
128 }
129
130 scope = field_path->root;
131
132end:
133 return scope;
134}
135
50842bdc
PP
136int64_t bt_field_path_get_index_count(
137 const struct bt_field_path *field_path)
b011f6b0 138{
1e6cfa95
PP
139 int64_t count = (int64_t) -1;
140
141 if (!field_path) {
142 BT_LOGW_STR("Invalid parameter: field path is NULL.");
143 goto end;
144 }
145
146 count = (int64_t) field_path->indexes->len;
147
148end:
149 return count;
b011f6b0
PP
150}
151
50842bdc 152int bt_field_path_get_index(const struct bt_field_path *field_path,
9ac68eb1 153 uint64_t index)
b011f6b0
PP
154{
155 int ret = INT_MIN;
156
9ac68eb1 157 if (!field_path) {
1e6cfa95 158 BT_LOGW_STR("Invalid parameter: field path is NULL.");
b011f6b0
PP
159 goto end;
160 }
161
162 if (index >= field_path->indexes->len) {
1e6cfa95
PP
163 BT_LOGW("Invalid parameter: index is out of bounds: "
164 "addr=%p, index=%" PRIu64 ", count=%u",
165 field_path, index, field_path->indexes->len);
b011f6b0
PP
166 goto end;
167 }
168
169 ret = g_array_index(field_path->indexes, int, index);
170
171end:
172 return ret;
173}
This page took 0.044951 seconds and 4 git commands to generate.