lib: make BT_ASSERT_{PRE,POST}() always on; add BT_ASSERT_{PRE,POST}_DEV()
[babeltrace.git] / src / lib / trace-ir / packet.c
1 /*
2 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/PACKET"
24 #include "lib/logging.h"
25
26 #include "lib/assert-pre.h"
27 #include <babeltrace2/trace-ir/packet-const.h>
28 #include <babeltrace2/trace-ir/packet.h>
29 #include <babeltrace2/trace-ir/trace.h>
30 #include <babeltrace2/trace-ir/stream-class.h>
31 #include <babeltrace2/trace-ir/stream.h>
32 #include "lib/object.h"
33 #include "common/assert.h"
34 #include <inttypes.h>
35
36 #include "field.h"
37 #include "field-wrapper.h"
38 #include "packet.h"
39 #include "stream-class.h"
40 #include "stream.h"
41 #include "trace.h"
42 #include "lib/func-status.h"
43
44 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
45 {
46 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
47 return packet->stream;
48 }
49
50 const struct bt_stream *bt_packet_borrow_stream_const(
51 const struct bt_packet *packet)
52 {
53 return bt_packet_borrow_stream((void *) packet);
54 }
55
56 struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
57 {
58 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
59 return packet->context_field ? packet->context_field->field : NULL;
60 }
61
62 const struct bt_field *bt_packet_borrow_context_field_const(
63 const struct bt_packet *packet)
64 {
65 return bt_packet_borrow_context_field((void *) packet);
66 }
67
68 BT_HIDDEN
69 void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
70 {
71 if (!packet) {
72 return;
73 }
74
75 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
76 "is-frozen=%d", packet, is_frozen);
77
78 if (packet->context_field) {
79 BT_LOGD_STR("Setting packet's context field's frozen state.");
80 bt_field_set_is_frozen(packet->context_field->field,
81 is_frozen);
82 }
83
84 ((struct bt_packet *) packet)->frozen = is_frozen;
85 }
86
87 static inline
88 void reset_packet(struct bt_packet *packet)
89 {
90 BT_ASSERT(packet);
91 BT_LIB_LOGD("Resetting packet: %!+a", packet);
92 bt_packet_set_is_frozen(packet, false);
93
94 if (packet->context_field) {
95 bt_field_set_is_frozen(packet->context_field->field, false);
96 bt_field_reset(packet->context_field->field);
97 }
98 }
99
100 static
101 void recycle_context_field(struct bt_field_wrapper *context_field,
102 struct bt_stream_class *stream_class)
103 {
104 BT_ASSERT(context_field);
105 BT_LIB_LOGD("Recycling packet context field: "
106 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
107 stream_class, context_field->field);
108 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
109 context_field);
110 }
111
112 BT_HIDDEN
113 void bt_packet_recycle(struct bt_packet *packet)
114 {
115 struct bt_stream *stream;
116
117 BT_ASSERT(packet);
118 BT_LIB_LOGD("Recycling packet: %!+a", packet);
119
120 /*
121 * Those are the important ordered steps:
122 *
123 * 1. Reset the packet object (put any permanent reference it
124 * has, unfreeze it and its fields in developer mode, etc.),
125 * but do NOT put its stream's reference. This stream
126 * contains the pool to which we're about to recycle this
127 * packet object, so we must guarantee its existence thanks
128 * to this existing reference.
129 *
130 * 2. Move the stream reference to our `stream`
131 * variable so that we can set the packet's stream member
132 * to NULL before recycling it. We CANNOT do this after
133 * we put the stream reference because this bt_object_put_ref()
134 * could destroy the stream, also destroying its
135 * packet pool, thus also destroying our packet object (this
136 * would result in an invalid write access).
137 *
138 * 3. Recycle the packet object.
139 *
140 * 4. Put our stream reference.
141 */
142 reset_packet(packet);
143 stream = packet->stream;
144 BT_ASSERT(stream);
145 packet->stream = NULL;
146 bt_object_pool_recycle_object(&stream->packet_pool, packet);
147 bt_object_put_no_null_check(&stream->base);
148 }
149
150 BT_HIDDEN
151 void bt_packet_destroy(struct bt_packet *packet)
152 {
153 BT_LIB_LOGD("Destroying packet: %!+a", packet);
154
155 if (packet->context_field) {
156 if (packet->stream) {
157 BT_LOGD_STR("Recycling packet's context field.");
158 recycle_context_field(packet->context_field,
159 packet->stream->class);
160 } else {
161 bt_field_wrapper_destroy(packet->context_field);
162 }
163
164 packet->context_field = NULL;
165 }
166
167 BT_LOGD_STR("Putting packet's stream.");
168 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
169 g_free(packet);
170 }
171
172 BT_HIDDEN
173 struct bt_packet *bt_packet_new(struct bt_stream *stream)
174 {
175 struct bt_packet *packet = NULL;
176 struct bt_trace_class *trace_class = NULL;
177
178 BT_ASSERT(stream);
179 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
180 packet = g_new0(struct bt_packet, 1);
181 if (!packet) {
182 BT_LIB_LOGE_APPEND_CAUSE(
183 "Failed to allocate one packet object.");
184 goto error;
185 }
186
187 bt_object_init_shared(&packet->base,
188 (bt_object_release_func) bt_packet_recycle);
189 packet->stream = stream;
190 bt_object_get_no_null_check(stream);
191 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
192 BT_ASSERT(trace_class);
193
194 if (stream->class->packet_context_fc) {
195 BT_LOGD_STR("Creating initial packet context field.");
196 packet->context_field = bt_field_wrapper_create(
197 &stream->class->packet_context_field_pool,
198 stream->class->packet_context_fc);
199 if (!packet->context_field) {
200 BT_LIB_LOGE_APPEND_CAUSE(
201 "Cannot create packet context field wrapper.");
202 goto error;
203 }
204 }
205
206 BT_LIB_LOGD("Created packet object: %!+a", packet);
207 goto end;
208
209 error:
210 BT_OBJECT_PUT_REF_AND_RESET(packet);
211
212 end:
213 return packet;
214 }
215
216 struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
217 {
218 struct bt_packet *packet = NULL;
219 struct bt_stream *stream = (void *) c_stream;
220
221 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
222 BT_ASSERT_PRE(stream->class->supports_packets,
223 "Stream class does not support packets: %![sc-]+S",
224 stream->class);
225 packet = bt_object_pool_create_object(&stream->packet_pool);
226 if (G_UNLIKELY(!packet)) {
227 BT_LIB_LOGE_APPEND_CAUSE(
228 "Cannot allocate one packet from stream's packet pool: "
229 "%![stream-]+s", stream);
230 goto end;
231 }
232
233 if (G_LIKELY(!packet->stream)) {
234 packet->stream = stream;
235 bt_object_get_no_null_check_no_parent_check(
236 &packet->stream->base);
237 }
238
239 end:
240 return (void *) packet;
241 }
242
243 enum bt_packet_move_context_field_status bt_packet_move_context_field(
244 struct bt_packet *packet,
245 struct bt_packet_context_field *context_field)
246 {
247 struct bt_stream_class *stream_class;
248 struct bt_field_wrapper *field_wrapper = (void *) context_field;
249
250 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
251 BT_ASSERT_PRE_DEV_NON_NULL(field_wrapper, "Context field");
252 BT_ASSERT_PRE_DEV_HOT(packet, "Packet", ": %!+a", packet);
253 stream_class = packet->stream->class;
254 BT_ASSERT_PRE_DEV(stream_class->packet_context_fc,
255 "Stream class has no packet context field class: %!+S",
256 stream_class);
257 BT_ASSERT_PRE_DEV(field_wrapper->field->class ==
258 stream_class->packet_context_fc,
259 "Unexpected packet context field's class: "
260 "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class,
261 stream_class->packet_context_fc);
262
263 /* Recycle current context field: always exists */
264 BT_ASSERT(packet->context_field);
265 recycle_context_field(packet->context_field, stream_class);
266
267 /* Move new field */
268 packet->context_field = field_wrapper;
269 return BT_FUNC_STATUS_OK;
270 }
271
272 void bt_packet_get_ref(const struct bt_packet *packet)
273 {
274 bt_object_get_ref(packet);
275 }
276
277 void bt_packet_put_ref(const struct bt_packet *packet)
278 {
279 bt_object_put_ref(packet);
280 }
This page took 0.039376 seconds and 4 git commands to generate.