Always evaluate BT_ASSERT(); add BT_ASSERT_DBG() for debug mode only
[babeltrace.git] / src / lib / graph / message / stream.c
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 "LIB/MSG-STREAM"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include "compat/compiler.h"
29 #include <babeltrace2/trace-ir/clock-snapshot-const.h>
30 #include "lib/trace-ir/stream.h"
31 #include <babeltrace2/trace-ir/stream-class.h>
32 #include "lib/trace-ir/stream-class.h"
33 #include <babeltrace2/graph/message-stream-beginning.h>
34 #include <babeltrace2/graph/message-stream-end.h>
35 #include <babeltrace2/graph/message-stream-beginning-const.h>
36 #include <babeltrace2/graph/message-stream-end-const.h>
37 #include "common/assert.h"
38 #include <inttypes.h>
39
40 #include "stream.h"
41
42 static
43 void destroy_stream_message(struct bt_object *obj)
44 {
45 struct bt_message_stream *message = (void *) obj;
46
47 BT_LIB_LOGD("Destroying stream message: %!+n", message);
48
49 if (message->default_cs) {
50 BT_LIB_LOGD("Putting default clock snapshot: %!+k",
51 message->default_cs);
52 bt_clock_snapshot_destroy(message->default_cs);
53 message->default_cs = NULL;
54 }
55
56 BT_LIB_LOGD("Putting stream: %!+s", message->stream);
57 BT_OBJECT_PUT_REF_AND_RESET(message->stream);
58 g_free(message);
59 }
60
61 static inline
62 struct bt_message *create_stream_message(
63 struct bt_self_message_iterator *self_msg_iter,
64 struct bt_stream *stream, enum bt_message_type type)
65 {
66 struct bt_message_stream *message;
67 struct bt_stream_class *stream_class;
68
69 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
70 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
71 stream_class = bt_stream_borrow_class(stream);
72 BT_ASSERT(stream_class);
73 BT_LIB_LOGD("Creating stream message object: "
74 "type=%s, %![stream-]+s, %![sc-]+S",
75 bt_message_type_string(type), stream, stream_class);
76 message = g_new0(struct bt_message_stream, 1);
77 if (!message) {
78 BT_LIB_LOGE_APPEND_CAUSE(
79 "Failed to allocate one stream message.");
80 goto error;
81 }
82
83 bt_message_init(&message->parent, type,
84 destroy_stream_message, NULL);
85 message->stream = stream;
86 bt_object_get_ref_no_null_check(message->stream);
87
88 if (stream_class->default_clock_class) {
89 message->default_cs = bt_clock_snapshot_create(
90 stream_class->default_clock_class);
91 if (!message->default_cs) {
92 goto error;
93 }
94 }
95
96 BT_LIB_LOGD("Created stream message object: "
97 "%![msg-]+n, %![stream-]+s, %![sc-]+S", message,
98 stream, stream_class);
99
100 goto end;
101
102 error:
103 if (message) {
104 g_free(message);
105 message = NULL;
106 }
107
108 end:
109 return &message->parent;
110 }
111
112 struct bt_message *bt_message_stream_beginning_create(
113 struct bt_self_message_iterator *self_msg_iter,
114 const struct bt_stream *stream)
115 {
116 return create_stream_message(self_msg_iter, (void *) stream,
117 BT_MESSAGE_TYPE_STREAM_BEGINNING);
118 }
119
120 struct bt_message *bt_message_stream_end_create(
121 struct bt_self_message_iterator *self_msg_iter,
122 const struct bt_stream *stream)
123 {
124 return create_stream_message(self_msg_iter, (void *) stream,
125 BT_MESSAGE_TYPE_STREAM_END);
126 }
127
128 static inline
129 struct bt_stream *borrow_stream_message_stream(struct bt_message *message)
130 {
131 struct bt_message_stream *stream_msg;
132
133 BT_ASSERT_DBG(message);
134 stream_msg = (void *) message;
135 return stream_msg->stream;
136 }
137
138 struct bt_stream *bt_message_stream_beginning_borrow_stream(
139 struct bt_message *message)
140 {
141 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
142 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
143 BT_MESSAGE_TYPE_STREAM_BEGINNING);
144 return borrow_stream_message_stream(message);
145 }
146
147 struct bt_stream *bt_message_stream_end_borrow_stream(
148 struct bt_message *message)
149 {
150 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
151 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
152 BT_MESSAGE_TYPE_STREAM_END);
153 return borrow_stream_message_stream(message);
154 }
155
156 const struct bt_stream *bt_message_stream_beginning_borrow_stream_const(
157 const struct bt_message *message)
158 {
159 return bt_message_stream_beginning_borrow_stream(
160 (void *) message);
161 }
162
163 const struct bt_stream *bt_message_stream_end_borrow_stream_const(
164 const struct bt_message *message)
165 {
166 return bt_message_stream_end_borrow_stream(
167 (void *) message);
168 }
169
170 static
171 void bt_message_stream_set_default_clock_snapshot(
172 struct bt_message *msg, uint64_t raw_value)
173 {
174 struct bt_message_stream *stream_msg = (void *) msg;
175 struct bt_stream_class *sc;
176
177 BT_ASSERT(msg);
178 BT_ASSERT_PRE_DEV_HOT(msg, "Message", ": %!+n", msg);
179 sc = stream_msg->stream->class;
180 BT_ASSERT(sc);
181 BT_ASSERT_PRE(sc->default_clock_class,
182 "Message's stream's class has no default clock class: "
183 "%![msg-]+n, %![sc-]+S", msg, sc);
184 BT_ASSERT(stream_msg->default_cs);
185 bt_clock_snapshot_set_raw_value(stream_msg->default_cs, raw_value);
186 stream_msg->default_cs_state = BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN;
187 BT_LIB_LOGD("Set stream message's default clock snapshot: "
188 "%![msg-]+n, value=%" PRIu64, msg, raw_value);
189 }
190
191 void bt_message_stream_beginning_set_default_clock_snapshot(
192 struct bt_message *message, uint64_t raw_value)
193 {
194 BT_ASSERT_PRE_NON_NULL(message, "Message");
195 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_BEGINNING);
196
197 bt_message_stream_set_default_clock_snapshot(message, raw_value);
198 }
199
200 void bt_message_stream_end_set_default_clock_snapshot(
201 struct bt_message *message, uint64_t raw_value)
202 {
203 BT_ASSERT_PRE_NON_NULL(message, "Message");
204 BT_ASSERT_PRE_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
205
206 return bt_message_stream_set_default_clock_snapshot(message, raw_value);
207 }
208
209 static enum bt_message_stream_clock_snapshot_state
210 bt_message_stream_borrow_default_clock_snapshot_const(
211 const bt_message *msg, const bt_clock_snapshot **snapshot)
212 {
213 struct bt_message_stream *stream_msg = (void *) msg;
214 struct bt_stream_class *sc;
215
216 BT_ASSERT_DBG(msg);
217 sc = stream_msg->stream->class;
218 BT_ASSERT_DBG(sc);
219 BT_ASSERT_PRE_DEV(sc->default_clock_class,
220 "Message's stream's class has no default clock class: "
221 "%![msg-]+n, %![sc-]+S", msg, sc);
222 BT_ASSERT_DBG(stream_msg->default_cs);
223
224 *snapshot = stream_msg->default_cs;
225
226 return stream_msg->default_cs_state;
227 }
228
229 enum bt_message_stream_clock_snapshot_state
230 bt_message_stream_beginning_borrow_default_clock_snapshot_const(
231 const bt_message *message, const bt_clock_snapshot **snapshot)
232 {
233 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
234 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message,
235 BT_MESSAGE_TYPE_STREAM_BEGINNING);
236 return bt_message_stream_borrow_default_clock_snapshot_const(
237 message, snapshot);
238 }
239
240 enum bt_message_stream_clock_snapshot_state
241 bt_message_stream_end_borrow_default_clock_snapshot_const(
242 const bt_message *message, const bt_clock_snapshot **snapshot)
243 {
244 BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
245 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_STREAM_END);
246 return bt_message_stream_borrow_default_clock_snapshot_const(
247 message, snapshot);
248 }
249
250 static inline
251 const struct bt_clock_class *
252 borrow_stream_message_stream_class_default_clock_class(
253 const struct bt_message *msg)
254 {
255 struct bt_message_stream *stream_msg = (void *) msg;
256
257 BT_ASSERT_DBG(msg);
258 return stream_msg->stream->class->default_clock_class;
259 }
260
261 const struct bt_clock_class *
262 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(
263 const struct bt_message *msg)
264 {
265 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
266 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg,
267 BT_MESSAGE_TYPE_STREAM_BEGINNING);
268 return borrow_stream_message_stream_class_default_clock_class(msg);
269 }
270
271 const struct bt_clock_class *
272 bt_message_stream_end_borrow_stream_class_default_clock_class_const(
273 const struct bt_message *msg)
274 {
275 BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
276 BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_STREAM_END);
277 return borrow_stream_message_stream_class_default_clock_class(msg);
278 }
This page took 0.035053 seconds and 4 git commands to generate.