lib: use common precond. assert. macros from `assert-cond.h` thru lib
[babeltrace.git] / src / lib / graph / message / event.c
index 285bf8aa86715fb6fae5be4a3f63f4560b8900bb..ac567bfa4a954b49bb771bcb11aa2efefb40120e 100644 (file)
@@ -1,32 +1,15 @@
 /*
+ * SPDX-License-Identifier: MIT
+ *
  * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
  * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
  */
 
 #define BT_LOG_TAG "LIB/MSG-EVENT"
 #include "lib/logging.h"
 
 #include "common/assert.h"
-#include "lib/assert-pre.h"
-#include "lib/assert-post.h"
+#include "lib/assert-cond.h"
 #include "compat/compiler.h"
 #include "lib/object.h"
 #include <babeltrace2/trace-ir/event.h>
@@ -36,8 +19,7 @@
 #include <babeltrace2/trace-ir/trace.h>
 #include "lib/trace-ir/clock-snapshot.h"
 #include "lib/graph/graph.h"
-#include <babeltrace2/graph/message-event-const.h>
-#include <babeltrace2/graph/message-event.h>
+#include <babeltrace2/graph/message.h>
 #include <babeltrace2/types.h>
 #include <stdbool.h>
 #include <inttypes.h>
@@ -49,8 +31,8 @@ static inline bool event_class_has_trace(struct bt_event_class *event_class)
        struct bt_stream_class *stream_class;
 
        stream_class = bt_event_class_borrow_stream_class_inline(event_class);
-       BT_ASSERT(stream_class);
-       return bt_stream_class_borrow_trace_class(stream_class) != NULL;
+       BT_ASSERT_DBG(stream_class);
+       return bt_stream_class_borrow_trace_class(stream_class);
 }
 
 BT_HIDDEN
@@ -77,6 +59,50 @@ end:
        return (void *) message;
 }
 
