assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[babeltrace.git] / lib / graph / notification / packet.c
1 /*
2 * Babeltrace Plug-in Packet-related Notifications
3 *
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_TAG "NOTIF-PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ctf-ir/packet.h>
32 #include <babeltrace/ctf-ir/packet-internal.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/stream.h>
35 #include <babeltrace/ctf-ir/stream-internal.h>
36 #include <babeltrace/graph/graph-internal.h>
37 #include <babeltrace/graph/notification-packet-internal.h>
38 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
39 #include <babeltrace/assert-internal.h>
40 #include <babeltrace/assert-pre-internal.h>
41 #include <babeltrace/object-internal.h>
42 #include <inttypes.h>
43
44 BT_HIDDEN
45 struct bt_notification *bt_notification_packet_begin_new(struct bt_graph *graph)
46 {
47 struct bt_notification_packet_begin *notification;
48
49 notification = g_new0(struct bt_notification_packet_begin, 1);
50 if (!notification) {
51 BT_LOGE_STR("Failed to allocate one packet beginning notification.");
52 goto error;
53 }
54
55 bt_notification_init(&notification->parent,
56 BT_NOTIFICATION_TYPE_PACKET_BEGIN,
57 (bt_object_release_func) bt_notification_packet_begin_recycle,
58 graph);
59 goto end;
60
61 error:
62 BT_PUT(notification);
63
64 end:
65 return (void *) notification;
66 }
67
68 struct bt_notification *bt_notification_packet_begin_create(
69 struct bt_private_connection_private_notification_iterator *notif_iter,
70 struct bt_packet *packet)
71 {
72 struct bt_notification_packet_begin *notification = NULL;
73 struct bt_stream *stream;
74 struct bt_stream_class *stream_class;
75 struct bt_graph *graph;
76 int ret;
77
78 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
79 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
80 stream = bt_packet_borrow_stream(packet);
81 BT_ASSERT(stream);
82 stream_class = bt_stream_borrow_class(stream);
83 BT_ASSERT(stream_class);
84 BT_LOGD("Creating packet beginning notification object: "
85 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
86 "stream-class-addr=%p, stream-class-name=\"%s\", "
87 "stream-class-id=%" PRId64,
88 packet, stream, bt_stream_get_name(stream),
89 stream_class,
90 bt_stream_class_get_name(stream_class),
91 bt_stream_class_get_id(stream_class));
92
93 if (likely(!packet->props_are_set)) {
94 ret = bt_packet_set_properties(packet);
95 if (ret) {
96 BT_LIB_LOGE("Cannot update packet's properties: "
97 "%![prev-packet-]+a", packet);
98 goto end;
99 }
100 }
101
102 graph = bt_private_connection_private_notification_iterator_borrow_graph(
103 notif_iter);
104 notification = (void *) bt_notification_create_from_pool(
105 &graph->packet_begin_notif_pool, graph);
106 if (!notification) {
107 /* bt_notification_create_from_pool() logs errors */
108 goto end;
109 }
110
111 BT_ASSERT(!notification->packet);
112 notification->packet = packet;
113 bt_object_get_no_null_check_no_parent_check(
114 &notification->packet->base);
115 bt_packet_set_is_frozen(packet, true);
116 bt_packet_validate_properties(packet);
117 BT_LOGD("Created packet beginning notification object: "
118 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
119 "stream-class-addr=%p, stream-class-name=\"%s\", "
120 "stream-class-id=%" PRId64 ", addr=%p",
121 packet, stream, bt_stream_get_name(stream),
122 stream_class,
123 bt_stream_class_get_name(stream_class),
124 bt_stream_class_get_id(stream_class), notification);
125 goto end;
126
127 end:
128 return (void *) notification;
129 }
130
131 BT_HIDDEN
132 void bt_notification_packet_begin_destroy(struct bt_notification *notif)
133 {
134 struct bt_notification_packet_begin *packet_begin_notif = (void *) notif;
135
136 BT_LOGD("Destroying packet beginning notification: addr=%p", notif);
137 BT_LOGD_STR("Putting packet.");
138 BT_PUT(packet_begin_notif->packet);
139 g_free(notif);
140 }
141
142 BT_HIDDEN
143 void bt_notification_packet_begin_recycle(struct bt_notification *notif)
144 {
145 struct bt_notification_packet_begin *packet_begin_notif = (void *) notif;
146 struct bt_graph *graph;
147
148 BT_ASSERT(packet_begin_notif);
149
150 if (unlikely(!notif->graph)) {
151 bt_notification_packet_begin_destroy(notif);
152 return;
153 }
154
155 BT_LOGD("Recycling packet beginning notification: addr=%p", notif);
156 bt_notification_reset(notif);
157 bt_object_put_no_null_check(&packet_begin_notif->packet->base);
158 packet_begin_notif->packet = NULL;
159 graph = notif->graph;
160 notif->graph = NULL;
161 bt_object_pool_recycle_object(&graph->packet_begin_notif_pool, notif);
162 }
163
164 struct bt_packet *bt_notification_packet_begin_borrow_packet(
165 struct bt_notification *notification)
166 {
167 struct bt_notification_packet_begin *packet_begin;
168
169 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
170 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
171 BT_NOTIFICATION_TYPE_PACKET_BEGIN);
172 packet_begin = container_of(notification,
173 struct bt_notification_packet_begin, parent);
174 return packet_begin->packet;
175 }
176
177 BT_HIDDEN
178 struct bt_notification *bt_notification_packet_end_new(struct bt_graph *graph)
179 {
180 struct bt_notification_packet_end *notification;
181
182 notification = g_new0(struct bt_notification_packet_end, 1);
183 if (!notification) {
184 BT_LOGE_STR("Failed to allocate one packet end notification.");
185 goto error;
186 }
187
188 bt_notification_init(&notification->parent,
189 BT_NOTIFICATION_TYPE_PACKET_END,
190 (bt_object_release_func) bt_notification_packet_end_recycle,
191 graph);
192 goto end;
193
194 error:
195 BT_PUT(notification);
196
197 end:
198 return (void *) notification;
199 }
200
201 struct bt_notification *bt_notification_packet_end_create(
202 struct bt_private_connection_private_notification_iterator *notif_iter,
203 struct bt_packet *packet)
204 {
205 struct bt_notification_packet_end *notification = NULL;
206 struct bt_stream *stream;
207 struct bt_stream_class *stream_class;
208 struct bt_graph *graph;
209 int ret;
210
211 BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator");
212 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
213 stream = bt_packet_borrow_stream(packet);
214 BT_ASSERT(stream);
215 stream_class = bt_stream_borrow_class(stream);
216 BT_ASSERT(stream_class);
217 BT_LOGD("Creating packet end notification object: "
218 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
219 "stream-class-addr=%p, stream-class-name=\"%s\", "
220 "stream-class-id=%" PRId64,
221 packet, stream, bt_stream_get_name(stream),
222 stream_class,
223 bt_stream_class_get_name(stream_class),
224 bt_stream_class_get_id(stream_class));
225
226 if (unlikely(!packet->props_are_set)) {
227 ret = bt_packet_set_properties(packet);
228 if (ret) {
229 BT_LIB_LOGE("Cannot update packet's properties: "
230 "%![prev-packet-]+a", packet);
231 goto end;
232 }
233 }
234
235 graph = bt_private_connection_private_notification_iterator_borrow_graph(
236 notif_iter);
237 notification = (void *) bt_notification_create_from_pool(
238 &graph->packet_end_notif_pool, graph);
239 if (!notification) {
240 /* bt_notification_create_from_pool() logs errors */
241 goto end;
242 }
243
244 BT_ASSERT(!notification->packet);
245 notification->packet = packet;
246 bt_object_get_no_null_check_no_parent_check(
247 &notification->packet->base);
248 bt_packet_set_is_frozen(packet, true);
249 bt_packet_validate_properties(packet);
250 BT_LOGD("Created packet end notification object: "
251 "packet-addr=%p, stream-addr=%p, stream-name=\"%s\", "
252 "stream-class-addr=%p, stream-class-name=\"%s\", "
253 "stream-class-id=%" PRId64 ", addr=%p",
254 packet, stream, bt_stream_get_name(stream),
255 stream_class,
256 bt_stream_class_get_name(stream_class),
257 bt_stream_class_get_id(stream_class), notification);
258 goto end;
259
260 end:
261 return (void *) notification;
262 }
263
264 BT_HIDDEN
265 void bt_notification_packet_end_destroy(struct bt_notification *notif)
266 {
267 struct bt_notification_packet_end *packet_end_notif = (void *) notif;
268
269 BT_LOGD("Destroying packet end notification: addr=%p", notif);
270 BT_LOGD_STR("Putting packet.");
271 BT_PUT(packet_end_notif->packet);
272 g_free(notif);
273 }
274
275 BT_HIDDEN
276 void bt_notification_packet_end_recycle(struct bt_notification *notif)
277 {
278 struct bt_notification_packet_end *packet_end_notif = (void *) notif;
279 struct bt_graph *graph;
280
281 BT_ASSERT(packet_end_notif);
282
283 if (!notif->graph) {
284 bt_notification_packet_end_destroy(notif);
285 return;
286 }
287
288 BT_LOGD("Recycling packet end notification: addr=%p", notif);
289 bt_notification_reset(notif);
290 BT_PUT(packet_end_notif->packet);
291 graph = notif->graph;
292 notif->graph = NULL;
293 bt_object_pool_recycle_object(&graph->packet_end_notif_pool, notif);
294 }
295
296 struct bt_packet *bt_notification_packet_end_borrow_packet(
297 struct bt_notification *notification)
298 {
299 struct bt_notification_packet_end *packet_end;
300
301 BT_ASSERT_PRE_NON_NULL(notification, "Notification");
302 BT_ASSERT_PRE_NOTIF_IS_TYPE(notification,
303 BT_NOTIFICATION_TYPE_PACKET_END);
304 packet_end = container_of(notification,
305 struct bt_notification_packet_end, parent);
306 return packet_end->packet;
307 }
This page took 0.035319 seconds and 4 git commands to generate.