Commit | Line | Data |
---|---|---|
f79cf0f0 | 1 | /* |
f2b0325d | 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 | ||
b03487ab | 23 | #define BT_LOG_TAG "LIB/PACKET" |
1633ef46 | 24 | #include "lib/logging.h" |
8ea705f0 | 25 | |
57952005 | 26 | #include "lib/assert-pre.h" |
71c5da58 MJ |
27 | #include <babeltrace2/trace-ir/packet-const.h> |
28 | #include <babeltrace2/trace-ir/packet.h> | |
71c5da58 | 29 | #include <babeltrace2/trace-ir/trace.h> |
71c5da58 MJ |
30 | #include <babeltrace2/trace-ir/stream-class.h> |
31 | #include <babeltrace2/trace-ir/stream.h> | |
57952005 MJ |
32 | #include "lib/object.h" |
33 | #include "common/assert.h" | |
be514b0c | 34 | #include <inttypes.h> |
969c1d8a | 35 | #include <stdbool.h> |
f79cf0f0 | 36 | |
57952005 MJ |
37 | #include "field.h" |
38 | #include "field-wrapper.h" | |
39 | #include "packet.h" | |
40 | #include "stream-class.h" | |
41 | #include "stream.h" | |
42 | #include "trace.h" | |
fb25b9e3 | 43 | #include "lib/func-status.h" |
57952005 | 44 | |
5fe68922 | 45 | struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet) |
f79cf0f0 | 46 | { |
fa6cfec3 | 47 | BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet"); |
5fe68922 | 48 | return packet->stream; |
f79cf0f0 PP |
49 | } |
50 | ||
78cf9df6 PP |
51 | const struct bt_stream *bt_packet_borrow_stream_const( |
52 | const struct bt_packet *packet) | |
9e550e5f | 53 | { |
78cf9df6 | 54 | return bt_packet_borrow_stream((void *) packet); |
9e550e5f PP |
55 | } |
56 | ||
7b33a0e0 | 57 | struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet) |
f79cf0f0 | 58 | { |
fa6cfec3 | 59 | BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet"); |
7b33a0e0 | 60 | return packet->context_field ? packet->context_field->field : NULL; |
a6918753 | 61 | } |
c5c82c6e | 62 | |
78cf9df6 PP |
63 | const struct bt_field *bt_packet_borrow_context_field_const( |
64 | const struct bt_packet *packet) | |
9e550e5f | 65 | { |
78cf9df6 | 66 | return bt_packet_borrow_context_field((void *) packet); |
9e550e5f PP |
67 | } |
68 | ||
a6918753 | 69 | BT_HIDDEN |
78cf9df6 | 70 | void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen) |
a6918753 | 71 | { |
435ec8af | 72 | if (!packet) { |
a6918753 | 73 | return; |
c5c82c6e PP |
74 | } |
75 | ||
7b33a0e0 PP |
76 | BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, " |
77 | "is-frozen=%d", packet, is_frozen); | |
a6918753 | 78 | |
7b33a0e0 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, | |
c5a24b0a | 82 | is_frozen); |
a6918753 PP |
83 | } |
84 | ||
78cf9df6 | 85 | ((struct bt_packet *) packet)->frozen = is_frozen; |
8b45963b PP |
86 | } |
87 | ||
a6918753 | 88 | static inline |
7b33a0e0 | 89 | void reset_packet(struct bt_packet *packet) |
8b45963b | 90 | { |
a6918753 | 91 | BT_ASSERT(packet); |
7b33a0e0 | 92 | BT_LIB_LOGD("Resetting packet: %!+a", packet); |
c5a24b0a | 93 | bt_packet_set_is_frozen(packet, false); |
a6918753 | 94 | |
7b33a0e0 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); | |
a6918753 | 98 | } |
f79cf0f0 PP |
99 | } |
100 | ||
a6918753 | 101 | static |
7b33a0e0 | 102 | void recycle_context_field(struct bt_field_wrapper *context_field, |
a6918753 | 103 | struct bt_stream_class *stream_class) |
f79cf0f0 | 104 | { |
a6918753 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 | |
a6918753 | 114 | void bt_packet_recycle(struct bt_packet *packet) |
f79cf0f0 | 115 | { |
a6918753 | 116 | struct bt_stream *stream; |
f79cf0f0 | 117 | |
a6918753 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 | |
8138bfe1 | 134 | * we put the stream reference because this bt_object_put_ref() |
a6918753 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 | */ | |
7b33a0e0 | 143 | reset_packet(packet); |
a6918753 PP |
144 | stream = packet->stream; |
145 | BT_ASSERT(stream); | |
146 | packet->stream = NULL; | |
147 | bt_object_pool_recycle_object(&stream->packet_pool, packet); | |
864aa43f | 148 | bt_object_put_ref_no_null_check(&stream->base); |
f79cf0f0 PP |
149 | } |
150 | ||
a6918753 PP |
151 | BT_HIDDEN |
152 | void bt_packet_destroy(struct bt_packet *packet) | |
f79cf0f0 | 153 | { |
7b33a0e0 | 154 | BT_LIB_LOGD("Destroying packet: %!+a", packet); |
a6918753 | 155 | |
7b33a0e0 | 156 | if (packet->context_field) { |
a6918753 PP |
157 | if (packet->stream) { |
158 | BT_LOGD_STR("Recycling packet's context field."); | |
7b33a0e0 PP |
159 | recycle_context_field(packet->context_field, |
160 | packet->stream->class); | |
a6918753 | 161 | } else { |
7b33a0e0 | 162 | bt_field_wrapper_destroy(packet->context_field); |
a6918753 | 163 | } |
1248f5ea PP |
164 | |
165 | packet->context_field = NULL; | |
a6918753 PP |
166 | } |
167 | ||
d409daba | 168 | BT_LOGD_STR("Putting packet's stream."); |
1248f5ea | 169 | BT_OBJECT_PUT_REF_AND_RESET(packet->stream); |
f79cf0f0 PP |
170 | g_free(packet); |
171 | } | |
172 | ||
a6918753 PP |
173 | BT_HIDDEN |
174 | struct bt_packet *bt_packet_new(struct bt_stream *stream) | |
f79cf0f0 | 175 | { |
839d52a5 | 176 | struct bt_packet *packet = NULL; |
10b7a2e4 | 177 | struct bt_trace_class *trace_class = NULL; |
f79cf0f0 | 178 | |
a6918753 | 179 | BT_ASSERT(stream); |
7b33a0e0 | 180 | BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream); |
839d52a5 | 181 | packet = g_new0(struct bt_packet, 1); |
f79cf0f0 | 182 | if (!packet) { |
a8f90e5d PP |
183 | BT_LIB_LOGE_APPEND_CAUSE( |
184 | "Failed to allocate one packet object."); | |
7b33a0e0 | 185 | goto error; |
f79cf0f0 PP |
186 | } |
187 | ||
1d7bf349 PP |
188 | bt_object_init_shared(&packet->base, |
189 | (bt_object_release_func) bt_packet_recycle); | |
4b70020d | 190 | packet->stream = stream; |
864aa43f | 191 | bt_object_get_ref_no_null_check(stream); |
10b7a2e4 PP |
192 | trace_class = bt_stream_class_borrow_trace_class_inline(stream->class); |
193 | BT_ASSERT(trace_class); | |
be514b0c | 194 | |
939190b3 | 195 | if (stream->class->packet_context_fc) { |
7b33a0e0 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, | |
939190b3 | 199 | stream->class->packet_context_fc); |
7b33a0e0 | 200 | if (!packet->context_field) { |
a8f90e5d PP |
201 | BT_LIB_LOGE_APPEND_CAUSE( |
202 | "Cannot create packet context field wrapper."); | |
7b33a0e0 | 203 | goto error; |
be514b0c | 204 | } |
f79cf0f0 PP |
205 | } |
206 | ||
7b33a0e0 PP |
207 | BT_LIB_LOGD("Created packet object: %!+a", packet); |
208 | goto end; | |
e5815ba2 | 209 | |
7b33a0e0 | 210 | error: |
8138bfe1 | 211 | BT_OBJECT_PUT_REF_AND_RESET(packet); |
e5815ba2 PP |
212 | |
213 | end: | |
7b33a0e0 | 214 | return packet; |
e5815ba2 PP |
215 | } |
216 | ||
6a6975d2 | 217 | struct bt_packet *bt_packet_create(const struct bt_stream *c_stream) |
a6918753 PP |
218 | { |
219 | struct bt_packet *packet = NULL; | |
6a6975d2 | 220 | struct bt_stream *stream = (void *) c_stream; |
a6918753 | 221 | |
7c7324d3 | 222 | BT_ASSERT_PRE_NO_ERROR(); |
a6918753 | 223 | BT_ASSERT_PRE_NON_NULL(stream, "Stream"); |
37a93d41 PP |
224 | BT_ASSERT_PRE(stream->class->supports_packets, |
225 | "Stream class does not support packets: %![sc-]+S", | |
226 | stream->class); | |
a6918753 | 227 | packet = bt_object_pool_create_object(&stream->packet_pool); |
85e7137b | 228 | if (G_UNLIKELY(!packet)) { |
a8f90e5d PP |
229 | BT_LIB_LOGE_APPEND_CAUSE( |
230 | "Cannot allocate one packet from stream's packet pool: " | |
a6918753 | 231 | "%![stream-]+s", stream); |
c5a24b0a | 232 | goto end; |
a6918753 PP |
233 | } |
234 | ||
85e7137b | 235 | if (G_LIKELY(!packet->stream)) { |
c5a24b0a | 236 | packet->stream = stream; |
864aa43f | 237 | bt_object_get_ref_no_null_check_no_parent_check( |
18acc6f8 | 238 | &packet->stream->base); |
a6918753 PP |
239 | } |
240 | ||
a6918753 | 241 | end: |
9e550e5f | 242 | return (void *) packet; |
a6918753 PP |
243 | } |
244 | ||
fb25b9e3 PP |
245 | enum bt_packet_move_context_field_status bt_packet_move_context_field( |
246 | struct bt_packet *packet, | |
78cf9df6 | 247 | struct bt_packet_context_field *context_field) |
a6918753 PP |
248 | { |
249 | struct bt_stream_class *stream_class; | |
250 | struct bt_field_wrapper *field_wrapper = (void *) context_field; | |
251 | ||
7c7324d3 | 252 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
fa6cfec3 PP |
253 | BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet"); |
254 | BT_ASSERT_PRE_DEV_NON_NULL(field_wrapper, "Context field"); | |
255 | BT_ASSERT_PRE_DEV_HOT(packet, "Packet", ": %!+a", packet); | |
7b33a0e0 | 256 | stream_class = packet->stream->class; |
fa6cfec3 | 257 | BT_ASSERT_PRE_DEV(stream_class->packet_context_fc, |
66fd07a5 | 258 | "Stream class has no packet context field class: %!+S", |
a6918753 | 259 | stream_class); |
fa6cfec3 | 260 | BT_ASSERT_PRE_DEV(field_wrapper->field->class == |
939190b3 | 261 | stream_class->packet_context_fc, |
0476f6f7 | 262 | "Unexpected packet context field's class: " |
939190b3 PP |
263 | "%![fc-]+F, %![expected-fc-]+F", field_wrapper->field->class, |
264 | stream_class->packet_context_fc); | |
a6918753 PP |
265 | |
266 | /* Recycle current context field: always exists */ | |
7b33a0e0 PP |
267 | BT_ASSERT(packet->context_field); |
268 | recycle_context_field(packet->context_field, stream_class); | |
a6918753 PP |
269 | |
270 | /* Move new field */ | |
7b33a0e0 | 271 | packet->context_field = field_wrapper; |
fb25b9e3 | 272 | return BT_FUNC_STATUS_OK; |
a6918753 | 273 | } |
8fc063a2 | 274 | |
8c6884d9 PP |
275 | void bt_packet_get_ref(const struct bt_packet *packet) |
276 | { | |
277 | bt_object_get_ref(packet); | |
278 | } | |
279 | ||
280 | void bt_packet_put_ref(const struct bt_packet *packet) | |
281 | { | |
282 | bt_object_put_ref(packet); | |
283 | } |