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