lib: update and simplify the `bt_object` API
[babeltrace.git] / lib / ctf-ir / packet.c
1 /*
2 * packet.c
3 *
4 * Babeltrace CTF IR - Stream packet
5 *
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/fields-internal.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/packet-internal.h>
34 #include <babeltrace/ctf-ir/field-wrapper-internal.h>
35 #include <babeltrace/ctf-ir/trace.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/stream-class.h>
38 #include <babeltrace/ctf-ir/stream.h>
39 #include <babeltrace/ctf-ir/stream-internal.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/assert-internal.h>
44 #include <inttypes.h>
45
46 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
47 {
48 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
49 return packet->stream;
50 }
51
52 struct bt_field *bt_packet_borrow_header(struct bt_packet *packet)
53 {
54 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
55 return packet->header ? (void *) packet->header->field : NULL;
56 }
57
58 struct bt_field *bt_packet_borrow_context(struct bt_packet *packet)
59 {
60 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
61 return packet->context ? (void *) packet->context->field : NULL;
62 }
63
64 BT_HIDDEN
65 void _bt_packet_freeze(struct bt_packet *packet)
66 {
67 if (!packet || packet->frozen) {
68 return;
69 }
70
71 BT_LOGD("Freezing packet: addr=%p", packet);
72
73 if (packet->header) {
74 BT_LOGD_STR("Freezing packet's header field.");
75 bt_field_set_is_frozen_recursive((void *) packet->header->field, true);
76 }
77
78 if (packet->context) {
79 BT_LOGD_STR("Freezing packet's context field.");
80 bt_field_set_is_frozen_recursive((void *) packet->context->field, true);
81 }
82
83 packet->frozen = 1;
84 }
85
86 static inline
87 void bt_packet_reset(struct bt_packet *packet)
88 {
89 BT_ASSERT(packet);
90 packet->frozen = false;
91
92 if (packet->header) {
93 bt_field_set_is_frozen_recursive(
94 (void *) packet->header->field, false);
95 bt_field_reset_recursive((void *) packet->header->field);
96 }
97
98 if (packet->context) {
99 bt_field_set_is_frozen_recursive(
100 (void *) packet->context->field, false);
101 bt_field_reset_recursive((void *) packet->context->field);
102 }
103 }
104
105 static
106 void bt_packet_header_field_recycle(struct bt_field_wrapper *header_field,
107 struct bt_trace *trace)
108 {
109 BT_ASSERT(header_field);
110 BT_LIB_LOGD("Recycling packet header field: "
111 "addr=%p, %![trace-]+t, %![field-]+f", header_field,
112 trace, header_field->field);
113 bt_object_pool_recycle_object(&trace->packet_header_field_pool,
114 header_field);
115 }
116
117 static
118 void bt_packet_context_field_recycle(struct bt_field_wrapper *context_field,
119 struct bt_stream_class *stream_class)
120 {
121 BT_ASSERT(context_field);
122 BT_LIB_LOGD("Recycling packet context field: "
123 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
124 stream_class, context_field->field);
125 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
126 context_field);
127 }
128
129 BT_HIDDEN
130 void bt_packet_recycle(struct bt_packet *packet)
131 {
132 struct bt_stream *stream;
133
134 BT_ASSERT(packet);
135 BT_LIB_LOGD("Recycling packet: %!+a", packet);
136
137 /*
138 * Those are the important ordered steps:
139 *
140 * 1. Reset the packet object (put any permanent reference it
141 * has, unfreeze it and its fields in developer mode, etc.),
142 * but do NOT put its stream's reference. This stream
143 * contains the pool to which we're about to recycle this
144 * packet object, so we must guarantee its existence thanks
145 * to this existing reference.
146 *
147 * 2. Move the stream reference to our `stream`
148 * variable so that we can set the packet's stream member
149 * to NULL before recycling it. We CANNOT do this after
150 * we put the stream reference because this bt_put()
151 * could destroy the stream, also destroying its
152 * packet pool, thus also destroying our packet object (this
153 * would result in an invalid write access).
154 *
155 * 3. Recycle the packet object.
156 *
157 * 4. Put our stream reference.
158 */
159 bt_packet_reset(packet);
160 stream = packet->stream;
161 BT_ASSERT(stream);
162 packet->stream = NULL;
163 bt_object_pool_recycle_object(&stream->packet_pool, packet);
164 bt_put(stream);
165 }
166
167 BT_HIDDEN
168 void bt_packet_destroy(struct bt_packet *packet)
169 {
170 BT_LOGD("Destroying packet: addr=%p", packet);
171 BT_LOGD_STR("Destroying packet's header field.");
172
173 if (packet->header) {
174 if (packet->stream) {
175 BT_LOGD_STR("Recycling packet's header field.");
176 bt_packet_header_field_recycle(packet->header,
177 bt_stream_class_borrow_trace(
178 bt_stream_borrow_class(packet->stream)));
179 } else {
180 bt_field_wrapper_destroy(packet->header);
181 }
182 }
183
184 if (packet->context) {
185 if (packet->stream) {
186 BT_LOGD_STR("Recycling packet's context field.");
187 bt_packet_context_field_recycle(packet->context,
188 bt_stream_borrow_class(packet->stream));
189 } else {
190 bt_field_wrapper_destroy(packet->context);
191 }
192 }
193
194 BT_LOGD_STR("Putting packet's stream.");
195 bt_put(packet->stream);
196 g_free(packet);
197 }
198
199 BT_HIDDEN
200 struct bt_packet *bt_packet_new(struct bt_stream *stream)
201 {
202 struct bt_packet *packet = NULL;
203 struct bt_stream_class *stream_class = NULL;
204 struct bt_trace *trace = NULL;
205
206 BT_ASSERT(stream);
207 BT_LOGD("Creating packet object: stream-addr=%p, "
208 "stream-name=\"%s\", stream-class-addr=%p, "
209 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
210 stream, bt_stream_get_name(stream),
211 stream->common.stream_class,
212 bt_stream_class_common_get_name(stream->common.stream_class),
213 bt_stream_class_common_get_id(stream->common.stream_class));
214 stream_class = bt_stream_borrow_class(stream);
215 BT_ASSERT(stream_class);
216 trace = bt_stream_class_borrow_trace(stream_class);
217 BT_ASSERT(trace);
218 packet = g_new0(struct bt_packet, 1);
219 if (!packet) {
220 BT_LOGE_STR("Failed to allocate one packet object.");
221 goto end;
222 }
223
224 bt_object_init_shared(&packet->base,
225 (bt_object_release_func) bt_packet_recycle);
226 packet->stream = bt_get(stream);
227
228 if (trace->common.packet_header_field_type) {
229 BT_LOGD("Creating initial packet header field: ft-addr=%p",
230 trace->common.packet_header_field_type);
231 packet->header = bt_field_wrapper_create(
232 &trace->packet_header_field_pool,
233 (void *) trace->common.packet_header_field_type);
234 if (!packet->header) {
235 BT_LOGE("Cannot create packet header field wrapper.");
236 BT_PUT(packet);
237 goto end;
238 }
239 }
240
241 if (stream->common.stream_class->packet_context_field_type) {
242 BT_LOGD("Creating initial packet context field: ft-addr=%p",
243 stream->common.stream_class->packet_context_field_type);
244 packet->context = bt_field_wrapper_create(
245 &stream_class->packet_context_field_pool,
246 (void *) stream->common.stream_class->packet_context_field_type);
247 if (!packet->context) {
248 BT_LOGE("Cannot create packet context field wrapper.");
249 BT_PUT(packet);
250 goto end;
251 }
252 }
253
254 BT_LOGD("Created packet object: addr=%p", packet);
255
256 end:
257 return packet;
258 }
259
260 struct bt_packet *bt_packet_create(struct bt_stream *stream)
261 {
262 struct bt_packet *packet = NULL;
263
264 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
265 packet = bt_object_pool_create_object(&stream->packet_pool);
266 if (!packet) {
267 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
268 "%![stream-]+s", stream);
269 goto error;
270 }
271
272 if (!packet->stream) {
273 packet->stream = bt_get(stream);
274 }
275
276 goto end;
277
278 error:
279 if (packet) {
280 bt_packet_recycle(packet);
281 packet = NULL;
282 }
283
284 end:
285 return packet;
286 }
287
288 int bt_packet_move_header(struct bt_packet *packet,
289 struct bt_packet_header_field *header_field)
290 {
291 struct bt_trace *trace;
292 struct bt_field_wrapper *field_wrapper = (void *) header_field;
293
294 BT_ASSERT_PRE_NON_NULL(packet, "Event");
295 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
296 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
297 trace = bt_stream_class_borrow_trace(
298 bt_stream_borrow_class(packet->stream));
299 BT_ASSERT_PRE(trace->common.packet_header_field_type,
300 "Trace has no packet header field type: %!+t",
301 trace);
302
303 /* TODO: compare field types (precondition) */
304
305 /* Recycle current header field: always exists */
306 BT_ASSERT(packet->header);
307 bt_packet_header_field_recycle(packet->header, trace);
308
309 /* Move new field */
310 packet->header = field_wrapper;
311 return 0;
312 }
313
314 int bt_packet_move_context(struct bt_packet *packet,
315 struct bt_packet_context_field *context_field)
316 {
317 struct bt_stream_class *stream_class;
318 struct bt_field_wrapper *field_wrapper = (void *) context_field;
319
320 BT_ASSERT_PRE_NON_NULL(packet, "Event");
321 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
322 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
323 stream_class = bt_stream_borrow_class(packet->stream);
324 BT_ASSERT_PRE(stream_class->common.packet_context_field_type,
325 "Stream class has no packet context field type: %!+S",
326 stream_class);
327
328 /* TODO: compare field types (precondition) */
329
330 /* Recycle current context field: always exists */
331 BT_ASSERT(packet->context);
332 bt_packet_context_field_recycle(packet->context, stream_class);
333
334 /* Move new field */
335 packet->context = field_wrapper;
336 return 0;
337 }
This page took 0.038372 seconds and 5 git commands to generate.