Add a common, internal CTF serialization library; make CTF writer use it
[babeltrace.git] / lib / ctf-writer / stream.c
index 455027ce723332175c45ff4453c257b30a53f37f..3546facd0bf42a7dd1b155ea5516c5265387934e 100644 (file)
@@ -39,7 +39,8 @@
 #include <babeltrace/ctf-writer/trace-internal.h>
 #include <babeltrace/ctf-writer/trace.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
-#include <babeltrace/ref.h>
+#include <babeltrace/ctf-writer/object.h>
+#include <babeltrace/ctfser-internal.h>
 #include <inttypes.h>
 #include <stdint.h>
 #include <unistd.h>
@@ -59,12 +60,12 @@ BT_HIDDEN
 int bt_ctf_stream_common_initialize(
                struct bt_ctf_stream_common *stream,
                struct bt_ctf_stream_class_common *stream_class, const char *name,
-               uint64_t id, bt_object_release_func release_func)
+               uint64_t id, bt_ctf_object_release_func release_func)
 {
        int ret = 0;
        struct bt_ctf_trace_common *trace = NULL;
 
-       bt_object_init_shared_with_parent(&stream->base, release_func);
+       bt_ctf_object_init_shared_with_parent(&stream->base, release_func);
 
        if (!stream_class) {
                BT_LOGW_STR("Invalid parameter: stream class is NULL.");
@@ -113,7 +114,7 @@ int bt_ctf_stream_common_initialize(
         * Acquire reference to parent since stream will become publicly
         * reachable; it needs its parent to remain valid.
         */
-       bt_object_set_parent(&stream->base, &trace->base);
+       bt_ctf_object_set_parent(&stream->base, &trace->base);
        stream->stream_class = stream_class;
        stream->id = (int64_t) id;
 
@@ -139,7 +140,7 @@ end:
 }
 
 static
-void bt_ctf_stream_destroy(struct bt_object *obj);
+void bt_ctf_stream_destroy(struct bt_ctf_object *obj);
 
 static
 int try_set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
@@ -191,7 +192,7 @@ int set_integer_field_value(struct bt_ctf_field* field, uint64_t value)
                }
        }
 end:
-       bt_put(field_type);
+       bt_ctf_object_put_ref(field_type);
        return ret;
 }
 
@@ -228,7 +229,7 @@ int set_packet_header_magic(struct bt_ctf_stream *stream)
                        magic_field, (uint64_t) magic_value);
        }
 end:
-       bt_put(magic_field);
+       bt_ctf_object_put_ref(magic_field);
        return ret;
 }
 
@@ -252,7 +253,7 @@ int set_packet_header_uuid(struct bt_ctf_stream *stream)
        }
 
        trace = (struct bt_ctf_trace *)
-               bt_object_get_parent(&stream->common.base);
+               bt_ctf_object_get_parent(&stream->common.base);
 
        for (i = 0; i < 16; i++) {
                struct bt_ctf_field *uuid_element =
@@ -260,7 +261,7 @@ int set_packet_header_uuid(struct bt_ctf_stream *stream)
 
                ret = bt_ctf_field_integer_unsigned_set_value(
                        uuid_element, (uint64_t) trace->common.uuid[i]);
-               bt_put(uuid_element);
+               bt_ctf_object_put_ref(uuid_element);
                if (ret) {
                        BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
                                "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
@@ -276,8 +277,8 @@ int set_packet_header_uuid(struct bt_ctf_stream *stream)
                stream, bt_ctf_stream_get_name(stream), uuid_field);
 
 end:
-       bt_put(uuid_field);
-       BT_PUT(trace);
+       bt_ctf_object_put_ref(uuid_field);
+       BT_CTF_OBJECT_PUT_REF_AND_RESET(trace);
        return ret;
 }
 static
@@ -313,7 +314,7 @@ int set_packet_header_stream_id(struct bt_ctf_stream *stream)
        }
 
 end:
-       bt_put(stream_id_field);
+       bt_ctf_object_put_ref(stream_id_field);
        return ret;
 }
 
@@ -359,43 +360,33 @@ end:
 }
 
 static
-int set_packet_context_packet_size(struct bt_ctf_stream *stream)
+int set_packet_context_packet_size(struct bt_ctf_stream *stream,
+               uint64_t packet_size_bits)
 {
        int ret = 0;
        struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
                stream->packet_context, "packet_size");
 
