Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / stream-class.c
index d1dc9bda029563f56274c81c656082ddb3253b16..3ce610a5f77a80d7a4d3752f381081c3a2c1ed47 100644 (file)
@@ -48,6 +48,7 @@
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/align-internal.h>
 #include <babeltrace/endian-internal.h>
+#include <babeltrace/assert-internal.h>
 #include <inttypes.h>
 #include <stdint.h>
 #include <stdbool.h>
@@ -281,7 +282,7 @@ BT_HIDDEN
 void _bt_stream_class_set_id(
                struct bt_stream_class *stream_class, int64_t id)
 {
-       assert(stream_class);
+       BT_ASSERT(stream_class);
        stream_class->id = id;
        stream_class->id_set = 1;
        BT_LOGV("Set stream class's ID (internal): "
@@ -512,8 +513,8 @@ int bt_stream_class_add_event_class(
                 * The trace and stream class should be valid at this
                 * point.
                 */
-               assert(trace->valid);
-               assert(stream_class->valid);
+               BT_ASSERT(trace->valid);
+               BT_ASSERT(stream_class->valid);
                packet_header_type =
                        bt_trace_get_packet_header_type(trace);
                packet_context_type =
@@ -614,7 +615,7 @@ int bt_stream_class_add_event_class(
         * now if the stream class is frozen.
         */
        if (stream_class->frozen && expected_clock_class) {
-               assert(!stream_class->clock_class ||
+               BT_ASSERT(!stream_class->clock_class ||
                        stream_class->clock_class == expected_clock_class);
                BT_MOVE(stream_class->clock_class, expected_clock_class);
        }
@@ -642,12 +643,12 @@ end:
        BT_PUT(old_stream_class);
        bt_validation_output_put_types(&validation_output);
        bt_put(expected_clock_class);
-       assert(!packet_header_type);
-       assert(!packet_context_type);
-       assert(!event_header_type);
-       assert(!stream_event_ctx_type);
-       assert(!event_context_type);
-       assert(!event_payload_type);
+       BT_ASSERT(!packet_header_type);
+       BT_ASSERT(!packet_context_type);
+       BT_ASSERT(!event_header_type);
+       BT_ASSERT(!stream_event_ctx_type);
+       BT_ASSERT(!event_context_type);
+       BT_ASSERT(!event_payload_type);
        g_free(event_id);
 
        return ret;
@@ -1045,7 +1046,7 @@ int bt_stream_class_serialize(struct bt_stream_class *stream_class,
         * and serialization.
         */
        trace = bt_stream_class_borrow_trace(stream_class);
-       assert(trace);
+       BT_ASSERT(trace);
        packet_header_type = bt_trace_get_packet_header_type(trace);
        trace = NULL;
        if (packet_header_type) {
@@ -1280,20 +1281,27 @@ end:
 
 static
 int try_map_clock_class(struct bt_stream_class *stream_class,
-               struct bt_field_type *ft)
+               struct bt_field_type *parent_ft, const char *field_name)
 {
        struct bt_clock_class *mapped_clock_class = NULL;
        int ret = 0;
+       struct bt_field_type *ft =
+               bt_field_type_structure_get_field_type_by_name(parent_ft,
+                       field_name);
+
+       BT_ASSERT(stream_class->clock);
 
        if (!ft) {
                /* Field does not exist: not an error */
                goto end;
        }
 
-       assert(bt_field_type_is_integer(ft));
+       BT_ASSERT(bt_field_type_is_integer(ft));
        mapped_clock_class =
                bt_field_type_integer_get_mapped_clock_class(ft);
        if (!mapped_clock_class) {
+               struct bt_field_type *ft_copy;
+
                if (!stream_class->clock) {
                        BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
                                "stream-class-addr=%p, stream-class-name=\"%s\", "
@@ -1304,25 +1312,28 @@ int try_map_clock_class(struct bt_stream_class *stream_class,
                        goto end;
                }
 
-               ret = bt_field_type_integer_set_mapped_clock_class_no_check(
-                       ft, stream_class->clock->clock_class);
-               if (ret) {
-                       BT_LOGW("Cannot set field type's mapped clock class: "
-                               "stream-class-addr=%p, stream-class-name=\"%s\", "
-                               "stream-class-id=%" PRId64 ", ft-addr=%p",
-                               stream_class, bt_stream_class_get_name(stream_class),
-                               bt_stream_class_get_id(stream_class), ft);
-                       goto end;
+               ft_copy = bt_field_type_copy(ft);
+               if (!ft_copy) {
+                       BT_LOGE("Failed to copy integer field type: ft-addr=%p",
+                               ft);
                }
 
+               ret = bt_field_type_integer_set_mapped_clock_class_no_check(
+                       ft_copy, stream_class->clock->clock_class);
+               BT_ASSERT(ret == 0);
+               ret = bt_field_type_structure_replace_field(parent_ft,
+                       field_name, ft_copy);
+               bt_put(ft_copy);
                BT_LOGV("Automatically mapped field type to stream class's clock class: "
                        "stream-class-addr=%p, stream-class-name=\"%s\", "
-                       "stream-class-id=%" PRId64 ", ft-addr=%p",
+                       "stream-class-id=%" PRId64 ", ft-addr=%p, "
+                       "ft-copy-addr=%p",
                        stream_class, bt_stream_class_get_name(stream_class),
-                       bt_stream_class_get_id(stream_class), ft);
+                       bt_stream_class_get_id(stream_class), ft, ft_copy);
        }
 
 end:
+       bt_put(ft);
        bt_put(mapped_clock_class);
        return ret;
 }
@@ -1333,42 +1344,38 @@ int bt_stream_class_map_clock_class(
                struct bt_field_type *packet_context_type,
                struct bt_field_type *event_header_type)
 {
-       struct bt_field_type *ft = NULL;
        int ret = 0;
 
-       assert(stream_class);
+       BT_ASSERT(stream_class);
+
+       if (!stream_class->clock) {
+               /* No clock class to map to */
+               goto end;
+       }
 
        if (packet_context_type) {
-               ft = bt_field_type_structure_get_field_type_by_name(
-                       packet_context_type, "timestamp_begin");
-               if (try_map_clock_class(stream_class, ft)) {
+               if (try_map_clock_class(stream_class, packet_context_type,
+                               "timestamp_begin")) {
                        BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
                        ret = -1;
                        goto end;
                }
 
-               bt_put(ft);
-               ft = bt_field_type_structure_get_field_type_by_name(
-                       packet_context_type, "timestamp_end");
-               if (try_map_clock_class(stream_class, ft)) {
+               if (try_map_clock_class(stream_class, packet_context_type,
+                               "timestamp_end")) {
                        BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
                        ret = -1;
                        goto end;
                }
-
-               BT_PUT(ft);
        }
 
        if (event_header_type) {
-               ft = bt_field_type_structure_get_field_type_by_name(
-                       event_header_type, "timestamp");
-               if (try_map_clock_class(stream_class, ft)) {
+               if (try_map_clock_class(stream_class, event_header_type,
+                               "timestamp")) {
                        BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");
                        ret = -1;
                        goto end;
                }
-
-               BT_PUT(ft);
        }
 
 end:
@@ -1383,8 +1390,8 @@ int bt_stream_class_validate_single_clock_class(
        int ret;
        uint64_t i;
 
-       assert(stream_class);
-       assert(expected_clock_class);
+       BT_ASSERT(stream_class);
+       BT_ASSERT(expected_clock_class);
        ret = bt_validate_single_clock_class(stream_class->packet_context_type,
                expected_clock_class);
        if (ret) {
@@ -1440,7 +1447,7 @@ int bt_stream_class_validate_single_clock_class(
                struct bt_event_class *event_class =
                        g_ptr_array_index(stream_class->event_classes, i);
 
-               assert(event_class);
+               BT_ASSERT(event_class);
                ret = bt_event_class_validate_single_clock_class(event_class,
                        expected_clock_class);
                if (ret) {
This page took 0.025989 seconds and 4 git commands to generate.