+
+struct bt_packet *bt_packet_create(struct bt_stream *stream)
+{
+ struct bt_packet *packet = NULL;
+
+ BT_ASSERT_PRE_NON_NULL(stream, "Stream");
+ packet = bt_object_pool_create_object(&stream->packet_pool);
+ if (!packet) {
+ BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
+ "%![stream-]+s", stream);
+ goto error;
+ }
+
+ if (!packet->stream) {
+ packet->stream = bt_get(stream);
+ }
+
+ goto end;
+
+error:
+ if (packet) {
+ bt_packet_recycle(packet);
+ packet = NULL;
+ }
+
+end:
+ return packet;
+}
+
+int bt_packet_move_header(struct bt_packet *packet,
+ struct bt_packet_header_field *header_field)
+{
+ struct bt_trace *trace;
+ struct bt_field_wrapper *field_wrapper = (void *) header_field;
+
+ BT_ASSERT_PRE_NON_NULL(packet, "Event");
+ BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
+ BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
+ trace = bt_stream_class_borrow_trace(
+ bt_stream_borrow_class(packet->stream));
+ BT_ASSERT_PRE(trace->common.packet_header_field_type,
+ "Trace has no packet header field type: %!+t",
+ trace);
+
+ /* TODO: compare field types (precondition) */
+
+ /* Recycle current header field: always exists */
+ BT_ASSERT(packet->header);
+ bt_packet_header_field_recycle(packet->header, trace);
+
+ /* Move new field */
+ packet->header = field_wrapper;
+ return 0;
+}
+
+int bt_packet_move_context(struct bt_packet *packet,
+ struct bt_packet_context_field *context_field)
+{
+ struct bt_stream_class *stream_class;
+ struct bt_field_wrapper *field_wrapper = (void *) context_field;
+
+ BT_ASSERT_PRE_NON_NULL(packet, "Event");
+ BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
+ BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
+ stream_class = bt_stream_borrow_class(packet->stream);
+ BT_ASSERT_PRE(stream_class->common.packet_context_field_type,
+ "Stream class has no packet context field type: %!+S",
+ stream_class);
+
+ /* TODO: compare field types (precondition) */
+
+ /* Recycle current context field: always exists */
+ BT_ASSERT(packet->context);
+ bt_packet_context_field_recycle(packet->context, stream_class);
+
+ /* Move new field */
+ packet->context = field_wrapper;
+ return 0;
+}