| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/FIELD-PATH" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "lib/assert-cond.h" |
| 12 | #include <babeltrace2/trace-ir/field-class.h> |
| 13 | #include <babeltrace2/trace-ir/field-path.h> |
| 14 | #include <stdint.h> |
| 15 | #include "common/assert.h" |
| 16 | #include <glib.h> |
| 17 | |
| 18 | #include "field-path.h" |
| 19 | |
| 20 | static |
| 21 | void destroy_field_path(struct bt_object *obj) |
| 22 | { |
| 23 | struct bt_field_path *field_path = (struct bt_field_path *) obj; |
| 24 | |
| 25 | BT_ASSERT(field_path); |
| 26 | BT_LIB_LOGD("Destroying field path: %!+P", field_path); |
| 27 | g_array_free(field_path->items, TRUE); |
| 28 | field_path->items = NULL; |
| 29 | g_free(field_path); |
| 30 | } |
| 31 | |
| 32 | struct bt_field_path *bt_field_path_create(void) |
| 33 | { |
| 34 | struct bt_field_path *field_path = NULL; |
| 35 | |
| 36 | BT_LOGD_STR("Creating empty field path object."); |
| 37 | |
| 38 | field_path = g_new0(struct bt_field_path, 1); |
| 39 | if (!field_path) { |
| 40 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one field path."); |
| 41 | goto error; |
| 42 | } |
| 43 | |
| 44 | bt_object_init_shared(&field_path->base, destroy_field_path); |
| 45 | field_path->items = g_array_new(FALSE, FALSE, |
| 46 | sizeof(struct bt_field_path_item)); |
| 47 | if (!field_path->items) { |
| 48 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray."); |
| 49 | goto error; |
| 50 | } |
| 51 | |
| 52 | BT_LIB_LOGD("Created empty field path object: %!+P", field_path); |
| 53 | goto end; |
| 54 | |
| 55 | error: |
| 56 | BT_OBJECT_PUT_REF_AND_RESET(field_path); |
| 57 | |
| 58 | end: |
| 59 | return field_path; |
| 60 | } |
| 61 | |
| 62 | BT_EXPORT |
| 63 | enum bt_field_path_scope bt_field_path_get_root_scope( |
| 64 | const struct bt_field_path *field_path) |
| 65 | { |
| 66 | BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path); |
| 67 | return field_path->root; |
| 68 | } |
| 69 | |
| 70 | BT_EXPORT |
| 71 | uint64_t bt_field_path_get_item_count(const struct bt_field_path *field_path) |
| 72 | { |
| 73 | BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path); |
| 74 | return (uint64_t) field_path->items->len; |
| 75 | } |
| 76 | |
| 77 | BT_EXPORT |
| 78 | const struct bt_field_path_item *bt_field_path_borrow_item_by_index_const( |
| 79 | const struct bt_field_path *field_path, uint64_t index) |
| 80 | { |
| 81 | BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path); |
| 82 | BT_ASSERT_PRE_DEV_VALID_INDEX(index, field_path->items->len); |
| 83 | return bt_field_path_borrow_item_by_index_inline(field_path, index); |
| 84 | } |
| 85 | |
| 86 | BT_EXPORT |
| 87 | enum bt_field_path_item_type bt_field_path_item_get_type( |
| 88 | const struct bt_field_path_item *field_path_item) |
| 89 | { |
| 90 | BT_ASSERT_PRE_DEV_NON_NULL("field-path-item", field_path_item, |
| 91 | "Field path item"); |
| 92 | return field_path_item->type; |
| 93 | } |
| 94 | |
| 95 | BT_EXPORT |
| 96 | uint64_t bt_field_path_item_index_get_index( |
| 97 | const struct bt_field_path_item *field_path_item) |
| 98 | { |
| 99 | BT_ASSERT_PRE_DEV_NON_NULL("field-path-item", field_path_item, |
| 100 | "Field path item"); |
| 101 | BT_ASSERT_PRE_DEV("is-index-field-path-item", |
| 102 | field_path_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX, |
| 103 | "Field path item is not an index field path item: " |
| 104 | "addr=%p, type=%s", field_path_item, |
| 105 | bt_field_path_item_type_string(field_path_item->type)); |
| 106 | return field_path_item->index; |
| 107 | } |
| 108 | |
| 109 | BT_EXPORT |
| 110 | void bt_field_path_get_ref(const struct bt_field_path *field_path) |
| 111 | { |
| 112 | bt_object_get_ref(field_path); |
| 113 | } |
| 114 | |
| 115 | BT_EXPORT |
| 116 | void bt_field_path_put_ref(const struct bt_field_path *field_path) |
| 117 | { |
| 118 | bt_object_put_ref(field_path); |
| 119 | } |