lib: use common precond. assert. macros from `assert-cond.h` thru lib
[babeltrace.git] / src / lib / trace-ir / field-path.c
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 <limits.h>
15 #include <stdint.h>
16 #include <inttypes.h>
17 #include "common/assert.h"
18 #include <glib.h>
19
20 #include "field-class.h"
21 #include "field-path.h"
22
23 static
24 void destroy_field_path(struct bt_object *obj)
25 {
26 struct bt_field_path *field_path = (struct bt_field_path *) obj;
27
28 BT_ASSERT(field_path);
29 BT_LIB_LOGD("Destroying field path: %!+P", field_path);
30 g_array_free(field_path->items, TRUE);
31 field_path->items = NULL;
32 g_free(field_path);
33 }
34
35 BT_HIDDEN
36 struct bt_field_path *bt_field_path_create(void)
37 {
38 struct bt_field_path *field_path = NULL;
39
40 BT_LOGD_STR("Creating empty field path object.");
41
42 field_path = g_new0(struct bt_field_path, 1);
43 if (!field_path) {
44 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one field path.");
45 goto error;
46 }
47
48 bt_object_init_shared(&field_path->base, destroy_field_path);
49 field_path->items = g_array_new(FALSE, FALSE,
50 sizeof(struct bt_field_path_item));
51 if (!field_path->items) {
52 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
53 goto error;
54 }
55
56 BT_LIB_LOGD("Created empty field path object: %!+P", field_path);
57 goto end;
58
59 error:
60 BT_OBJECT_PUT_REF_AND_RESET(field_path);
61
62 end:
63 return field_path;
64 }
65
66 enum bt_field_path_scope bt_field_path_get_root_scope(
67 const struct bt_field_path *field_path)
68 {
69 BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path);
70 return field_path->root;
71 }
72
73 uint64_t bt_field_path_get_item_count(const struct bt_field_path *field_path)
74 {
75 BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path);
76 return (uint64_t) field_path->items->len;
77 }
78
79 const struct bt_field_path_item *bt_field_path_borrow_item_by_index_const(
80 const struct bt_field_path *field_path, uint64_t index)
81 {
82 BT_ASSERT_PRE_DEV_FP_NON_NULL(field_path);
83 BT_ASSERT_PRE_DEV_VALID_INDEX(index, field_path->items->len);
84 return bt_field_path_borrow_item_by_index_inline(field_path, index);
85 }
86
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 return field_path_item->type;
92 }
93
94 uint64_t bt_field_path_item_index_get_index(
95 const struct bt_field_path_item *field_path_item)
96 {
97 BT_ASSERT_PRE_DEV_NON_NULL(field_path_item, "Field path item");
98 BT_ASSERT_PRE_DEV(field_path_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX,
99 "Field path item is not an index field path item: "
100 "addr=%p, type=%s", field_path_item,
101 bt_field_path_item_type_string(field_path_item->type));
102 return field_path_item->index;
103 }
104
105 void bt_field_path_get_ref(const struct bt_field_path *field_path)
106 {
107 bt_object_get_ref(field_path);
108 }
109
110 void bt_field_path_put_ref(const struct bt_field_path *field_path)
111 {
112 bt_object_put_ref(field_path);
113 }
This page took 0.034623 seconds and 4 git commands to generate.