lib: trace IR, values: reset pointers to `NULL` on destruction
[babeltrace.git] / lib / trace-ir / field-path.c
CommitLineData
b011f6b0 1/*
b011f6b0
PP
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
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
PP
24#define BT_LOG_TAG "FIELD-PATH"
25#include <babeltrace/lib-logging-internal.h>
26
44c440bc 27#include <babeltrace/assert-pre-internal.h>
5cd6d0e5
PP
28#include <babeltrace/trace-ir/field-classes.h>
29#include <babeltrace/trace-ir/field-classes-internal.h>
56e18c4c
PP
30#include <babeltrace/trace-ir/field-path-internal.h>
31#include <babeltrace/trace-ir/field-path.h>
e5be10ef 32#include <babeltrace/object.h>
b011f6b0 33#include <limits.h>
544d0515 34#include <stdint.h>
1e6cfa95 35#include <inttypes.h>
f6ccaed9 36#include <babeltrace/assert-internal.h>
b011f6b0
PP
37#include <glib.h>
38
39static
44c440bc 40void destroy_field_path(struct bt_object *obj)
b011f6b0 41{
50842bdc 42 struct bt_field_path *field_path = (struct bt_field_path *) obj;
b011f6b0 43
44c440bc
PP
44 BT_ASSERT(field_path);
45 BT_LIB_LOGD("Destroying field path: %!+P", field_path);
46 g_array_free(field_path->indexes, TRUE);
238b7404 47 field_path->indexes = NULL;
b011f6b0
PP
48 g_free(field_path);
49}
50
51BT_HIDDEN
50842bdc 52struct bt_field_path *bt_field_path_create(void)
b011f6b0 53{
50842bdc 54 struct bt_field_path *field_path = NULL;
b011f6b0 55
1e6cfa95
PP
56 BT_LOGD_STR("Creating empty field path object.");
57
50842bdc 58 field_path = g_new0(struct bt_field_path, 1);
b011f6b0 59 if (!field_path) {
1e6cfa95 60 BT_LOGE_STR("Failed to allocate one field path.");
b011f6b0
PP
61 goto error;
62 }
63
44c440bc
PP
64 bt_object_init_shared(&field_path->base, destroy_field_path);
65 field_path->indexes = g_array_new(FALSE, FALSE, sizeof(uint64_t));
b011f6b0 66 if (!field_path->indexes) {
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
74error:
65300d60 75 BT_OBJECT_PUT_REF_AND_RESET(field_path);
b011f6b0 76
b011f6b0 77end:
44c440bc 78 return field_path;
b011f6b0
PP
79}
80
44c440bc 81enum bt_scope bt_field_path_get_root_scope(struct bt_field_path *field_path)
b011f6b0 82{
44c440bc
PP
83 BT_ASSERT_PRE_NON_NULL(field_path, "Field path");
84 return field_path->root;
b011f6b0
PP
85}
86
44c440bc 87uint64_t bt_field_path_get_index_count(struct bt_field_path *field_path)
b011f6b0 88{
44c440bc
PP
89 BT_ASSERT_PRE_NON_NULL(field_path, "Field path");
90 return (uint64_t) field_path->indexes->len;
b011f6b0
PP
91}
92
44c440bc 93uint64_t bt_field_path_get_index_by_index(struct bt_field_path *field_path,
9ac68eb1 94 uint64_t index)
b011f6b0 95{
44c440bc
PP
96 BT_ASSERT_PRE_NON_NULL(field_path, "Field path");
97 BT_ASSERT_PRE_VALID_INDEX(index, field_path->indexes->len);
98 return bt_field_path_get_index_by_index_inline(field_path, index);
b011f6b0 99}
This page took 0.04363 seconds and 4 git commands to generate.