1c9268e8fdd2e4cff773867da761764f823cf16c
[babeltrace.git] / src / lib / trace-ir / packet.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/PACKET"
8 #include "lib/logging.h"
9
10 #include "lib/assert-pre.h"
11 #include <babeltrace2/trace-ir/packet.h>
12 #include <babeltrace2/trace-ir/trace.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/stream.h>
15 #include "lib/object.h"
16 #include "common/assert.h"
17 #include <inttypes.h>
18 #include <stdbool.h>
19
20 #include "field.h"
21 #include "field-wrapper.h"
22 #include "packet.h"
23 #include "stream-class.h"
24 #include "stream.h"
25 #include "trace.h"
26 #include "lib/func-status.h"
27
28 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
29 {
30 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
31 return packet->stream;
32 }
33
34 const struct bt_stream *bt_packet_borrow_stream_const(
35 const struct bt_packet *packet)
36 {
37 return bt_packet_borrow_stream((void *) packet);
38 }
39
40 struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
41 {
42 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
43 return packet->context_field ? packet->context_field->field : NULL;
44 }
45
46 const struct bt_field *bt_packet_borrow_context_field_const(
47 const struct bt_packet *packet)
48 {
49 return bt_packet_borrow_context_field((void *) packet);
50 }
51
52 BT_HIDDEN
53 void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
54 {
55 if (!packet) {
56 return;
57 }
58
59 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
60 "is-frozen=%d", packet, is_frozen);
61
62 if (packet->context_field) {
63 BT_LOGD_STR("Setting packet's context field's frozen state.");
64 bt_field_set_is_frozen(packet->context_field->field,
65 is_frozen);
66 }
67
68 ((struct bt_packet *) packet)->frozen = is_frozen;
69 }
70
71 static inline
72 void reset_packet(struct bt_packet *packet)
73 {
74 BT_ASSERT(packet);
75 BT_LIB_LOGD("Resetting packet: %!+a", packet);
76 bt_packet_set_is_frozen(packet, false);
77
78 if (packet->context_field) {
79 bt_field_set_is_frozen(packet->context_field->field, false);
80 bt_field_reset(packet->context_field->field);
81 }
82 }
83
84 static
85 void recycle_context_field(struct bt_field_wrapper *context_field,
86 struct bt_stream_class *stream_class)
87 {
88 BT_ASSERT(context_field);
89 BT_LIB_LOGD("Recycling packet context field: "
90 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
91 stream_class, context_field->field);
92 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
93 context_field);
94 }
95
96 BT_HIDDEN
97 void bt_packet_recycle(struct bt_packet *packet)
98 {
99 struct bt_stream *stream;
100
101 BT_ASSERT(packet);
102 BT_LIB_LOGD("Recycling packet: %!+a", packet);
103
104 /*
105 * Those are the important ordered steps:
106 *
107 * 1. Reset the packet object (put any permanent reference it
108 * has, unfreeze it and its fields in developer mode, etc.),
109 * but do NOT put its stream's reference. This stream
110 * contains the pool to which we're about to recycle this
111 * packet object, so we must guarantee its existence thanks
112 * to this existing reference.
113 *
114 * 2. Move the stream reference to our `stream`
115 * variable so that we can set the packet's stream member
116 * to NULL before recycling it. We CANNOT do this after
117 * we put the stream reference because this bt_object_put_ref()
118 * could destroy the stream, also destroying its
119 * packet pool, thus also destroying our packet object (this
120 * would result in an invalid write access).
121 *
122 * 3. Recycle the packet object.
123 *
124 * 4. Put our stream reference.
125 */
126 reset_packet(packet);
127 stream = packet->stream;
128 BT_ASSERT(stream);
129 packet->stream = NULL;
130 bt_object_pool_recycle_object(&stream->packet_pool, packet);
131 bt_object_put_ref_no_null_check(&stream->base);
132 }
133
134 BT_HIDDEN
135 void bt_packet_destroy(struct bt_packet *packet)
136 {
137 BT_LIB_LOGD("Destroying packet: %!+a", packet);
138
139 if (packet->context_field) {
140 if (packet->stream) {
141 BT_LOGD_STR("Recycling packet's context field.");
142 recycle_context_field(packet->context_field,
143 packet->stream->class);
144 } else {
145 bt_field_wrapper_destroy(packet->context_field);
146 }
147
148 packet->context_field = NULL;
149 }
150
151 BT_LOGD_STR("Putting packet's stream.");
152 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
153 g_free(packet);
154 }
155
156 BT_HIDDEN
157 struct bt_packet *bt_packet_new(struct bt_stream *stream)
158 {
159 struct bt_packet *packet = NULL;
160 struct bt_trace_class *trace_class = NULL;
161
162 BT_ASSERT(stream);
163 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
164 packet = g_new0(struct bt_packet, 1);
165 if (!packet) {
166 BT_LIB_LOGE_APPEND_CAUSE(
167 "Failed to allocate one packet object.");
168 goto error;
169 }
170
171 bt_object_init_shared(&packet->base,
172 (bt_object_release_func) bt_packet_recycle);
173 packet->stream = stream;
174 bt_object_get_ref_no_null_check(stream);
175 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
176 BT_ASSERT(trace_class);
177
178 if (stream->class->packet_context_fc) {
179 BT_LOGD_STR("Creating initial packet context field.");
180 packet->context_field = bt_field_wrapper_create(
181 &stream->class->packet_context_field_pool,
182 stream->class->packet_context_fc);
183 if (!packet->context_field) {
184 BT_LIB_LOGE_APPEND_CAUSE(
185 "Cannot create packet context field wrapper.");
186 goto error;
187 }
188 }
189
190 BT_LIB_LOGD("Created packet object: %!+a", packet);
191 goto end;
192
193 error:
194 BT_OBJECT_PUT_REF_AND_RESET(packet);
195
196 end:
197 return packet;
198 }
199
200 struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
201 {
202 struct bt_packet *packet = NULL;
203 struct bt_stream *stream = (void *) c_stream;
204
205 BT_ASSERT_PRE_NO_ERROR();
206 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
207 BT_ASSERT_PRE(stream->class->supports_packets,
208 "Stream class does not support packets: %![sc-]+S",
209 stream->class);
210 packet = bt_object_pool_create_object(&stream->packet_pool);
211 if (G_UNLIKELY(!packet)) {
212 BT_LIB_LOGE_APPEND_CAUSE(
213 "Cannot allocate one packet from stream's packet pool: "
214 "%![stream-]+s", stream);
215 goto end;
216 }
217
218 if (G_LIKELY(!packet->stream)) {
219 packet->stream = stream;
220 bt_object_get_ref_no_null_check_no_parent_check(
221 &packet->stream->base);
222 }
223
224 end:
225 return (void *) packet;
226 }
227
228 void bt_packet_get_ref(const struct bt_packet *packet)
229 {
230 bt_object_get_ref(packet);
231 }
232
233 void bt_packet_put_ref(const struct bt_packet *packet)
234 {
235 bt_object_put_ref(packet);
236 }
This page took 0.03348 seconds and 3 git commands to generate.