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