| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
| 6 | * |
| 7 | * Babeltrace CTF writer - Field path |
| 8 | */ |
| 9 | |
| 10 | #define BT_LOG_TAG "CTF-WRITER/FIELD-PATH" |
| 11 | #include "logging.h" |
| 12 | |
| 13 | #include <glib.h> |
| 14 | #include <inttypes.h> |
| 15 | #include <limits.h> |
| 16 | #include <stdint.h> |
| 17 | |
| 18 | #include <babeltrace2-ctf-writer/field-types.h> |
| 19 | |
| 20 | #include "common/assert.h" |
| 21 | |
| 22 | #include "field-path.h" |
| 23 | #include "field-types.h" |
| 24 | |
| 25 | static |
| 26 | void field_path_destroy(struct bt_ctf_object *obj) |
| 27 | { |
| 28 | struct bt_ctf_field_path *field_path = (struct bt_ctf_field_path *) obj; |
| 29 | |
| 30 | BT_LOGD("Destroying field path: addr=%p", obj); |
| 31 | |
| 32 | if (!field_path) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | if (field_path->indexes) { |
| 37 | g_array_free(field_path->indexes, TRUE); |
| 38 | } |
| 39 | g_free(field_path); |
| 40 | } |
| 41 | |
| 42 | BT_HIDDEN |
| 43 | struct bt_ctf_field_path *bt_ctf_field_path_create(void) |
| 44 | { |
| 45 | struct bt_ctf_field_path *field_path = NULL; |
| 46 | |
| 47 | BT_LOGD_STR("Creating empty field path object."); |
| 48 | |
| 49 | field_path = g_new0(struct bt_ctf_field_path, 1); |
| 50 | if (!field_path) { |
| 51 | BT_LOGE_STR("Failed to allocate one field path."); |
| 52 | goto error; |
| 53 | } |
| 54 | |
| 55 | bt_ctf_object_init_shared(&field_path->base, field_path_destroy); |
| 56 | field_path->root = BT_CTF_SCOPE_UNKNOWN; |
| 57 | field_path->indexes = g_array_new(TRUE, FALSE, sizeof(int)); |
| 58 | if (!field_path->indexes) { |
| 59 | BT_LOGE_STR("Failed to allocate a GArray."); |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | BT_LOGD("Created empty field path object: addr=%p", field_path); |
| 64 | return field_path; |
| 65 | |
| 66 | error: |
| 67 | BT_CTF_OBJECT_PUT_REF_AND_RESET(field_path); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | BT_HIDDEN |
| 72 | void bt_ctf_field_path_clear(struct bt_ctf_field_path *field_path) |
| 73 | { |
| 74 | if (field_path->indexes->len > 0) { |
| 75 | g_array_remove_range(field_path->indexes, 0, |
| 76 | field_path->indexes->len); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | BT_HIDDEN |
| 81 | struct bt_ctf_field_path *bt_ctf_field_path_copy( |
| 82 | struct bt_ctf_field_path *path) |
| 83 | { |
| 84 | struct bt_ctf_field_path *new_path; |
| 85 | |
| 86 | BT_ASSERT_DBG(path); |
| 87 | BT_LOGD("Copying field path: addr=%p, index-count=%u", |
| 88 | path, path->indexes->len); |
| 89 | new_path = bt_ctf_field_path_create(); |
| 90 | if (!new_path) { |
| 91 | BT_LOGE_STR("Cannot create empty field path."); |
| 92 | goto end; |
| 93 | } |
| 94 | |
| 95 | new_path->root = path->root; |
| 96 | g_array_insert_vals(new_path->indexes, 0, |
| 97 | path->indexes->data, path->indexes->len); |
| 98 | BT_LOGD("Copied field path: original-addr=%p, copy-addr=%p", |
| 99 | path, new_path); |
| 100 | end: |
| 101 | return new_path; |
| 102 | } |
| 103 | |
| 104 | enum bt_ctf_scope bt_ctf_field_path_get_root_scope( |
| 105 | const struct bt_ctf_field_path *field_path) |
| 106 | { |
| 107 | enum bt_ctf_scope scope = BT_CTF_SCOPE_UNKNOWN; |
| 108 | |
| 109 | if (!field_path) { |
| 110 | BT_LOGW_STR("Invalid parameter: field path is NULL."); |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | scope = field_path->root; |
| 115 | |
| 116 | end: |
| 117 | return scope; |
| 118 | } |
| 119 | |
| 120 | int64_t bt_ctf_field_path_get_index_count( |
| 121 | const struct bt_ctf_field_path *field_path) |
| 122 | { |
| 123 | int64_t count = (int64_t) -1; |
| 124 | |
| 125 | if (!field_path) { |
| 126 | BT_LOGW_STR("Invalid parameter: field path is NULL."); |
| 127 | goto end; |
| 128 | } |
| 129 | |
| 130 | count = (int64_t) field_path->indexes->len; |
| 131 | |
| 132 | end: |
| 133 | return count; |
| 134 | } |
| 135 | |
| 136 | int bt_ctf_field_path_get_index(const struct bt_ctf_field_path *field_path, |
| 137 | uint64_t index) |
| 138 | { |
| 139 | int ret = INT_MIN; |
| 140 | |
| 141 | if (!field_path) { |
| 142 | BT_LOGW_STR("Invalid parameter: field path is NULL."); |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | if (index >= field_path->indexes->len) { |
| 147 | BT_LOGW("Invalid parameter: index is out of bounds: " |
| 148 | "addr=%p, index=%" PRIu64 ", count=%u", |
| 149 | field_path, index, field_path->indexes->len); |
| 150 | goto end; |
| 151 | } |
| 152 | |
| 153 | ret = g_array_index(field_path->indexes, int, index); |
| 154 | |
| 155 | end: |
| 156 | return ret; |
| 157 | } |