doc/api/libbabeltrace2/DoxygenLayout.xml: use `topics` tab
[babeltrace.git] / src / lib / graph / message / packet.c
index 4a5a3e39b8dc42aa409bf662041e6136fcf685e7..b956b106b28ff7be73458110915fb68a2d69f602 100644 (file)
@@ -1,29 +1,16 @@
 /*
+ * 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-PACKET"
-#include "lib/lib-logging.h"
+#include "lib/logging.h"
+
+#include <stdbool.h>
 
+#include "lib/assert-cond.h"
 #include "compat/compiler.h"
 #include <babeltrace2/trace-ir/packet.h>
 #include "lib/trace-ir/packet.h"
 #include "lib/trace-ir/stream.h"
 #include "lib/trace-ir/stream-class.h"
 #include "lib/graph/graph.h"
-#include <babeltrace2/graph/message-packet-beginning-const.h>
-#include <babeltrace2/graph/message-packet-end-const.h>
-#include <babeltrace2/graph/message-packet-beginning.h>
-#include <babeltrace2/graph/message-packet-end.h>
+#include <babeltrace2/graph/message.h>
 #include "common/assert.h"
-#include "lib/assert-pre.h"
 #include "lib/object.h"
 #include <inttypes.h>
 
 #include "packet.h"
 
+#define BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(_msg)                        \
+       BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg),               \
+               "packet-beginning", BT_MESSAGE_TYPE_PACKET_BEGINNING)
+
+#define BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(_msg)                      \
+       BT_ASSERT_PRE_DEV_MSG_HAS_TYPE("message", (_msg),               \
+               "packet-end", BT_MESSAGE_TYPE_PACKET_END)
+
+#define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(_msg) \
+       BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-beginning-default-clock-snapshot", ({ \
+               const struct bt_packet *_packet = bt_message_packet_beginning_borrow_packet_const(_msg); \
+               const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \
+               const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \
+               bt_stream_class_packets_have_beginning_default_clock_snapshot(_stream_class); \
+       }), "Packets of the message's stream don't have a beginning default clock snapshot.")
+
+#define BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(_msg) \
+       BT_ASSERT_PRE_DEV("msg-stream-class-packets-have-end-default-clock-snapshot", ({ \
+               const struct bt_packet *_packet = bt_message_packet_end_borrow_packet_const(_msg); \
+               const struct bt_stream *_stream = bt_packet_borrow_stream_const(_packet); \
+               const struct bt_stream_class *_stream_class = bt_stream_borrow_class_const(_stream); \
+               bt_stream_class_packets_have_end_default_clock_snapshot(_stream_class); \
+       }), "Packets of the message's stream don't have an end default clock snapshot.")
+
 static inline
 struct bt_message *new_packet_message(struct bt_graph *graph,
                enum bt_message_type type, bt_object_release_func recycle_func)
@@ -51,7 +58,8 @@ struct bt_message *new_packet_message(struct bt_graph *graph,
 
        message = g_new0(struct bt_message_packet, 1);
        if (!message) {
-               BT_LOGE_STR("Failed to allocate one packet message.");
+               BT_LIB_LOGE_APPEND_CAUSE(
+                       "Failed to allocate one packet message.");
                goto error;
        }
 
@@ -65,14 +73,12 @@ end:
        return (void *) message;
 }
 
-BT_HIDDEN
 struct bt_message *bt_message_packet_beginning_new(struct bt_graph *graph)
 {
        return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_BEGINNING,
                (bt_object_release_func) bt_message_packet_beginning_recycle);
 }
 
-BT_HIDDEN
 struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
 {
        return new_packet_message(graph, BT_MESSAGE_TYPE_PACKET_END,
@@ -81,9 +87,9 @@ struct bt_message *bt_message_packet_end_new(struct bt_graph *graph)
 
 static inline
 struct bt_message *create_packet_message(
-               struct bt_self_component_port_input_message_iterator *msg_iter,
+               struct bt_message_iterator *msg_iter,
                struct bt_packet *packet, struct bt_object_pool *pool,
-               bool with_cs, uint64_t raw_value)
+               bool with_cs, uint64_t raw_value, const char *api_func)
 {
        struct bt_message_packet *message = NULL;
        struct bt_stream *stream;
@@ -91,12 +97,18 @@ struct bt_message *create_packet_message(
        bool need_cs;
 
        BT_ASSERT(msg_iter);
-       BT_ASSERT_PRE_NON_NULL(packet, "Packet");
+       BT_ASSERT_PRE_PACKET_NON_NULL_FROM_FUNC(api_func, packet);
        stream = bt_packet_borrow_stream(packet);
        BT_ASSERT(stream);
        stream_class = bt_stream_borrow_class(stream);
        BT_ASSERT(stream_class);
 
+       /*
+        * It's not possible to create a packet from a stream of which
+        * the class indicates that packets are not supported.
+        */
+       BT_ASSERT(stream_class->supports_packets);
+
        if (pool == &msg_iter->graph->packet_begin_msg_pool) {
                need_cs = stream_class->packets_have_beginning_default_clock_snapshot;
        } else {
@@ -107,14 +119,16 @@ struct bt_message *create_packet_message(
         * `packet_has_default_clock_snapshot` implies that the stream
         * class has a default clock class (precondition).
         */
-       BT_ASSERT_PRE(need_cs ? with_cs : true,
+       BT_ASSERT_PRE_FROM_FUNC(api_func, "with-default-clock-snapshot",
+               need_cs ? with_cs : true,
                "Unexpected stream class configuration when creating "
                "a packet beginning or end message: "
                "a default clock snapshot is needed, but none was provided: "
                "%![stream-]+s, %![sc-]+S, with-cs=%d, "
                "cs-val=%" PRIu64,
                stream, stream_class, with_cs, raw_value);
-       BT_ASSERT_PRE(!need_cs ? !with_cs : true,
+       BT_ASSERT_PRE_FROM_FUNC(api_func, "without-default-clock-snapshot",
+               !need_cs ? !with_cs : true,
                "Unexpected stream class configuration when creating "
                "a packet beginning or end message: "
                "no default clock snapshot is needed, but one was provided: "
@@ -135,7 +149,7 @@ struct bt_message *create_packet_message(
                message->default_cs = bt_clock_snapshot_create(
                        stream_class->default_clock_class);
                if (!message->default_cs) {
-                       bt_object_put_no_null_check(message);
+                       bt_object_put_ref_no_null_check(message);
                        message = NULL;
                        goto end;
                }
@@ -145,7 +159,7 @@ struct bt_message *create_packet_message(
 
        BT_ASSERT(!message->packet);
        message->packet = packet;
-       bt_object_get_no_null_check_no_parent_check(
+       bt_object_get_ref_no_null_check_no_parent_check(
                &message->packet->base);
        bt_packet_set_is_frozen(packet, true);
        BT_LIB_LOGD("Created packet message object: "
@@ -157,55 +171,64 @@ end:
        return (void *) message;
 }
 
+BT_EXPORT
 struct bt_message *bt_message_packet_beginning_create(
                struct bt_self_message_iterator *self_msg_iter,
                const struct bt_packet *packet)
 {
-       struct bt_self_component_port_input_message_iterator *msg_iter =
+       struct bt_message_iterator *msg_iter =
                (void *) self_msg_iter;
 
-       BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
        return create_packet_message(msg_iter, (void *) packet,
-               &msg_iter->graph->packet_begin_msg_pool, false, 0);
+               &msg_iter->graph->packet_begin_msg_pool, false, 0, __func__);
 }
 
+BT_EXPORT
 struct bt_message *bt_message_packet_beginning_create_with_default_clock_snapshot(
                struct bt_self_message_iterator *self_msg_iter,
                const struct bt_packet *packet, uint64_t raw_value)
 {
-       struct bt_self_component_port_input_message_iterator *msg_iter =
+       struct bt_message_iterator *msg_iter =
                (void *) self_msg_iter;
 
-       BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
        return create_packet_message(msg_iter, (void *) packet,
-               &msg_iter->graph->packet_begin_msg_pool, true, raw_value);
+               &msg_iter->graph->packet_begin_msg_pool, true, raw_value,
+               __func__);
 }
 
+BT_EXPORT
 struct bt_message *bt_message_packet_end_create(
                struct bt_self_message_iterator *self_msg_iter,
                const struct bt_packet *packet)
 {
-       struct bt_self_component_port_input_message_iterator *msg_iter =
+       struct bt_message_iterator *msg_iter =
                (void *) self_msg_iter;
 
-       BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
        return create_packet_message(msg_iter, (void *) packet,
-               &msg_iter->graph->packet_end_msg_pool, false, 0);
+               &msg_iter->graph->packet_end_msg_pool, false, 0, __func__);
 }
 
+BT_EXPORT
 struct bt_message *bt_message_packet_end_create_with_default_clock_snapshot(
                struct bt_self_message_iterator *self_msg_iter,
                const struct bt_packet *packet, uint64_t raw_value)
 {
-       struct bt_self_component_port_input_message_iterator *msg_iter =
+       struct bt_message_iterator *msg_iter =
                (void *) self_msg_iter;
 
-       BT_ASSERT_PRE_NON_NULL(msg_iter, "Message iterator");
+       BT_ASSERT_PRE_DEV_NO_ERROR();
+       BT_ASSERT_PRE_MSG_ITER_NON_NULL(msg_iter);
        return create_packet_message(msg_iter, (void *) packet,
-               &msg_iter->graph->packet_end_msg_pool, true, raw_value);
+               &msg_iter->graph->packet_end_msg_pool, true, raw_value,
+               __func__);
 }
 
-BT_HIDDEN
 void bt_message_packet_destroy(struct bt_message *msg)
 {
        struct bt_message_packet *packet_msg = (void *) msg;
@@ -229,7 +252,7 @@ void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
 
        BT_LIB_LOGD("Recycling packet message: %!+n", msg);
        bt_message_reset(msg);
-       bt_object_put_no_null_check(&packet_msg->packet->base);
+       bt_object_put_ref_no_null_check(&packet_msg->packet->base);
 
        if (packet_msg->default_cs) {
                bt_clock_snapshot_recycle(packet_msg->default_cs);
@@ -241,7 +264,6 @@ void recycle_packet_message(struct bt_message *msg, struct bt_object_pool *pool)
        bt_object_pool_recycle_object(pool, msg);
 }
 
-BT_HIDDEN
 void bt_message_packet_beginning_recycle(struct bt_message *msg)
 {
        BT_ASSERT(msg);
@@ -254,7 +276,6 @@ void bt_message_packet_beginning_recycle(struct bt_message *msg)
        recycle_packet_message(msg, &msg->graph->packet_begin_msg_pool);
 }
 
-BT_HIDDEN
 void bt_message_packet_end_recycle(struct bt_message *msg)
 {
        BT_ASSERT(msg);
@@ -267,17 +288,18 @@ void bt_message_packet_end_recycle(struct bt_message *msg)
        recycle_packet_message(msg, &msg->graph->packet_end_msg_pool);
 }
 
+BT_EXPORT
 struct bt_packet *bt_message_packet_beginning_borrow_packet(
                struct bt_message *message)
 {
        struct bt_message_packet *packet_msg = (void *) message;
 
-       BT_ASSERT_PRE_NON_NULL(message, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(message,
-               BT_MESSAGE_TYPE_PACKET_BEGINNING);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(message);
        return packet_msg->packet;
 }
 
+BT_EXPORT
 const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
                const struct bt_message *message)
 {
@@ -285,17 +307,18 @@ const struct bt_packet *bt_message_packet_beginning_borrow_packet_const(
                (void *) message);
 }
 
+BT_EXPORT
 struct bt_packet *bt_message_packet_end_borrow_packet(
                struct bt_message *message)
 {
        struct bt_message_packet *packet_msg = (void *) message;
 
-       BT_ASSERT_PRE_NON_NULL(message, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(message,
-               BT_MESSAGE_TYPE_PACKET_END);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(message);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(message);
        return packet_msg->packet;
 }
 
+BT_EXPORT
 const struct bt_packet *bt_message_packet_end_borrow_packet_const(
                const struct bt_message *message)
 {
@@ -303,6 +326,10 @@ const struct bt_packet *bt_message_packet_end_borrow_packet_const(
                (void *) message);
 }
 
+#define BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(_msg)                      \
+       BT_ASSERT_PRE_DEV_MSG_SC_DEF_CLK_CLS((_msg),                    \
+               ((struct bt_message_packet *) _msg)->packet->stream->class)
+
 static inline
 const struct bt_clock_snapshot *
 borrow_packet_message_default_clock_snapshot_const(
@@ -310,29 +337,31 @@ borrow_packet_message_default_clock_snapshot_const(
 {
        struct bt_message_packet *packet_msg = (void *) message;
 
-       BT_ASSERT(message);
-       BT_ASSERT_PRE(packet_msg->packet->stream->class->default_clock_class,
-               "Message's stream's class has no default clock class: "
-               "%![msg-]+n, %![sc-]+S",
-               message, packet_msg->packet->stream->class);
+       BT_ASSERT_DBG(message);
        return packet_msg->default_cs;
 }
 
+BT_EXPORT
 const struct bt_clock_snapshot *
 bt_message_packet_beginning_borrow_default_clock_snapshot_const(
                const struct bt_message *msg)
 {
-       BT_ASSERT_PRE_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(msg);
+       BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg);
+       BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_BEGINNING_DEF_CS(msg);
        return borrow_packet_message_default_clock_snapshot_const(msg);
 }
 
+BT_EXPORT
 const struct bt_clock_snapshot *
 bt_message_packet_end_borrow_default_clock_snapshot_const(
                const struct bt_message *msg)
 {
-       BT_ASSERT_PRE_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(msg);
+       BT_ASSERT_PRE_DEV_FOR_BORROW_DEF_CS(msg);
+       BT_ASSERT_PRE_DEV_MSG_STREAM_CLASS_PACKETS_HAVE_END_DEF_CS(msg);
        return borrow_packet_message_default_clock_snapshot_const(msg);
 }
 
@@ -343,24 +372,26 @@ borrow_packet_message_stream_class_default_clock_class(
 {
        struct bt_message_packet *packet_msg = (void *) msg;
 
-       BT_ASSERT(msg);
+       BT_ASSERT_DBG(msg);
        return packet_msg->packet->stream->class->default_clock_class;
 }
 
+BT_EXPORT
 const struct bt_clock_class *
 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
                const struct bt_message *msg)
 {
-       BT_ASSERT_PRE_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_BEGINNING);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_BEGINNING(msg);
        return borrow_packet_message_stream_class_default_clock_class(msg);
 }
 
+BT_EXPORT
 const struct bt_clock_class *
 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
                const struct bt_message *msg)
 {
-       BT_ASSERT_PRE_NON_NULL(msg, "Message");
-       BT_ASSERT_PRE_MSG_IS_TYPE(msg, BT_MESSAGE_TYPE_PACKET_END);
+       BT_ASSERT_PRE_DEV_MSG_NON_NULL(msg);
+       BT_ASSERT_PRE_DEV_MSG_IS_PACKET_END(msg);
        return borrow_packet_message_stream_class_default_clock_class(msg);
 }
This page took 0.030739 seconds and 4 git commands to generate.