-       BT_ASSERT(stream);
-
-       if (!field) {
-               /* No packet size field found. Not an error, skip. */
-               BT_LOGV("No field named `packet_size` in packet context: skipping: "
-                       "stream-addr=%p, stream-name=\"%s\"",
-                       stream, bt_ctf_stream_get_name(stream));
-               goto end;
-       }
-
-       ret = bt_ctf_field_integer_unsigned_set_value(field,
-               stream->pos.packet_size);
+       ret = bt_ctf_field_integer_unsigned_set_value(field, packet_size_bits);
        if (ret) {
                BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
                        "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
                        stream, bt_ctf_stream_get_name(stream),
-                       field, stream->pos.packet_size);
+                       field, packet_size_bits);
        } else {
                BT_LOGV("Set packet context field's `packet_size` field's value: "
                        "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
                        stream, bt_ctf_stream_get_name(stream),
-                       field, stream->pos.packet_size);
+                       field, packet_size_bits);
        }
 
-end:
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        return ret;
 }
 
 static
-int set_packet_context_content_size(struct bt_ctf_stream *stream)
+int set_packet_context_content_size(struct bt_ctf_stream *stream,
+               uint64_t content_size_bits)
 {
        int ret = 0;
        struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
@@ -411,22 +402,21 @@ int set_packet_context_content_size(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       ret = bt_ctf_field_integer_unsigned_set_value(field,
-               stream->pos.offset);
+       ret = bt_ctf_field_integer_unsigned_set_value(field, content_size_bits);
        if (ret) {
                BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
-                       "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64,
+                       "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
                        stream, bt_ctf_stream_get_name(stream),
-                       field, stream->pos.offset);
+                       field, content_size_bits);
        } else {
                BT_LOGV("Set packet context field's `content_size` field's value: "
-                       "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64,
+                       "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64,
                        stream, bt_ctf_stream_get_name(stream),
-                       field, stream->pos.offset);
+                       field, content_size_bits);
        }
 
 end:
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        return ret;
 }
 
@@ -494,7 +484,7 @@ int set_packet_context_events_discarded(struct bt_ctf_stream *stream)
        }
 
 end:
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        return ret;
 }
 
@@ -554,7 +544,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
                        goto end;
                }
 
-               bt_put(cc);
+               bt_ctf_object_put_ref(cc);
                val_size = bt_ctf_field_type_integer_get_size(
                        (void *) field_common->type);
                BT_ASSERT(val_size >= 1);
@@ -584,7 +574,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
 
                BT_ASSERT(int_field);
                ret = visit_field_update_clock_value(int_field, val);
-               bt_put(int_field);
+               bt_ctf_object_put_ref(int_field);
                break;
        }
        case BT_CTF_FIELD_TYPE_ID_ARRAY:
@@ -601,7 +591,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
 
                        BT_ASSERT(elem_field);
                        ret = visit_field_update_clock_value(elem_field, val);
-                       bt_put(elem_field);
+                       bt_ctf_object_put_ref(elem_field);
                        if (ret) {
                                goto end;
                        }
@@ -625,7 +615,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
 
                        BT_ASSERT(elem_field);
                        ret = visit_field_update_clock_value(elem_field, val);
-                       bt_put(elem_field);
+                       bt_ctf_object_put_ref(elem_field);
                        if (ret) {
                                goto end;
                        }
@@ -646,7 +636,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
 
                        BT_ASSERT(member_field);
                        ret = visit_field_update_clock_value(member_field, val);
-                       bt_put(member_field);
+                       bt_ctf_object_put_ref(member_field);
                        if (ret) {
                                goto end;
                        }
@@ -664,7 +654,7 @@ int visit_field_update_clock_value(struct bt_ctf_field *field, uint64_t *val)
                }
 
                ret = visit_field_update_clock_value(cur_field, val);
-               bt_put(cur_field);
+               bt_ctf_object_put_ref(cur_field);
                break;
        }
        default:
