lib: add internal object pool API and use it; adapt plugins/tests
[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(packet, (bt_object_release_func) bt_packet_recycle);
225 packet->stream = bt_get(stream);
226
227 if (trace->common.packet_header_field_type) {
228 BT_LOGD("Creating initial packet header field: ft-addr=%p",
229 trace->common.packet_header_field_type);
230 packet->header = bt_field_wrapper_create(
231 &trace->packet_header_field_pool,
232 (void *) trace->common.packet_header_field_type);
233 if (!packet->header) {
234 BT_LOGE("Cannot create packet header field wrapper.");
235 BT_PUT(packet);
236 goto end;
237 }
238 }
239
240 if (stream->common.stream_class->packet_context_field_type) {
241 BT_LOGD("Creating initial packet context field: ft-addr=%p",
242 stream->common.stream_class->packet_context_field_type);
243 packet->context = bt_field_wrapper_create(
244 &stream_class->packet_context_field_pool,
245 (void *) stream->common.stream_class->packet_context_field_type);
246 if (!packet->context) {
247 BT_LOGE("Cannot create packet context field wrapper.");
248 BT_PUT(packet);
249 goto end;
250 }
251 }
252
253 BT_LOGD("Created packet object: addr=%p", packet);
254
255 end:
256 return packet;
257 }
258
259 struct bt_packet *bt_packet_create(struct bt_stream *stream)
260 {
261 struct bt_packet *packet = NULL;
262
263 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
264 packet = bt_object_pool_create_object(&stream->packet_pool);
265 if (!packet) {
266 BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: "
267 "%![stream-]+s", stream);
268 goto error;
269 }
270
271 if (!packet->stream) {
272 packet->stream = bt_get(stream);
273 }
274
275 goto end;
276
277 error:
278 if (packet) {
279 bt_packet_recycle(packet);
280 packet = NULL;
281 }
282
283 end:
284 return packet;
285 }
286
287 int bt_packet_move_header(struct bt_packet *packet,
288 struct bt_packet_header_field *header_field)
289 {
290 struct bt_trace *trace;
291 struct bt_field_wrapper *field_wrapper = (void *) header_field;
292
293 BT_ASSERT_PRE_NON_NULL(packet, "Event");
294 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
295 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
296 trace = bt_stream_class_borrow_trace(
297 bt_stream_borrow_class(packet->stream));
298 BT_ASSERT_PRE(trace->common.packet_header_field_type,
299 "Trace has no packet header field type: %!+t",
300 trace);
301
302 /* TODO: compare field types (precondition) */
303
304 /* Recycle current header field: always exists */
305 BT_ASSERT(packet->header);
306 bt_packet_header_field_recycle(packet->header, trace);
307
308 /* Move new field */
309 packet->header = field_wrapper;
310 return 0;
311 }
312
313 int bt_packet_move_context(struct bt_packet *packet,
314 struct bt_packet_context_field *context_field)
315 {
316 struct bt_stream_class *stream_class;
317 struct bt_field_wrapper *field_wrapper = (void *) context_field;
318
319 BT_ASSERT_PRE_NON_NULL(packet, "Event");
320 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field");
321 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
322 stream_class = bt_stream_borrow_class(packet->stream);
323 BT_ASSERT_PRE(stream_class->common.packet_context_field_type,
324 "Stream class has no packet context field type: %!+S",
325 stream_class);
326
327 /* TODO: compare field types (precondition) */
328
329 /* Recycle current context field: always exists */
330 BT_ASSERT(packet->context);
331 bt_packet_context_field_recycle(packet->context, stream_class);
332
333 /* Move new field */
334 packet->context = field_wrapper;
335 return 0;
336 }
This page took 0.03503 seconds and 4 git commands to generate.