+static
+struct bt_event *create_event(struct bt_event_class *event_class,
+               struct bt_packet *packet, struct bt_stream *stream)
+{
+       struct bt_event *event = NULL;
+
+       BT_ASSERT_DBG(event_class);
+       BT_ASSERT_DBG(stream);
+       event = bt_object_pool_create_object(&event_class->event_pool);
+       if (G_UNLIKELY(!event)) {
+               BT_LIB_LOGE_APPEND_CAUSE(
+                       "Cannot allocate one event from event class's event pool: "
+                       "%![ec-]+E", event_class);
+               goto end;
+       }
+
+       if (G_LIKELY(!event->class)) {
+               event->class = event_class;
+               bt_object_get_ref_no_null_check(&event_class->base);
+       }
+
+       BT_ASSERT_DBG(!event->stream);
+       event->stream = stream;
+       bt_object_get_ref_no_null_check_no_parent_check(&event->stream->base);
+       BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s",
+               event, stream);
+
+       if (packet) {
+               BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
+                       event_class) == packet->stream->class,
+                       "Packet's stream class and event class's stream class differ: "
+                       "%![ec-]+E, %![packet-]+a", event, packet);
+               BT_ASSERT_DBG(event->stream->class->supports_packets);
+               BT_ASSERT_DBG(!event->packet);
+               event->packet = packet;
+               bt_object_get_ref_no_null_check_no_parent_check(&event->packet->base);
+               BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a",
+                       event, packet);
+       }
+
+end:
+       return event;
+}
+
 static inline
 struct bt_message *create_event_message(
                struct bt_self_message_iterator *self_msg_iter,
@@ -85,7 +111,7 @@ struct bt_message *create_event_message(
                const struct bt_stream *c_stream, bool with_cs,
                uint64_t raw_value)
 {
-       struct bt_self_component_port_input_message_iterator *msg_iter =
+       struct bt_message_iterator *msg_iter =
                (void *) self_msg_iter;
        struct bt_message_event *message = NULL;
        struct bt_event_class *event_class = (void *) c_event_class;
@@ -94,13 +120,17 @@ struct bt_message *create_event_message(
        struct bt_stream *stream = (void *) c_stream;
        struct bt_event *event;
 
-       BT_ASSERT(stream);
-       BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
-       BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
+       BT_ASSERT_DBG(stream);
+       BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
+       BT_ASSERT_PRE_EC_NON_NULL(event_class);
        BT_ASSERT_PRE(event_class_has_trace(event_class),
                "Event class is not part of a trace: %!+E", event_class);
+       BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(event_class) ==
+               stream->class,
+               "Stream's class and event's stream class differ: "
+               "%![ec-]+E, %![stream-]+s", event_class, stream);
        stream_class = bt_event_class_borrow_stream_class_inline(event_class);
-       BT_ASSERT(stream_class);
+       BT_ASSERT_DBG(stream_class);
        BT_ASSERT_PRE((with_cs && stream_class->default_clock_class) ||
                (!with_cs && !stream_class->default_clock_class),
                "Creating an event message with a default clock snapshot, but without "
@@ -110,7 +140,7 @@ struct bt_message *create_event_message(
                "cs-val=%" PRIu64,
                event_class, stream_class, with_cs, raw_value);
        BT_LIB_LOGD("Creating event message object: %![ec-]+E", event_class);
-       event = bt_event_create(event_class, packet, stream);
+       event = create_event(event_class, packet, stream);
        if (G_UNLIKELY(!event)) {
                BT_LIB_LOGE_APPEND_CAUSE(
                        "Cannot create event from event class: "
@@ -141,7 +171,7 @@ struct bt_message *create_event_message(
        }
 
        if (with_cs) {
-               BT_ASSERT(stream_class->default_clock_class);
+               BT_ASSERT_DBG(stream_class->default_clock_class);
                message->default_cs = bt_clock_snapshot_create(
                        stream_class->default_clock_class);
                if (!message->default_cs) {
@@ -151,7 +181,7 @@ struct bt_message *create_event_message(
                bt_clock_snapshot_set_raw_value(message->default_cs, raw_value);
        }
 
-       BT_ASSERT(!message->event);
+       BT_ASSERT_DBG(!message->event);
        message->event = event;
 
        if (packet) {
@@ -177,7 +207,8 @@ struct bt_message *bt_message_event_create(
                const struct bt_event_class *event_class,
                const struct bt_stream *stream)
 {
-       BT_ASSERT_PRE_NON_NULL(stream, "Stream");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_STREAM_NON_NULL(stream);
        return create_event_message(msg_iter, event_class, NULL, stream, false, 0);
 }
 
@@ -186,7 +217,8 @@ struct bt_message *bt_message_event_create_with_packet(
                const struct bt_event_class *event_class,
                const struct bt_packet *packet)
 {
-       BT_ASSERT_PRE_NON_NULL(packet, "Packet");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_PACKET_NON_NULL(packet);
        return create_event_message(msg_iter, event_class, packet,
                packet->stream, false, 0);
 }
@@ -197,7 +229,8 @@ struct bt_message *bt_message_event_create_with_default_clock_snapshot(
                const struct bt_stream *stream,
                uint64_t raw_value)
 {
-       BT_ASSERT_PRE_NON_NULL(stream, "Stream");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_STREAM_NON_NULL(stream);
        return create_event_message(msg_iter, event_class, NULL, stream,
                true, raw_value);
 }
@@ -209,7 +242,8 @@ bt_message_event_create_with_packet_and_default_clock_snapshot(
                const struct bt_packet *packet,
                uint64_t raw_value)
 {
-       BT_ASSERT_PRE_NON_NULL(packet, "Packet");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_PACKET_NON_NULL(packet);
        return create_event_message(msg_iter, event_class, packet,
                packet->stream, true, raw_value);
 }
@@ -241,7 +275,7 @@ void bt_message_event_recycle(struct bt_message *msg)
        struct bt_message_event *event_msg = (void *) msg;
        struct bt_graph *graph;
 
-       BT_ASSERT(event_msg);
+       BT_ASSERT_DBG(event_msg);
 
        if (G_UNLIKELY(!msg->graph)) {
                bt_message_event_destroy(msg);
@@ -251,7 +285,7 @@ void bt_message_event_recycle(struct bt_message *msg)
        BT_LIB_LOGD("Recycling event message: %![msg-]+n, %![event-]+e",
                msg, event_msg->event);
        bt_message_reset(msg);
-       BT_ASSERT(event_msg->event);
+       BT_ASSERT_DBG(event_msg->event);
        bt_event_recycle(event_msg->event);
        event_msg->event = NULL;
 
@@ -270,8 +304,8 @@ struct bt_event *borrow_event(struct bt_message *message)
 {
        struct bt_message_event *event_message;
 
-       BT_ASSERT_PRE_DEV_NON_NULL(message, "Message");
-       BT_ASSERT_PRE_DEV_MSG_IS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
+       BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(message, BT_MESSAGE_TYPE_EVENT);
        event_message = container_of(message,
                        struct bt_message_event, parent);
        return event_message->event;
@@ -296,14 +330,12 @@ bt_message_event_borrow_default_clock_snapshot_const(
        struct bt_message_event *event_msg = (void *) msg;
        struct bt_stream_class *stream_class;
 
-       BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
        stream_class = bt_event_class_borrow_stream_class_inline(
                event_msg->event->class);
-       BT_ASSERT(stream_class);
-       BT_ASSERT_PRE_DEV(stream_class->default_clock_class,
-               "Message's stream's class has no default clock class: "
-               "%![msg-]+n, %![sc-]+S", msg, stream_class);
+       BT_ASSERT_DBG(stream_class);
+       BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS(msg, stream_class);
        return event_msg->default_cs;
 }
 
@@ -314,10 +346,10 @@ bt_message_event_borrow_stream_class_default_clock_class_const(
        struct bt_message_event *event_msg = (void *) msg;
        struct bt_stream_class *stream_class;
 
-       BT_ASSERT_PRE_DEV_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_DEV_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_HAS_TYPE(msg, BT_MESSAGE_TYPE_EVENT);
        stream_class = bt_event_class_borrow_stream_class_inline(
                event_msg->event->class);
-       BT_ASSERT(stream_class);
+       BT_ASSERT_DBG(stream_class);
        return stream_class->default_clock_class;
 }
This page took 0.026302 seconds and 4 git commands to generate.