@@ -682,7 +672,7 @@ int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val)
 
        field = bt_ctf_event_get_header(event);
        ret = visit_field_update_clock_value(field, val);
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        if (ret) {
                BT_LOGW_STR("Cannot automatically update clock value in "
                        "event's header.");
@@ -691,7 +681,7 @@ int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val)
 
        field = bt_ctf_event_get_stream_event_context(event);
        ret = visit_field_update_clock_value(field, val);
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        if (ret) {
                BT_LOGW_STR("Cannot automatically update clock value in "
                        "event's stream event context.");
@@ -700,7 +690,7 @@ int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val)
 
        field = bt_ctf_event_get_context(event);
        ret = visit_field_update_clock_value(field, val);
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        if (ret) {
                BT_LOGW_STR("Cannot automatically update clock value in "
                        "event's context.");
@@ -709,7 +699,7 @@ int visit_event_update_clock_value(struct bt_ctf_event *event, uint64_t *val)
 
        field = bt_ctf_event_get_payload_field(event);
        ret = visit_field_update_clock_value(field, val);
-       bt_put(field);
+       bt_ctf_object_put_ref(field);
        if (ret) {
                BT_LOGW_STR("Cannot automatically update clock value in "
                        "event's payload.");
@@ -796,31 +786,31 @@ int set_packet_context_timestamps(struct bt_ctf_stream *stream)
 
                if (strcmp(member_name, "packet_size") == 0 &&
                                !bt_ctf_field_is_set_recursive(member_field)) {
-                       bt_put(member_field);
+                       bt_ctf_object_put_ref(member_field);
                        continue;
                }
 
                if (strcmp(member_name, "content_size") == 0 &&
                                !bt_ctf_field_is_set_recursive(member_field)) {
-                       bt_put(member_field);
+                       bt_ctf_object_put_ref(member_field);
                        continue;
                }
 
                if (strcmp(member_name, "events_discarded") == 0 &&
                                !bt_ctf_field_is_set_recursive(member_field)) {
-                       bt_put(member_field);
+                       bt_ctf_object_put_ref(member_field);
                        continue;
                }
 
                if (strcmp(member_name, "packet_seq_num") == 0 &&
                                !bt_ctf_field_is_set_recursive(member_field)) {
-                       bt_put(member_field);
+                       bt_ctf_object_put_ref(member_field);
                        continue;
                }
 
                ret = visit_field_update_clock_value(member_field,
                        &cur_clock_value);
-               bt_put(member_field);
+               bt_ctf_object_put_ref(member_field);
                if (ret) {
                        BT_LOGW("Cannot automatically update clock value "
                                "in stream's packet context: "
@@ -894,13 +884,14 @@ int set_packet_context_timestamps(struct bt_ctf_stream *stream)
        }
 
 end:
-       bt_put(ts_begin_field);
-       bt_put(ts_end_field);
+       bt_ctf_object_put_ref(ts_begin_field);
+       bt_ctf_object_put_ref(ts_end_field);
        return ret;
 }
 
 static
-int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts)
+int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts,
+               uint64_t packet_size_bits, uint64_t content_size_bits)
 {
        int ret = 0;
 
@@ -908,7 +899,7 @@ int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts)
                goto end;
        }
 
