Commit | Line | Data |
---|---|---|
f79cf0f0 | 1 | /* |
e2f7325d | 2 | * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com> |
f79cf0f0 PP |
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 | ||
350ad6c1 | 23 | #define BT_LOG_TAG "LIB/PACKET" |
578e048b | 24 | #include "lib/lib-logging.h" |
8ea705f0 | 25 | |
578e048b | 26 | #include "lib/assert-pre.h" |
3fadfbc0 MJ |
27 | #include <babeltrace2/trace-ir/packet-const.h> |
28 | #include <babeltrace2/trace-ir/packet.h> | |
3fadfbc0 | 29 | #include <babeltrace2/trace-ir/trace.h> |
3fadfbc0 MJ |
30 | #include <babeltrace2/trace-ir/stream-class.h> |
31 | #include <babeltrace2/trace-ir/stream.h> | |
578e048b MJ |
32 | #include "lib/object.h" |
33 | #include "common/assert.h" | |
be514b0c | 34 | #include <inttypes.h> |
f79cf0f0 | 35 | |
578e048b MJ |
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 | ||
44c440bc PP |
43 | #define BT_ASSERT_PRE_PACKET_HOT(_packet) \ |
44 | BT_ASSERT_PRE_HOT((_packet), "Packet", ": %!+a", (_packet)) | |
45 | ||
094ff7c0 | 46 | struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet) |
f79cf0f0 | 47 | { |
f6ccaed9 | 48 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
094ff7c0 | 49 | return packet->stream; |
f79cf0f0 PP |
50 | } |
51 | ||
40f4ba76 PP |
52 | const struct bt_stream *bt_packet_borrow_stream_const( |
53 | const struct bt_packet *packet) | |
e5be10ef | 54 | { |
40f4ba76 | 55 | return bt_packet_borrow_stream((void *) packet); |
e5be10ef PP |
56 | } |
57 | ||
44c440bc | 58 | struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet) |
f79cf0f0 | 59 | { |
312c056a | 60 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
44c440bc | 61 | return packet->context_field ? packet->context_field->field : NULL; |
312c056a | 62 | } |
c5c82c6e | 63 | |
40f4ba76 PP |
64 | const struct bt_field *bt_packet_borrow_context_field_const( |
65 | const struct bt_packet *packet) | |
e5be10ef | 66 | { |
40f4ba76 | 67 | return bt_packet_borrow_context_field((void *) packet); |
e5be10ef PP |
68 | } |
69 | ||
312c056a | 70 | BT_HIDDEN |
40f4ba76 | 71 | void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen) |
312c056a | 72 | { |
59e3c4db | 73 | if (!packet) { |
312c056a | 74 | return; |
c5c82c6e PP |
75 | } |
76 | ||
44c440bc PP |
77 | BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, " |
78 | "is-frozen=%d", packet, is_frozen); | |
312c056a | 79 | |
44c440bc PP |
80 | if (packet->context_field) { |
81 | BT_LOGD_STR("Setting packet's context field's frozen state."); | |
82 | bt_field_set_is_frozen(packet->context_field->field, | |
6c677fb5 | 83 | is_frozen); |
312c056a PP |
84 | } |
85 | ||
40f4ba76 | 86 | ((struct bt_packet *) packet)->frozen = is_frozen; |
f6ccaed9 PP |
87 | } |
88 | ||
312c056a | 89 | static inline |
44c440bc | 90 | void reset_packet(struct bt_packet *packet) |
f6ccaed9 | 91 | { |
312c056a | 92 | BT_ASSERT(packet); |
44c440bc | 93 | BT_LIB_LOGD("Resetting packet: %!+a", packet); |
6c677fb5 | 94 | bt_packet_set_is_frozen(packet, false); |
312c056a | 95 | |
44c440bc PP |
96 | if (packet->context_field) { |
97 | bt_field_set_is_frozen(packet->context_field->field, false); | |
98 | bt_field_reset(packet->context_field->field); | |
312c056a | 99 | } |
f79cf0f0 PP |
100 | } |
101 | ||
312c056a | 102 | static |
44c440bc | 103 | void recycle_context_field(struct bt_field_wrapper *context_field, |
312c056a | 104 | struct bt_stream_class *stream_class) |
f79cf0f0 | 105 | { |
312c056a PP |
106 | BT_ASSERT(context_field); |
107 | BT_LIB_LOGD("Recycling packet context field: " | |
108 | "addr=%p, %![sc-]+S, %![field-]+f", context_field, | |
109 | stream_class, context_field->field); | |
110 | bt_object_pool_recycle_object(&stream_class->packet_context_field_pool, | |
111 | context_field); | |
f79cf0f0 PP |
112 | } |
113 | ||
114 | BT_HIDDEN | |
312c056a | 115 | void bt_packet_recycle(struct bt_packet *packet) |
f79cf0f0 | 116 | { |
312c056a | 117 | struct bt_stream *stream; |
f79cf0f0 | 118 | |
312c056a PP |
119 | BT_ASSERT(packet); |
120 | BT_LIB_LOGD("Recycling packet: %!+a", packet); | |
121 | ||
122 | /* | |
123 | * Those are the important ordered steps: | |
124 | * | |
125 | * 1. Reset the packet object (put any permanent reference it | |
126 | * has, unfreeze it and its fields in developer mode, etc.), | |
127 | * but do NOT put its stream's reference. This stream | |
128 | * contains the pool to which we're about to recycle this | |
129 | * packet object, so we must guarantee its existence thanks | |
130 | * to this existing reference. | |
131 | * | |
132 | * 2. Move the stream reference to our `stream` | |
133 | * variable so that we can set the packet's stream member | |
134 | * to NULL before recycling it. We CANNOT do this after | |
65300d60 | 135 | * we put the stream reference because this bt_object_put_ref() |
312c056a PP |
136 | * could destroy the stream, also destroying its |
137 | * packet pool, thus also destroying our packet object (this | |
138 | * would result in an invalid write access). | |
139 | * | |
140 | * 3. Recycle the packet object. | |
141 | * | |
142 | * 4. Put our stream reference. | |
143 | */ | |
44c440bc | 144 | reset_packet(packet); |
312c056a PP |
145 | stream = packet->stream; |
146 | BT_ASSERT(stream); | |
147 | packet->stream = NULL; | |
148 | bt_object_pool_recycle_object(&stream->packet_pool, packet); | |
cb6f1f7d | 149 | bt_object_put_no_null_check(&stream->base); |
f79cf0f0 PP |
150 | } |
151 | ||
312c056a PP |
152 | BT_HIDDEN |
153 | void bt_packet_destroy(struct bt_packet *packet) | |
f79cf0f0 | 154 | { |
44c440bc | 155 | BT_LIB_LOGD("Destroying packet: %!+a", packet); |
312c056a | 156 | |
44c440bc | 157 | if (packet->context_field) { |
312c056a PP |
158 | if (packet->stream) { |
159 | BT_LOGD_STR("Recycling packet's context field."); | |
44c440bc PP |
160 | recycle_context_field(packet->context_field, |
161 | packet->stream->class); | |
312c056a | 162 | } else { |
44c440bc | 163 | bt_field_wrapper_destroy(packet->context_field); |
312c056a | 164 | } |
238b7404 PP |
165 | |
166 | packet->context_field = NULL; | |
312c056a PP |
167 | } |
168 | ||
d409daba | 169 | BT_LOGD_STR("Putting packet's stream."); |
238b7404 | 170 | BT_OBJECT_PUT_REF_AND_RESET(packet->stream); |
f79cf0f0 PP |
171 | g_free(packet); |
172 | } | |
173 | ||
312c056a PP |
174 | BT_HIDDEN |
175 | struct bt_packet *bt_packet_new(struct bt_stream *stream) | |
f79cf0f0 | 176 | { |
50842bdc | 177 | struct bt_packet *packet = NULL; |
862ca4ed | 178 | struct bt_trace_class *trace_class = NULL; |
f79cf0f0 | 179 | |
312c056a | 180 | BT_ASSERT(stream); |
44c440bc | 181 | BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream); |
50842bdc | 182 | packet = g_new0(struct bt_packet, 1); |
f79cf0f0 | 183 | if (!packet) { |
8ea705f0 | 184 | BT_LOGE_STR("Failed to allocate one packet object."); |
44c440bc | 185 | goto error; |
f79cf0f0 PP |
186 | } |
187 | ||
3fea54f6 PP |
188 | bt_object_init_shared(&packet->base, |
189 | (bt_object_release_func) bt_packet_recycle); | |
398454ed PP |
190 | packet->stream = stream; |
191 | bt_object_get_no_null_check(stream); | |
862ca4ed PP |
192 | trace_class = bt_stream_class_borrow_trace_class_inline(stream->class); |
193 | BT_ASSERT(trace_class); | |
be514b0c | 194 | |
5cd6d0e5 | 195 | if (stream->class->packet_context_fc) { |
44c440bc PP |
196 | BT_LOGD_STR("Creating initial packet context field."); |
197 | packet->context_field = bt_field_wrapper_create( | |
198 | &stream->class->packet_context_field_pool, | |
5cd6d0e5 | 199 | stream->class->packet_context_fc); |
44c440bc PP |
200 | if (!packet->context_field) { |
201 | BT_LOGE_STR("Cannot create packet context field wrapper."); | |
202 | goto error; | |
be514b0c | 203 | } |
f79cf0f0 PP |
204 | } |
205 | ||
44c440bc PP |
206 | BT_LIB_LOGD("Created packet object: %!+a", packet); |
207 | goto end; | |
ccf82993 | 208 | |
44c440bc | 209 | error: |
65300d60 | 210 | BT_OBJECT_PUT_REF_AND_RESET(packet); |
ccf82993 PP |
211 | |
212 | end: | |
44c440bc | 213 | return packet; |
ccf82993 PP |
214 | } |
215 | ||
58085ca4 | 216 | struct bt_packet *bt_packet_create(const struct bt_stream *c_stream) |
312c056a PP |
217 | { |
218 | struct bt_packet *packet = NULL; | |
58085ca4 | 219 | struct bt_stream *stream = (void *) c_stream; |
312c056a PP |
220 | |
221 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); | |
222 | packet = bt_object_pool_create_object(&stream->packet_pool); | |
91d81473 | 223 | if (G_UNLIKELY(!packet)) { |
312c056a PP |
224 | BT_LIB_LOGE("Cannot allocate one packet from stream's packet pool: " |
225 | "%![stream-]+s", stream); | |
6c677fb5 | 226 | goto end; |
312c056a PP |
227 | } |
228 | ||
91d81473 | 229 | if (G_LIKELY(!packet->stream)) { |
6c677fb5 PP |
230 | packet->stream = stream; |
231 | bt_object_get_no_null_check_no_parent_check( | |
cb6f1f7d | 232 | &packet->stream->base); |
312c056a PP |
233 | } |
234 | ||
312c056a | 235 | end: |
e5be10ef | 236 | return (void *) packet; |
312c056a PP |
237 | } |
238 | ||
b029fcf8 | 239 | enum bt_packet_status bt_packet_move_context_field(struct bt_packet *packet, |
40f4ba76 | 240 | struct bt_packet_context_field *context_field) |
312c056a PP |
241 | { |
242 | struct bt_stream_class *stream_class; | |
243 | struct bt_field_wrapper *field_wrapper = (void *) context_field; | |
244 | ||
44c440bc | 245 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); |
312c056a | 246 | BT_ASSERT_PRE_NON_NULL(field_wrapper, "Context field"); |
e22b45d0 | 247 | BT_ASSERT_PRE_HOT(packet, "Packet", ": %!+a", packet); |
44c440bc | 248 | stream_class = packet->stream->class; |
5cd6d0e5 | 249 | BT_ASSERT_PRE(stream_class->packet_context_fc, |
e6276565 | 250 | "Stream class has no packet context field class: %!+S", |
312c056a | 251 | stream_class); |
5cd6d0e5 PP |
252 | BT_ASSERT_PRE(field_wrapper->field->class == |
253 | stream_class->packet_context_fc, | |
83ebb7f1 | 254 | "Unexpected packet context field's class: " |
5cd6d0e5 PP |
255 | "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class, |
256 | stream_class->packet_context_fc); | |
312c056a PP |
257 | |
258 | /* Recycle current context field: always exists */ | |
44c440bc PP |
259 | BT_ASSERT(packet->context_field); |
260 | recycle_context_field(packet->context_field, stream_class); | |
312c056a PP |
261 | |
262 | /* Move new field */ | |
44c440bc | 263 | packet->context_field = field_wrapper; |
b029fcf8 | 264 | return BT_PACKET_STATUS_OK; |
312c056a | 265 | } |
e22b45d0 | 266 | |
c5b9b441 PP |
267 | void bt_packet_get_ref(const struct bt_packet *packet) |
268 | { | |
269 | bt_object_get_ref(packet); | |
270 | } | |
271 | ||
272 | void bt_packet_put_ref(const struct bt_packet *packet) | |
273 | { | |
274 | bt_object_put_ref(packet); | |
275 | } |