Commit | Line | Data |
---|---|---|
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> |
b011f6b0 PP |
32 | #include <babeltrace/ctf-ir/field-path-internal.h> |
33 | #include <babeltrace/ctf-ir/field-path.h> | |
34 | #include <limits.h> | |
544d0515 | 35 | #include <stdint.h> |
1e6cfa95 | 36 | #include <inttypes.h> |
b011f6b0 PP |
37 | #include <glib.h> |
38 | ||
39 | static | |
40 | void field_path_destroy(struct bt_object *obj) | |
41 | { | |
42 | struct bt_ctf_field_path *field_path = (struct bt_ctf_field_path *) obj; | |
43 | ||
1e6cfa95 PP |
44 | BT_LOGD("Destroying field path: addr=%p", obj); |
45 | ||
b011f6b0 PP |
46 | if (!field_path) { |
47 | return; | |
48 | } | |
49 | ||
50 | if (field_path->indexes) { | |
51 | g_array_free(field_path->indexes, TRUE); | |
52 | } | |
53 | g_free(field_path); | |
54 | } | |
55 | ||
56 | BT_HIDDEN | |
57 | struct bt_ctf_field_path *bt_ctf_field_path_create(void) | |
58 | { | |
59 | struct bt_ctf_field_path *field_path = NULL; | |
60 | ||
1e6cfa95 PP |
61 | BT_LOGD_STR("Creating empty field path object."); |
62 | ||
b011f6b0 PP |
63 | field_path = g_new0(struct bt_ctf_field_path, 1); |
64 | if (!field_path) { | |
1e6cfa95 | 65 | BT_LOGE_STR("Failed to allocate one field path."); |
b011f6b0 PP |
66 | goto error; |
67 | } | |
68 | ||
69 | bt_object_init(field_path, field_path_destroy); | |
46df6b28 | 70 | field_path->root = BT_CTF_SCOPE_UNKNOWN; |
b011f6b0 PP |
71 | field_path->indexes = g_array_new(TRUE, FALSE, sizeof(int)); |
72 | if (!field_path->indexes) { | |
1e6cfa95 | 73 | BT_LOGE_STR("Failed to allocate a GArray."); |
b011f6b0 PP |
74 | goto error; |
75 | } | |
76 | ||
1e6cfa95 | 77 | BT_LOGD("Created empty field path object: addr=%p", field_path); |
b011f6b0 PP |
78 | return field_path; |
79 | ||
80 | error: | |
81 | BT_PUT(field_path); | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | BT_HIDDEN | |
86 | void bt_ctf_field_path_clear(struct bt_ctf_field_path *field_path) | |
87 | { | |
88 | if (field_path->indexes->len > 0) { | |
89 | g_array_remove_range(field_path->indexes, 0, | |
90 | field_path->indexes->len); | |
91 | } | |
92 | } | |
93 | ||
94 | BT_HIDDEN | |
95 | struct bt_ctf_field_path *bt_ctf_field_path_copy( | |
96 | struct bt_ctf_field_path *path) | |
97 | { | |
1e6cfa95 | 98 | struct bt_ctf_field_path *new_path; |
b011f6b0 | 99 | |
1e6cfa95 PP |
100 | assert(path); |
101 | BT_LOGD("Copying field path: addr=%p, index-count=%u", | |
102 | path, path->indexes->len); | |
103 | new_path = bt_ctf_field_path_create(); | |
b011f6b0 | 104 | if (!new_path) { |
1e6cfa95 | 105 | BT_LOGE_STR("Cannot create empty field path."); |
b011f6b0 PP |
106 | goto end; |
107 | } | |
108 | ||
109 | new_path->root = path->root; | |
110 | g_array_insert_vals(new_path->indexes, 0, | |
111 | path->indexes->data, path->indexes->len); | |
1e6cfa95 PP |
112 | BT_LOGD("Copied field path: original-addr=%p, copy-addr=%p", |
113 | path, new_path); | |
b011f6b0 PP |
114 | end: |
115 | return new_path; | |
116 | } | |
117 | ||
b74f1a20 | 118 | enum bt_ctf_scope bt_ctf_field_path_get_root_scope( |
b011f6b0 PP |
119 | const struct bt_ctf_field_path *field_path) |
120 | { | |
46df6b28 | 121 | enum bt_ctf_scope scope = BT_CTF_SCOPE_UNKNOWN; |
b011f6b0 PP |
122 | |
123 | if (!field_path) { | |
1e6cfa95 | 124 | BT_LOGW_STR("Invalid parameter: field path is NULL."); |
b011f6b0 PP |
125 | goto end; |
126 | } | |
127 | ||
128 | scope = field_path->root; | |
129 | ||
130 | end: | |
131 | return scope; | |
132 | } | |
133 | ||
544d0515 | 134 | int64_t bt_ctf_field_path_get_index_count( |
b011f6b0 PP |
135 | const struct bt_ctf_field_path *field_path) |
136 | { | |
1e6cfa95 PP |
137 | int64_t count = (int64_t) -1; |
138 | ||
139 | if (!field_path) { | |
140 | BT_LOGW_STR("Invalid parameter: field path is NULL."); | |
141 | goto end; | |
142 | } | |
143 | ||
144 | count = (int64_t) field_path->indexes->len; | |
145 | ||
146 | end: | |
147 | return count; | |
b011f6b0 PP |
148 | } |
149 | ||
150 | int bt_ctf_field_path_get_index(const struct bt_ctf_field_path *field_path, | |
9ac68eb1 | 151 | uint64_t index) |
b011f6b0 PP |
152 | { |
153 | int ret = INT_MIN; | |
154 | ||
9ac68eb1 | 155 | if (!field_path) { |
1e6cfa95 | 156 | BT_LOGW_STR("Invalid parameter: field path is NULL."); |
b011f6b0 PP |
157 | goto end; |
158 | } | |
159 | ||
160 | if (index >= field_path->indexes->len) { | |
1e6cfa95 PP |
161 | BT_LOGW("Invalid parameter: index is out of bounds: " |
162 | "addr=%p, index=%" PRIu64 ", count=%u", | |
163 | field_path, index, field_path->indexes->len); | |
b011f6b0 PP |
164 | goto end; |
165 | } | |
166 | ||
167 | ret = g_array_index(field_path->indexes, int, index); | |
168 | ||
169 | end: | |
170 | return ret; | |
171 | } |