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