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