assert-pre-internal.h: append "error is" to first message
[babeltrace.git] / lib / graph / message / event.c
CommitLineData
d6e69534
PP
1/*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@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
24#define BT_LOG_TAG "MSG-EVENT"
25#include <babeltrace/lib-logging-internal.h>
26
27#include <babeltrace/assert-internal.h>
28#include <babeltrace/assert-pre-internal.h>
29#include <babeltrace/compiler-internal.h>
30#include <babeltrace/object-internal.h>
31#include <babeltrace/trace-ir/event.h>
32#include <babeltrace/trace-ir/event-internal.h>
33#include <babeltrace/trace-ir/event-class-internal.h>
34#include <babeltrace/trace-ir/stream-class-internal.h>
35#include <babeltrace/trace-ir/trace.h>
36#include <babeltrace/graph/graph-internal.h>
37#include <babeltrace/graph/message-event-const.h>
38#include <babeltrace/graph/message-event.h>
39#include <babeltrace/graph/message-event-internal.h>
40#include <babeltrace/types.h>
41#include <stdbool.h>
42#include <inttypes.h>
43
44BT_ASSERT_PRE_FUNC
45static inline bool event_class_has_trace(struct bt_event_class *event_class)
46{
47 struct bt_stream_class *stream_class;
48
49 stream_class = bt_event_class_borrow_stream_class(event_class);
50 BT_ASSERT(stream_class);
51 return bt_stream_class_borrow_trace_class(stream_class) != NULL;
52}
53
54BT_HIDDEN
55struct bt_message *bt_message_event_new(
56 struct bt_graph *graph)
57{
58 struct bt_message_event *message = NULL;
59
60 message = g_new0(struct bt_message_event, 1);
61 if (!message) {
62 BT_LOGE_STR("Failed to allocate one event message.");
63 goto error;
64 }
65
66 bt_message_init(&message->parent, BT_MESSAGE_TYPE_EVENT,
67 (bt_object_release_func) bt_message_event_recycle, graph);
68 goto end;
69
70error:
71 BT_OBJECT_PUT_REF_AND_RESET(message);
72
73end:
74 return (void *) message;
75}
76
77struct bt_message *bt_message_event_create(
78 struct bt_self_message_iterator *self_msg_iter,
58085ca4
PP
79 const struct bt_event_class *c_event_class,
80 const struct bt_packet *c_packet)
d6e69534
PP
81{
82 struct bt_self_component_port_input_message_iterator *msg_iter =
83 (void *) self_msg_iter;
84 struct bt_message_event *message = NULL;
58085ca4
PP
85 struct bt_event_class *event_class = (void *) c_event_class;
86 struct bt_packet *packet = (void *) c_packet;
d6e69534
PP
87 struct bt_event *event;
88
89 BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
90 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
91 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
92 BT_ASSERT_PRE(event_class_has_trace(event_class),
93 "Event class is not part of a trace: %!+E", event_class);
94 BT_LIB_LOGD("Creating event message object: %![ec-]+E",
95 event_class);
96 event = bt_event_create(event_class, packet);
97 if (unlikely(!event)) {
98 BT_LIB_LOGE("Cannot create event from event class: "
99 "%![ec-]+E", event_class);
100 goto error;
101 }
102
103 /*
104 * Create message from pool _after_ we have everything
105 * (in this case, a valid event object) so that we never have an
106 * error condition with a non-NULL message object.
107 * Otherwise:
108 *
109 * * We cannot recycle the message on error because
110 * bt_message_event_recycle() expects a complete
111 * message (and the event or clock class priority map
112 * object could be unset).
113 *
114 * * We cannot destroy the message because we would need
115 * to notify the graph (pool owner) so that it removes the
116 * message from its message array.
117 */
118 message = (void *) bt_message_create_from_pool(
119 &msg_iter->graph->event_msg_pool, msg_iter->graph);
120 if (unlikely(!message)) {
121 /* bt_message_create_from_pool() logs errors */
122 goto error;
123 }
124
125 BT_ASSERT(!message->event);
126 message->event = event;
127 bt_packet_set_is_frozen(packet, true);
128 bt_event_class_freeze(event_class);
129 BT_LIB_LOGD("Created event message object: "
130 "%![msg-]+n, %![event-]+e", message, event);
131 goto end;
132
133error:
134 BT_ASSERT(!message);
135 bt_event_destroy(event);
136
137end:
138 return (void *) message;
139}
140
141BT_HIDDEN
142void bt_message_event_destroy(struct bt_message *msg)
143{
144 struct bt_message_event *event_msg = (void *) msg;
145
146 BT_LIB_LOGD("Destroying event message: %!+n", msg);
147
148 if (event_msg->event) {
149 BT_LIB_LOGD("Recycling event: %!+e", event_msg->event);
150 bt_event_recycle(event_msg->event);
151 event_msg->event = NULL;
152 }
153
154 g_free(msg);
155}
156
157BT_HIDDEN
158void bt_message_event_recycle(struct bt_message *msg)
159{
160 struct bt_message_event *event_msg = (void *) msg;
161 struct bt_graph *graph;
162
163 BT_ASSERT(event_msg);
164
165 if (unlikely(!msg->graph)) {
166 bt_message_event_destroy(msg);
167 return;
168 }
169
170 BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
171 msg, event_msg->event);
172 bt_message_reset(msg);
173 BT_ASSERT(event_msg->event);
174 bt_event_recycle(event_msg->event);
175 event_msg->event = NULL;
176 graph = msg->graph;
177 msg->graph = NULL;
178 bt_object_pool_recycle_object(&graph->event_msg_pool, msg);
179}
180
181static inline
182struct bt_event *borrow_event(struct bt_message *message)
183{
184 struct bt_message_event *event_message;
185
186 BT_ASSERT_PRE_NON_NULL(message, "Message");
187 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
188 event_message = container_of(message,
189 struct bt_message_event, parent);
190 return event_message->event;
191}
192
193struct bt_event *bt_message_event_borrow_event(
194 struct bt_message *message)
195{
196 return borrow_event(message);
197}
198
199const struct bt_event *bt_message_event_borrow_event_const(
200 const struct bt_message *message)
201{
202 return borrow_event((void *) message);
203}
This page took 0.029423 seconds and 4 git commands to generate.