cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / trace-ir / packet.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016-2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/PACKET"
8 #include "lib/logging.h"
9
10 #include "lib/assert-cond.h"
11 #include <babeltrace2/trace-ir/packet.h>
12 #include <babeltrace2/trace-ir/trace.h>
13 #include <babeltrace2/trace-ir/stream-class.h>
14 #include <babeltrace2/trace-ir/stream.h>
15 #include "lib/object.h"
16 #include "common/assert.h"
17 #include <stdbool.h>
18
19 #include "field.h"
20 #include "field-wrapper.h"
21 #include "packet.h"
22 #include "stream-class.h"
23 #include "stream.h"
24
25 BT_EXPORT
26 struct bt_stream *bt_packet_borrow_stream(struct bt_packet *packet)
27 {
28 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet);
29 return packet->stream;
30 }
31
32 BT_EXPORT
33 const struct bt_stream *bt_packet_borrow_stream_const(
34 const struct bt_packet *packet)
35 {
36 return bt_packet_borrow_stream((void *) packet);
37 }
38
39 BT_EXPORT
40 struct bt_field *bt_packet_borrow_context_field(struct bt_packet *packet)
41 {
42 BT_ASSERT_PRE_DEV_PACKET_NON_NULL(packet);
43 return packet->context_field ? packet->context_field->field : NULL;
44 }
45
46 BT_EXPORT
47 const struct bt_field *bt_packet_borrow_context_field_const(
48 const struct bt_packet *packet)
49 {
50 return bt_packet_borrow_context_field((void *) packet);
51 }
52
53 void _bt_packet_set_is_frozen(const struct bt_packet *packet, bool is_frozen)
54 {
55 if (!packet) {
56 return;
57 }
58
59 BT_LIB_LOGD("Setting packet's frozen state: %![packet-]+a, "
60 "is-frozen=%d", packet, is_frozen);
61
62 if (packet->context_field) {
63 BT_LOGD_STR("Setting packet's context field's frozen state.");
64 bt_field_set_is_frozen(packet->context_field->field,
65 is_frozen);
66 }
67
68 ((struct bt_packet *) packet)->frozen = is_frozen;
69 }
70
71 static inline
72 void reset_packet(struct bt_packet *packet)
73 {
74 BT_ASSERT(packet);
75 BT_LIB_LOGD("Resetting packet: %!+a", packet);
76 bt_packet_set_is_frozen(packet, false);
77
78 if (packet->context_field) {
79 bt_field_set_is_frozen(packet->context_field->field, false);
80 bt_field_reset(packet->context_field->field);
81 }
82 }
83
84 static
85 void recycle_context_field(struct bt_field_wrapper *context_field,
86 struct bt_stream_class *stream_class)
87 {
88 BT_ASSERT(context_field);
89 BT_LIB_LOGD("Recycling packet context field: "
90 "addr=%p, %![sc-]+S, %![field-]+f", context_field,
91 stream_class, context_field->field);
92 bt_object_pool_recycle_object(&stream_class->packet_context_field_pool,
93 context_field);
94 }
95
96 void bt_packet_recycle(struct bt_packet *packet)
97 {
98 struct bt_stream *stream;
99
100 BT_ASSERT(packet);
101 BT_LIB_LOGD("Recycling packet: %!+a", packet);
102
103 /*
104 * Those are the important ordered steps:
105 *
106 * 1. Reset the packet object (put any permanent reference it
107 * has, unfreeze it and its fields in developer mode, etc.),
108 * but do NOT put its stream's reference. This stream
109 * contains the pool to which we're about to recycle this
110 * packet object, so we must guarantee its existence thanks
111 * to this existing reference.
112 *
113 * 2. Move the stream reference to our `stream`
114 * variable so that we can set the packet's stream member
115 * to NULL before recycling it. We CANNOT do this after
116 * we put the stream reference because this bt_object_put_ref()
117 * could destroy the stream, also destroying its
118 * packet pool, thus also destroying our packet object (this
119 * would result in an invalid write access).
120 *
121 * 3. Recycle the packet object.
122 *
123 * 4. Put our stream reference.
124 */
125 reset_packet(packet);
126 stream = packet->stream;
127 BT_ASSERT(stream);
128 packet->stream = NULL;
129 bt_object_pool_recycle_object(&stream->packet_pool, packet);
130 bt_object_put_ref_no_null_check(&stream->base);
131 }
132
133 void bt_packet_destroy(struct bt_packet *packet)
134 {
135 BT_LIB_LOGD("Destroying packet: %!+a", packet);
136
137 if (packet->context_field) {
138 if (packet->stream) {
139 BT_LOGD_STR("Recycling packet's context field.");
140 recycle_context_field(packet->context_field,
141 packet->stream->class);
142 } else {
143 bt_field_wrapper_destroy(packet->context_field);
144 }
145
146 packet->context_field = NULL;
147 }
148
149 BT_LOGD_STR("Putting packet's stream.");
150 BT_OBJECT_PUT_REF_AND_RESET(packet->stream);
151 g_free(packet);
152 }
153
154 struct bt_packet *bt_packet_new(struct bt_stream *stream)
155 {
156 struct bt_packet *packet = NULL;
157 struct bt_trace_class *trace_class = NULL;
158
159 BT_ASSERT(stream);
160 BT_LIB_LOGD("Creating packet object: %![stream-]+s", stream);
161 packet = g_new0(struct bt_packet, 1);
162 if (!packet) {
163 BT_LIB_LOGE_APPEND_CAUSE(
164 "Failed to allocate one packet object.");
165 goto error;
166 }
167
168 bt_object_init_shared(&packet->base,
169 (bt_object_release_func) bt_packet_recycle);
170 packet->stream = stream;
171 bt_object_get_ref_no_null_check(stream);
172 trace_class = bt_stream_class_borrow_trace_class_inline(stream->class);
173 BT_ASSERT(trace_class);
174
175 if (stream->class->packet_context_fc) {
176 BT_LOGD_STR("Creating initial packet context field.");
177 packet->context_field = bt_field_wrapper_create(
178 &stream->class->packet_context_field_pool,
179 stream->class->packet_context_fc);
180 if (!packet->context_field) {
181 BT_LIB_LOGE_APPEND_CAUSE(
182 "Cannot create packet context field wrapper.");
183 goto error;
184 }
185 }
186
187 BT_LIB_LOGD("Created packet object: %!+a", packet);
188 goto end;
189
190 error:
191 BT_OBJECT_PUT_REF_AND_RESET(packet);
192
193 end:
194 return packet;
195 }
196
197 BT_EXPORT
198 struct bt_packet *bt_packet_create(const struct bt_stream *c_stream)
199 {
200 struct bt_packet *packet = NULL;
201 struct bt_stream *stream = (void *) c_stream;
202
203 BT_ASSERT_PRE_NO_ERROR();
204 BT_ASSERT_PRE_STREAM_NON_NULL(stream);
205 BT_ASSERT_PRE("stream-class-supports-packets",
206 stream->class->supports_packets,
207 "Stream class does not support packets: %![sc-]+S",
208 stream->class);
209 packet = bt_object_pool_create_object(&stream->packet_pool);
210 if (G_UNLIKELY(!packet)) {
211 BT_LIB_LOGE_APPEND_CAUSE(
212 "Cannot allocate one packet from stream's packet pool: "
213 "%![stream-]+s", stream);
214 goto end;
215 }
216
217 if (G_LIKELY(!packet->stream)) {
218 packet->stream = stream;
219 bt_object_get_ref_no_null_check_no_parent_check(
220 &packet->stream->base);
221 }
222
223 end:
224 return (void *) packet;
225 }
226
227 BT_EXPORT
228 void bt_packet_get_ref(const struct bt_packet *packet)
229 {
230 bt_object_get_ref(packet);
231 }
232
233 BT_EXPORT
234 void bt_packet_put_ref(const struct bt_packet *packet)
235 {
236 bt_object_put_ref(packet);
237 }
This page took 0.037297 seconds and 5 git commands to generate.