-       ret = set_packet_context_packet_size(stream);
+       ret = set_packet_context_packet_size(stream, packet_size_bits);
        if (ret) {
                BT_LOGW("Cannot set packet context's packet size field: "
                        "stream-addr=%p, stream-name=\"%s\"",
@@ -916,7 +907,7 @@ int auto_populate_packet_context(struct bt_ctf_stream *stream, bool set_ts)
                goto end;
        }
 
-       ret = set_packet_context_content_size(stream);
+       ret = set_packet_context_content_size(stream, content_size_bits);
        if (ret) {
                BT_LOGW("Cannot set packet context's content size field: "
                        "stream-addr=%p, stream-name=\"%s\"",
@@ -953,16 +944,16 @@ end:
 static
 void release_event(struct bt_ctf_event *event)
 {
-       if (bt_object_get_ref_count(&event->common.base)) {
+       if (bt_ctf_object_get_ref_count(&event->common.base)) {
                /*
                 * The event is being orphaned, but it must guarantee the
                 * existence of its event class for the duration of its
                 * lifetime.
                 */
-               bt_get(event->common.class);
-               BT_PUT(event->common.base.parent);
+               bt_ctf_object_get_ref(event->common.class);
+               BT_CTF_OBJECT_PUT_REF_AND_RESET(event->common.base.parent);
        } else {
-               bt_object_try_spec_release(&event->common.base);
+               bt_ctf_object_try_spec_release(&event->common.base);
        }
 }
 
@@ -970,7 +961,7 @@ static
 int create_stream_file(struct bt_ctf_writer *writer,
                struct bt_ctf_stream *stream)
 {
-       int fd;
+       int ret = 0;
        GString *filename = g_string_new(NULL);
        int64_t stream_class_id;
        char *file_path = NULL;
@@ -1028,36 +1019,25 @@ append_ids:
 
        file_path = g_build_filename(writer->path->str, filename->str, NULL);
        if (file_path == NULL) {
-               fd = -1;
+               ret = -1;
                goto end;
        }
 
-       fd = open(file_path,
-               O_RDWR | O_CREAT | O_TRUNC,
-               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       ret = bt_ctfser_init(&stream->ctfser, file_path);
        g_free(file_path);
-       if (fd < 0) {
-               BT_LOGW_ERRNO("Failed to open stream file for writing",
-                       ": file_path=\"%s\", filename=\"%s\", ret=%d",
-                       file_path, filename->str, fd);
+       if (ret) {
+               /* bt_ctfser_init() logs errors */
                goto end;
        }
 
        BT_LOGD("Created stream file for writing: "
                "stream-addr=%p, stream-name=\"%s\", "
-               "filename=\"%s\", fd=%d", stream, bt_ctf_stream_get_name(stream),
-               filename->str, fd);
+               "filename=\"%s\"", stream, bt_ctf_stream_get_name(stream),
+               filename->str);
 
 end:
        g_string_free(filename, TRUE);
-       return fd;
-}
-
-static
-void set_stream_fd(struct bt_ctf_stream *stream, int fd)
-{
-       (void) bt_ctf_stream_pos_init(&stream->pos, fd, O_RDWR);
-       stream->pos.fd = fd;
+       return ret;
 }
 
 BT_HIDDEN
@@ -1104,9 +1084,8 @@ struct bt_ctf_stream *bt_ctf_stream_create_with_id(
                goto error;
        }
 
-       stream->pos.fd = -1;
        writer = (struct bt_ctf_writer *)
-               bt_object_get_parent(&trace->common.base);
+               bt_ctf_object_get_parent(&trace->common.base);
        stream->last_ts_end = -1ULL;
        BT_LOGD("CTF writer stream object belongs writer's trace: "
                "writer-addr=%p", writer);
@@ -1174,8 +1153,6 @@ struct bt_ctf_stream *bt_ctf_stream_create_with_id(
                goto error;
        }
 
-       set_stream_fd(stream, fd);
-
        /* Freeze the writer */
        BT_LOGD_STR("Freezing stream's CTF writer.");
        bt_ctf_writer_freeze(writer);
@@ -1187,10 +1164,10 @@ struct bt_ctf_stream *bt_ctf_stream_create_with_id(
        goto end;
 
 error:
-       BT_PUT(stream);
+       BT_CTF_OBJECT_PUT_REF_AND_RESET(stream);
 
 end:
-       bt_put(writer);
+       bt_ctf_object_put_ref(writer);
        return stream;
 }
 
@@ -1219,14 +1196,6 @@ int bt_ctf_stream_get_discarded_events_count(
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
-                       "stream-addr=%p, stream-name=\"%s\"",
-                       stream, bt_ctf_stream_get_name(stream));
-               ret = -1;
-               goto end;
-       }
-
        *count = (uint64_t) stream->discarded_events;
 
 end:
@@ -1260,7 +1229,7 @@ int set_packet_context_events_discarded_field(struct bt_ctf_stream *stream,
        }
 
 end:
-       bt_put(events_discarded_field);
+       bt_ctf_object_put_ref(events_discarded_field);
        return ret;
 }
 
@@ -1285,11 +1254,6 @@ void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
-               goto end;
-       }
-
        events_discarded_field = bt_ctf_field_structure_get_field_by_name(
                stream->packet_context, "events_discarded");
        if (!events_discarded_field) {
@@ -1317,7 +1281,7 @@ void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
                stream, bt_ctf_stream_get_name(stream), event_count);
 
 end:
-       bt_put(events_discarded_field);
+       bt_ctf_object_put_ref(events_discarded_field);
 }
 
 static int auto_populate_event_header(struct bt_ctf_stream *stream,
@@ -1404,9 +1368,9 @@ static int auto_populate_event_header(struct bt_ctf_stream *stream,
                stream, bt_ctf_stream_get_name(stream), event);
 
 end:
-       bt_put(id_field);
-       bt_put(timestamp_field);
-       bt_put(mapped_clock_class);
+       bt_ctf_object_put_ref(id_field);
+       bt_ctf_object_put_ref(timestamp_field);
+       bt_ctf_object_put_ref(mapped_clock_class);
        return ret;
 }
 
@@ -1427,12 +1391,6 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
-               ret = -1;
-               goto end;
-       }
-
        BT_LOGV("Appending event to stream: "
                "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
                "event-class-name=\"%s\", event-class-id=%" PRId64,
@@ -1455,7 +1413,7 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       bt_object_set_parent(&event->common.base, &stream->common.base);
+       bt_ctf_object_set_parent(&event->common.base, &stream->common.base);
        BT_LOGV_STR("Automatically populating the header of the event to append.");
        ret = auto_populate_event_header(stream, event);
        if (ret) {
@@ -1480,7 +1438,7 @@ int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
         * longer needed.
         */
        BT_LOGV_STR("Putting the event's class.");
-       bt_put(event->common.class);
+       bt_ctf_object_put_ref(event->common.class);
        BT_LOGV("Appended event to stream: "
                "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
                "event-class-name=\"%s\", event-class-id=%" PRId64,
@@ -1498,7 +1456,7 @@ error:
         * Orphan the event; we were not successful in associating it to
         * a stream.
         */
-       bt_object_set_parent(&event->common.base, NULL);
+       bt_ctf_object_set_parent(&event->common.base, NULL);
        return ret;
 }
 
@@ -1511,16 +1469,9 @@ struct bt_ctf_field *bt_ctf_stream_get_packet_context(struct bt_ctf_stream *stre
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
-                       "stream-addr=%p, stream-name=\"%s\"", stream,
-                       bt_ctf_stream_get_name(stream));
-               goto end;
-       }
-
        packet_context = stream->packet_context;
        if (packet_context) {
-               bt_get(packet_context);
+               bt_ctf_object_get_ref(packet_context);
        }
 end:
        return packet_context;
@@ -1538,12 +1489,6 @@ int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
-               ret = -1;
-               goto end;
-       }
-
        field_type = bt_ctf_field_get_type(field);
        if (bt_ctf_field_type_common_compare((void *) field_type,
                        stream->common.stream_class->packet_context_field_type)) {
@@ -1557,9 +1502,9 @@ int bt_ctf_stream_set_packet_context(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       bt_put(field_type);
-       bt_put(stream->packet_context);
-       stream->packet_context = bt_get(field);
+       bt_ctf_object_put_ref(field_type);
+       bt_ctf_object_put_ref(stream->packet_context);
+       stream->packet_context = bt_ctf_object_get_ref(field);
        BT_LOGV("Set stream's packet context field: "
                "stream-addr=%p, stream-name=\"%s\", "
                "packet-context-field-addr=%p",
@@ -1577,16 +1522,9 @@ struct bt_ctf_field *bt_ctf_stream_get_packet_header(struct bt_ctf_stream *strea
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
-                       "stream-addr=%p, stream-name=\"%s\"", stream,
-                       bt_ctf_stream_get_name(stream));
-               goto end;
-       }
-
        packet_header = stream->packet_header;
        if (packet_header) {
-               bt_get(packet_header);
+               bt_ctf_object_get_ref(packet_header);
        }
 end:
        return packet_header;
@@ -1605,14 +1543,8 @@ int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
                goto end;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
-               ret = -1;
-               goto end;
-       }
-
        trace = (struct bt_ctf_trace *)
-               bt_object_get_parent(&stream->common.base);
+               bt_ctf_object_get_parent(&stream->common.base);
 
        if (!field) {
                if (trace->common.packet_header_field_type) {
@@ -1645,15 +1577,15 @@ int bt_ctf_stream_set_packet_header(struct bt_ctf_stream *stream,
        }
 
 skip_validation:
-       bt_put(stream->packet_header);
-       stream->packet_header = bt_get(field);
+       bt_ctf_object_put_ref(stream->packet_header);
+       stream->packet_header = bt_ctf_object_get_ref(field);
        BT_LOGV("Set stream's packet header field: "
                "stream-addr=%p, stream-name=\"%s\", "
                "packet-header-field-addr=%p",
                stream, bt_ctf_stream_get_name(stream), field);
 end:
-       BT_PUT(trace);
-       bt_put(field_type);
+       BT_CTF_OBJECT_PUT_REF_AND_RESET(trace);
+       bt_ctf_object_put_ref(field_type);
        return ret;
 }
 
@@ -1665,7 +1597,7 @@ void reset_structure_field(struct bt_ctf_field *structure, const char *name)
        member = bt_ctf_field_structure_get_field_by_name(structure, name);
        if (member) {
                bt_ctf_field_common_reset_recursive((void *) member);
-               bt_put(member);
+               bt_ctf_object_put_ref(member);
        }
 }
 
@@ -1673,10 +1605,12 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 {
        int ret = 0;
        size_t i;
-       struct bt_ctf_stream_pos packet_context_pos;
+       uint64_t packet_context_offset_bits;
        struct bt_ctf_trace *trace;
        enum bt_ctf_byte_order native_byte_order;
        bool has_packet_size = false;
+       uint64_t packet_size_bits = 0;
+       uint64_t content_size_bits = 0;
 
        if (!stream) {
                BT_LOGW_STR("Invalid parameter: stream is NULL.");
@@ -1684,19 +1618,13 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                goto end_no_stream;
        }
 
-       if (stream->pos.fd < 0) {
-               BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
-               ret = -1;
-               goto end;
-       }
-
        if (stream->packet_context) {
                struct bt_ctf_field *packet_size_field;
 
                packet_size_field = bt_ctf_field_structure_get_field_by_name(
                                stream->packet_context, "packet_size");
                has_packet_size = (packet_size_field != NULL);
-               bt_put(packet_size_field);
+               bt_ctf_object_put_ref(packet_size_field);
        }
 
        if (stream->flushed_packet_count == 1) {
@@ -1728,23 +1656,25 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                goto end;
        }
 
-       ret = auto_populate_packet_context(stream, true);
+       /* Initialize packet/content sizes to `0`; we will overwrite later */
+       ret = auto_populate_packet_context(stream, true, 0, 0);
        if (ret) {
                BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
                ret = -1;
                goto end;
        }
 
-       /* mmap the next packet */
-       BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64,
-               stream->pos.offset);
-       bt_ctf_stream_pos_packet_seek(&stream->pos, 0, SEEK_CUR);
-       BT_ASSERT(stream->pos.packet_size % 8 == 0);
+       ret = bt_ctfser_open_packet(&stream->ctfser);
+       if (ret) {
+               /* bt_ctfser_open_packet() logs errors */
+               ret = -1;
+               goto end;
+       }
 
        if (stream->packet_header) {
-               BT_LOGV_STR("Serializing packet header field.");
+               BT_LOGV_STR("Serializing packet header field (initial).");
                ret = bt_ctf_field_serialize_recursive(stream->packet_header,
-                       &stream->pos, native_byte_order);
+                       &stream->ctfser, native_byte_order);
                if (ret) {
                        BT_LOGW("Cannot serialize stream's packet header field: "
                                "field-addr=%p", stream->packet_header);
@@ -1753,12 +1683,15 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
        }
 
        if (stream->packet_context) {
+               /* Save packet context's position to overwrite it later */
+               packet_context_offset_bits =
+                       bt_ctfser_get_offset_in_current_packet_bits(
+                               &stream->ctfser);
+
                /* Write packet context */
-               memcpy(&packet_context_pos, &stream->pos,
-                       sizeof(packet_context_pos));
-               BT_LOGV_STR("Serializing packet context field.");
+               BT_LOGV_STR("Serializing packet context field (initial).");
                ret = bt_ctf_field_serialize_recursive(stream->packet_context,
-                       &stream->pos, native_byte_order);
+                       &stream->ctfser, native_byte_order);
                if (ret) {
                        BT_LOGW("Cannot serialize stream's packet context field: "
                                "field-addr=%p", stream->packet_context);
@@ -1777,17 +1710,18 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 
                BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
                        "event-class-name=\"%s\", event-class-id=%" PRId64 ", "
-                       "pos-offset=%" PRId64 ", packet-size=%" PRIu64,
+                       "ser-offset=%" PRIu64,
                        i, event, bt_ctf_event_class_get_name(event_class),
                        bt_ctf_event_class_get_id(event_class),
-                       stream->pos.offset, stream->pos.packet_size);
+                       bt_ctfser_get_offset_in_current_packet_bits(
+                               &stream->ctfser));
 
                /* Write event header */
                if (event->common.header_field) {
                        BT_LOGV_STR("Serializing event's header field.");
                        ret = bt_ctf_field_serialize_recursive(
                                (void *) event->common.header_field->field,
-                               &stream->pos, native_byte_order);
+                               &stream->ctfser, native_byte_order);
                        if (ret) {
                                BT_LOGW("Cannot serialize event's header field: "
                                        "field-addr=%p",
@@ -1801,7 +1735,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                        BT_LOGV_STR("Serializing event's stream event context field.");
                        ret = bt_ctf_field_serialize_recursive(
                                (void *) event->common.stream_event_context_field,
-                               &stream->pos, native_byte_order);
+                               &stream->ctfser, native_byte_order);
                        if (ret) {
                                BT_LOGW("Cannot serialize event's stream event context field: "
                                        "field-addr=%p",
@@ -1811,50 +1745,50 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                }
 
                /* Write event content */
-               ret = bt_ctf_event_serialize(event,
-                       &stream->pos, native_byte_order);
+               ret = bt_ctf_event_serialize(event, &stream->ctfser,
+                       native_byte_order);
                if (ret) {
                        /* bt_ctf_event_serialize() logs errors */
                        goto end;
                }
        }
 
-       if (!has_packet_size && stream->pos.offset % 8 != 0) {
+       content_size_bits = bt_ctfser_get_offset_in_current_packet_bits(
+               &stream->ctfser);
+
+       if (!has_packet_size && content_size_bits % 8 != 0) {
                BT_LOGW("Stream's packet context field type has no `packet_size` field, "
                        "but current content size is not a multiple of 8 bits: "
-                       "content-size=%" PRId64 ", "
+                       "content-size=%" PRIu64 ", "
                        "packet-size=%" PRIu64,
-                       stream->pos.offset,
-                       stream->pos.packet_size);
+                       content_size_bits,
+                       packet_size_bits);
                ret = -1;
                goto end;
        }
 
-       BT_ASSERT(stream->pos.packet_size % 8 == 0);
-
-       /*
-        * Remove extra padding bytes.
-        */
-       stream->pos.packet_size = (stream->pos.offset + 7) & ~7;
+       /* Set packet size; make it a multiple of 8 */
+       packet_size_bits = (content_size_bits + 7) & ~UINT64_C(7);
 
        if (stream->packet_context) {
                /*
-                * The whole packet is serialized at this point. Make sure that,
-                * if `packet_size` is missing, the current content size is
-                * equal to the current packet size.
+                * The whole packet is serialized at this point. Make
+                * sure that, if `packet_size` is missing, the current
+                * content size is equal to the current packet size.
                 */
-               struct bt_ctf_field *field = bt_ctf_field_structure_get_field_by_name(
-                       stream->packet_context, "content_size");
+               struct bt_ctf_field *field =
+                       bt_ctf_field_structure_get_field_by_name(
+                               stream->packet_context, "content_size");
 
-               bt_put(field);
+               bt_ctf_object_put_ref(field);
                if (!field) {
-                       if (stream->pos.offset != stream->pos.packet_size) {
+                       if (content_size_bits != packet_size_bits) {
                                BT_LOGW("Stream's packet context's `content_size` field is missing, "
                                        "but current packet's content size is not equal to its packet size: "
-                                       "content-size=%" PRId64 ", "
+                                       "content-size=%" PRIu64 ", "
                                        "packet-size=%" PRIu64,
-                                       stream->pos.offset,
-                                       stream->pos.packet_size);
+                                       bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser),
+                                       packet_size_bits);
                                ret = -1;
                                goto end;
                        }
@@ -1864,12 +1798,11 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
                 * Overwrite the packet context now that the stream
                 * position's packet and content sizes have the correct
                 * values.
-                *
-                * Copy base_mma as the packet may have been remapped
-                * (e.g. when a packet is resized).
                 */
-               packet_context_pos.base_mma = stream->pos.base_mma;
-               ret = auto_populate_packet_context(stream, false);
+               bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser,
+                       packet_context_offset_bits);
+               ret = auto_populate_packet_context(stream, false,
+                       packet_size_bits, content_size_bits);
                if (ret) {
                        BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
                        ret = -1;
@@ -1878,7 +1811,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 
                BT_LOGV("Rewriting (serializing) packet context field.");
                ret = bt_ctf_field_serialize_recursive(stream->packet_context,
-                       &packet_context_pos, native_byte_order);
+                       &stream->ctfser, native_byte_order);
                if (ret) {
                        BT_LOGW("Cannot serialize stream's packet context field: "
                                "field-addr=%p", stream->packet_context);
@@ -1888,7 +1821,7 @@ int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
 
        g_ptr_array_set_size(stream->events, 0);
        stream->flushed_packet_count++;
-       stream->size += stream->pos.packet_size / CHAR_BIT;
+       bt_ctfser_close_current_packet(&stream->ctfser, packet_size_bits / 8);
 
 end:
        /* Reset automatically-set fields. */
@@ -1900,18 +1833,10 @@ end:
                reset_structure_field(stream->packet_context, "events_discarded");
        }
 
-       if (ret < 0) {
-               /*
-                * We failed to write the packet. Its size is therefore set to 0
-                * to ensure the next mapping is done in the same place rather
-                * than advancing by "stream->pos.packet_size", which would
-                * leave a corrupted packet in the trace.
-                */
-               stream->pos.packet_size = 0;
-       } else {
-               BT_LOGV("Flushed stream's current packet: content-size=%" PRId64 ", "
-                       "packet-size=%" PRIu64,
-                       stream->pos.offset, stream->pos.packet_size);
+       if (ret == 0) {
+               BT_LOGV("Flushed stream's current packet: "
+                       "content-size=%" PRIu64 ", packet-size=%" PRIu64,
+                       content_size_bits, packet_size_bits);
        }
 
 end_no_stream:
@@ -1919,7 +1844,7 @@ end_no_stream:
 }
 
 static
-void bt_ctf_stream_destroy(struct bt_object *obj)
+void bt_ctf_stream_destroy(struct bt_ctf_object *obj)
 {
        struct bt_ctf_stream *stream = (void *) obj;
 
@@ -1927,30 +1852,7 @@ void bt_ctf_stream_destroy(struct bt_object *obj)
                stream, bt_ctf_stream_get_name(stream));
 
        bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream));
