Commit | Line | Data |
---|---|---|
b011f6b0 | 1 | /* |
e2f7325d | 2 | * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com> |
b011f6b0 | 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
b011f6b0 PP |
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 | ||
1e6cfa95 | 24 | #define BT_LOG_TAG "FIELD-PATH" |
3fadfbc0 | 25 | #include <babeltrace2/lib-logging-internal.h> |
1e6cfa95 | 26 | |
3fadfbc0 MJ |
27 | #include <babeltrace2/assert-pre-internal.h> |
28 | #include <babeltrace2/trace-ir/field-class.h> | |
29 | #include <babeltrace2/trace-ir/field-class-internal.h> | |
30 | #include <babeltrace2/trace-ir/field-path-internal.h> | |
31 | #include <babeltrace2/trace-ir/field-path-const.h> | |
b011f6b0 | 32 | #include <limits.h> |
544d0515 | 33 | #include <stdint.h> |
1e6cfa95 | 34 | #include <inttypes.h> |
3fadfbc0 | 35 | #include <babeltrace2/assert-internal.h> |
b011f6b0 PP |
36 | #include <glib.h> |
37 | ||
38 | static | |
44c440bc | 39 | void destroy_field_path(struct bt_object *obj) |
b011f6b0 | 40 | { |
50842bdc | 41 | struct bt_field_path *field_path = (struct bt_field_path *) obj; |
b011f6b0 | 42 | |
44c440bc PP |
43 | BT_ASSERT(field_path); |
44 | BT_LIB_LOGD("Destroying field path: %!+P", field_path); | |
66ddcddf PP |
45 | g_array_free(field_path->items, TRUE); |
46 | field_path->items = NULL; | |
b011f6b0 PP |
47 | g_free(field_path); |
48 | } | |
49 | ||
50 | BT_HIDDEN | |
50842bdc | 51 | struct bt_field_path *bt_field_path_create(void) |
b011f6b0 | 52 | { |
50842bdc | 53 | struct bt_field_path *field_path = NULL; |
b011f6b0 | 54 | |
1e6cfa95 PP |
55 | BT_LOGD_STR("Creating empty field path object."); |
56 | ||
50842bdc | 57 | field_path = g_new0(struct bt_field_path, 1); |
b011f6b0 | 58 | if (!field_path) { |
1e6cfa95 | 59 | BT_LOGE_STR("Failed to allocate one field path."); |
b011f6b0 PP |
60 | goto error; |
61 | } | |
62 | ||
44c440bc | 63 | bt_object_init_shared(&field_path->base, destroy_field_path); |
66ddcddf PP |
64 | field_path->items = g_array_new(FALSE, FALSE, |
65 | sizeof(struct bt_field_path_item)); | |
66 | if (!field_path->items) { | |
1e6cfa95 | 67 | BT_LOGE_STR("Failed to allocate a GArray."); |
b011f6b0 PP |
68 | goto error; |
69 | } | |
70 | ||
44c440bc PP |
71 | BT_LIB_LOGD("Created empty field path object: %!+P", field_path); |
72 | goto end; | |
b011f6b0 PP |
73 | |
74 | error: | |
65300d60 | 75 | BT_OBJECT_PUT_REF_AND_RESET(field_path); |
b011f6b0 | 76 | |
b011f6b0 | 77 | end: |
44c440bc | 78 | return field_path; |
b011f6b0 PP |
79 | } |
80 | ||
40f4ba76 PP |
81 | enum bt_scope bt_field_path_get_root_scope( |
82 | const struct bt_field_path *field_path) | |
b011f6b0 | 83 | { |
44c440bc PP |
84 | BT_ASSERT_PRE_NON_NULL(field_path, "Field path"); |
85 | return field_path->root; | |
b011f6b0 PP |
86 | } |
87 | ||
66ddcddf | 88 | uint64_t bt_field_path_get_item_count(const struct bt_field_path *field_path) |
b011f6b0 | 89 | { |
44c440bc | 90 | BT_ASSERT_PRE_NON_NULL(field_path, "Field path"); |
66ddcddf | 91 | return (uint64_t) field_path->items->len; |
b011f6b0 PP |
92 | } |
93 | ||
66ddcddf | 94 | const struct bt_field_path_item *bt_field_path_borrow_item_by_index_const( |
40f4ba76 | 95 | const struct bt_field_path *field_path, uint64_t index) |
b011f6b0 | 96 | { |
44c440bc | 97 | BT_ASSERT_PRE_NON_NULL(field_path, "Field path"); |
66ddcddf PP |
98 | BT_ASSERT_PRE_VALID_INDEX(index, field_path->items->len); |
99 | return bt_field_path_borrow_item_by_index_inline(field_path, index); | |
100 | } | |
101 | ||
102 | enum bt_field_path_item_type bt_field_path_item_get_type( | |
103 | const struct bt_field_path_item *field_path_item) | |
104 | { | |
105 | BT_ASSERT_PRE_NON_NULL(field_path_item, "Field path item"); | |
106 | return field_path_item->type; | |
107 | } | |
108 | ||
109 | uint64_t bt_field_path_item_index_get_index( | |
110 | const struct bt_field_path_item *field_path_item) | |
111 | { | |
112 | BT_ASSERT_PRE_NON_NULL(field_path_item, "Field path item"); | |
113 | BT_ASSERT_PRE(field_path_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX, | |
114 | "Field path item is not an index field path item: " | |
115 | "addr=%p, type=%s", field_path_item, | |
116 | bt_field_path_item_type_string(field_path_item->type)); | |
117 | return field_path_item->index; | |
b011f6b0 | 118 | } |
c5b9b441 PP |
119 | |
120 | void bt_field_path_get_ref(const struct bt_field_path *field_path) | |
121 | { | |
122 | bt_object_get_ref(field_path); | |
123 | } | |
124 | ||
125 | void bt_field_path_put_ref(const struct bt_field_path *field_path) | |
126 | { | |
127 | bt_object_put_ref(field_path); | |
128 | } |