lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / graph / notification / inactivity.c
1 /*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "NOTIF-INACTIVITY"
24 #include <babeltrace/lib-logging-internal.h>
25
26 #include <babeltrace/object-internal.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/ctf-ir/clock-class.h>
29 #include <babeltrace/ctf-ir/clock-value-internal.h>
30 #include <babeltrace/graph/clock-class-priority-map.h>
31 #include <babeltrace/graph/clock-class-priority-map-internal.h>
32 #include <babeltrace/graph/notification-internal.h>
33 #include <babeltrace/graph/notification-inactivity-internal.h>
34 #include <babeltrace/assert-pre-internal.h>
35
36 static
37 void bt_notification_inactivity_destroy(struct bt_object *obj)
38 {
39 struct bt_notification_inactivity *notification =
40 (struct bt_notification_inactivity *) obj;
41
42 BT_LOGD("Destroying inactivity notification: addr=%p", notification);
43 BT_LOGD_STR("Putting clock class priority map.");
44 bt_put(notification->cc_prio_map);
45
46 if (notification->clock_values) {
47 BT_LOGD_STR("Putting clock values.");
48 g_hash_table_destroy(notification->clock_values);
49 }
50
51 g_free(notification);
52 }
53
54 struct bt_notification *bt_notification_inactivity_create(
55 struct bt_clock_class_priority_map *cc_prio_map)
56 {
57 struct bt_notification_inactivity *notification;
58 struct bt_notification *ret_notif = NULL;
59 uint64_t i;
60
61 if (cc_prio_map) {
62 /* Function's reference, released at the end */
63 bt_get(cc_prio_map);
64 } else {
65 cc_prio_map = bt_clock_class_priority_map_create();
66 if (!cc_prio_map) {
67 BT_LOGE_STR("Cannot create empty clock class priority map.");
68 goto error;
69 }
70 }
71
72 BT_LOGD("Creating inactivity notification object: "
73 "cc-prio-map-addr=%p",
74 cc_prio_map);
75 notification = g_new0(struct bt_notification_inactivity, 1);
76 if (!notification) {
77 BT_LOGE_STR("Failed to allocate one inactivity notification.");
78 goto error;
79 }
80 bt_notification_init(&notification->parent,
81 BT_NOTIFICATION_TYPE_INACTIVITY,
82 bt_notification_inactivity_destroy);
83 ret_notif = &notification->parent;
84 notification->clock_values = g_hash_table_new_full(g_direct_hash,
85 g_direct_equal, NULL, (GDestroyNotify) bt_clock_value_recycle);
86 if (!notification->clock_values) {
87 BT_LOGE_STR("Failed to allocate a GHashTable.");
88 goto error;
89 }
90
91 for (i = 0; i < cc_prio_map->entries->len; i++) {
92 struct bt_clock_value *clock_value;
93 struct bt_clock_class *clock_class =
94 cc_prio_map->entries->pdata[i];
95
96 clock_value = bt_clock_value_create(clock_class);
97 if (!clock_value) {
98 BT_LIB_LOGE("Cannot create clock value from clock class: "
99 "%![cc-]+K", clock_class);
100 goto error;
101 }
102
103 g_hash_table_insert(notification->clock_values,
104 clock_class, clock_value);
105 }
106
107 notification->cc_prio_map = bt_get(cc_prio_map);
108 BT_LOGD_STR("Freezing inactivity notification's clock class priority map.");
109 bt_clock_class_priority_map_freeze(cc_prio_map);
110 BT_LOGD("Created inactivity notification object: "
111 "cc-prio-map-addr=%p, notif-addr=%p",
112 cc_prio_map, ret_notif);
113 goto end;
114
115 error:
116 BT_PUT(ret_notif);
117
118 end:
119 bt_put(cc_prio_map);
120 return ret_notif;
121 }
122
123 extern struct bt_clock_class_priority_map *
124 bt_notification_inactivity_borrow_clock_class_priority_map(
125 struct bt_notification *notification)
126 {
127 struct bt_notification_inactivity *inactivity_notification;
128
129 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
130 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
131 BT_NOTIFICATION_TYPE_INACTIVITY);
132 inactivity_notification = container_of(notification,
133 struct bt_notification_inactivity, parent);
134 return inactivity_notification->cc_prio_map;
135 }
136
137 struct bt_clock_value *bt_notification_inactivity_borrow_clock_value(
138 struct bt_notification *notification,
139 struct bt_clock_class *clock_class)
140 {
141 struct bt_notification_inactivity *inactivity_notification;
142
143 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
144 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
145 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
146 BT_NOTIFICATION_TYPE_INACTIVITY);
147 inactivity_notification = container_of(notification,
148 struct bt_notification_inactivity, parent);
149 return g_hash_table_lookup(
150 inactivity_notification->clock_values, clock_class);
151 }
This page took 0.031669 seconds and 4 git commands to generate.