-       (void) bt_ctf_stream_pos_fini(&stream->pos);
-
-       if (stream->pos.fd >= 0) {
-               int ret;
-
-               /*
-                * Truncate the file's size to the minimum required to fit the
-                * last packet as we might have grown it too much on the last
-                * mmap.
-                */
-               do {
-                       ret = ftruncate(stream->pos.fd, stream->size);
-               } while (ret == -1 && errno == EINTR);
-               if (ret) {
-                       BT_LOGE_ERRNO("Failed to truncate stream file",
-                               ": ret=%d, size=%" PRIu64,
-                               ret, (uint64_t) stream->size);
-               }
-
-               if (close(stream->pos.fd)) {
-                       BT_LOGE_ERRNO("Failed to close stream file",
-                               ": ret=%d", ret);
-               }
-       }
+       bt_ctfser_fini(&stream->ctfser);
 
        if (stream->events) {
                BT_LOGD_STR("Putting events.");
@@ -1958,9 +1860,9 @@ void bt_ctf_stream_destroy(struct bt_object *obj)
        }
 
        BT_LOGD_STR("Putting packet header field.");
-       bt_put(stream->packet_header);
+       bt_ctf_object_put_ref(stream->packet_header);
        BT_LOGD_STR("Putting packet context field.");
-       bt_put(stream->packet_context);
+       bt_ctf_object_put_ref(stream->packet_context);
        g_free(stream);
 }
 
@@ -2016,8 +1918,8 @@ int _set_structure_field_integer(struct bt_ctf_field *structure, char *name,
        }
        ret = !ret ? 1 : ret;
 end:
-       bt_put(integer);
-       bt_put(field_type);
+       bt_ctf_object_put_ref(integer);
+       bt_ctf_object_put_ref(field_type);
        return ret;
 }
 
@@ -2037,7 +1939,7 @@ int try_set_structure_field_integer(struct bt_ctf_field *structure, char *name,
 struct bt_ctf_stream_class *bt_ctf_stream_get_class(
                struct bt_ctf_stream *stream)
 {
-       return bt_get(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream)));
+       return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream)));
 }
 
 const char *bt_ctf_stream_get_name(struct bt_ctf_stream *stream)
This page took 0.071541 seconds and 4 git commands to generate.