Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / packet.c
1 /*
2 * packet.c
3 *
4 * Babeltrace CTF IR - Stream packet
5 *
6 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define BT_LOG_TAG "PACKET"
28 #include <babeltrace/lib-logging-internal.h>
29
30 #include <babeltrace/ctf-ir/fields-internal.h>
31 #include <babeltrace/ctf-ir/packet.h>
32 #include <babeltrace/ctf-ir/packet-internal.h>
33 #include <babeltrace/ctf-ir/trace.h>
34 #include <babeltrace/ctf-ir/stream-class-internal.h>
35 #include <babeltrace/ctf-ir/stream-class.h>
36 #include <babeltrace/ctf-ir/stream.h>
37 #include <babeltrace/ctf-ir/stream-internal.h>
38 #include <babeltrace/ctf-ir/trace-internal.h>
39 #include <babeltrace/object-internal.h>
40 #include <babeltrace/ref.h>
41 #include <babeltrace/assert-internal.h>
42 #include <babeltrace/assert-pre-internal.h>
43 #include <inttypes.h>
44
45 struct bt_stream *bt_packet_get_stream(struct bt_packet *packet)
46 {
47 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
48 return bt_get(packet->stream);
49 }
50
51 struct bt_field *bt_packet_get_header(
52 struct bt_packet *packet)
53 {
54 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
55 return bt_get(packet->header);
56 }
57
58 BT_ASSERT_PRE_FUNC
59 static inline bool validate_field_to_set(struct bt_field *field,
60 struct bt_field_type *expected_ft)
61 {
62 bool ret = true;
63
64 if (!field) {
65 if (expected_ft) {
66 BT_ASSERT_PRE_MSG("Setting no field, but expected "
67 "field type is not NULL: "
68 "%![field-]+f, %![expected-ft-]+F",
69 field, expected_ft);
70 ret = false;
71 goto end;
72 }
73
74 goto end;
75 }
76
77 if (bt_field_type_compare(field->type, expected_ft) != 0) {
78 BT_ASSERT_PRE_MSG("Field type is different from expected "
79 " field type: %![field-ft-]+F, %![expected-ft-]+F",
80 field->type, expected_ft);
81 ret = false;
82 goto end;
83 }
84
85 end:
86 return ret;
87 }
88
89 int bt_packet_set_header(struct bt_packet *packet,
90 struct bt_field *header)
91 {
92 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
93 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
94 BT_ASSERT_PRE(validate_field_to_set(header,
95 bt_stream_class_borrow_trace(packet->stream->stream_class)->packet_header_type),
96 "Invalid packet header field: "
97 "%![packet-]+a, %![field-]+f", packet, header);
98 bt_put(packet->header);
99 packet->header = bt_get(header);
100 BT_LOGV("Set packet's header field: packet-addr=%p, packet-header-addr=%p",
101 packet, header);
102 return 0;
103 }
104
105 struct bt_field *bt_packet_get_context(struct bt_packet *packet)
106 {
107 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
108 return bt_get(packet->context);
109 }
110
111 int bt_packet_set_context(struct bt_packet *packet,
112 struct bt_field *context)
113 {
114 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
115 BT_ASSERT_PRE_HOT(packet, "Packet", ": +%!+a", packet);
116 BT_ASSERT_PRE(validate_field_to_set(context,
117 packet->stream->stream_class->packet_context_type),
118 "Invalid packet context field: "
119 "%![packet-]+a, %![field-]+f", packet, context);
120 bt_put(packet->context);
121 packet->context = bt_get(context);
122 BT_LOGV("Set packet's context field: packet-addr=%p, packet-context-addr=%p",
123 packet, context);
124 return 0;
125 }
126
127 BT_HIDDEN
128 void _bt_packet_freeze(struct bt_packet *packet)
129 {
130 if (!packet || packet->frozen) {
131 return;
132 }
133
134 BT_LOGD("Freezing packet: addr=%p", packet);
135 BT_LOGD_STR("Freezing packet's header field.");
136 bt_field_freeze_recursive(packet->header);
137 BT_LOGD_STR("Freezing packet's context field.");
138 bt_field_freeze_recursive(packet->context);
139 packet->frozen = 1;
140 }
141
142 static
143 void bt_packet_destroy(struct bt_object *obj)
144 {
145 struct bt_packet *packet;
146
147 packet = container_of(obj, struct bt_packet, base);
148 BT_LOGD("Destroying packet: addr=%p", packet);
149 BT_LOGD_STR("Putting packet's header field.");
150 bt_put(packet->header);
151 BT_LOGD_STR("Putting packet's context field.");
152 bt_put(packet->context);
153 BT_LOGD_STR("Putting packet's stream.");
154 bt_put(packet->stream);
155 g_free(packet);
156 }
157
158 struct bt_packet *bt_packet_create(
159 struct bt_stream *stream)
160 {
161 struct bt_packet *packet = NULL;
162 struct bt_stream_class *stream_class = NULL;
163 struct bt_trace *trace = NULL;
164
165 BT_ASSERT_PRE_NON_NULL(stream, "Stream");
166 BT_LOGD("Creating packet object: stream-addr=%p, "
167 "stream-name=\"%s\", stream-class-addr=%p, "
168 "stream-class-name=\"%s\", stream-class-id=%" PRId64,
169 stream, bt_stream_get_name(stream),
170 stream->stream_class,
171 bt_stream_class_get_name(stream->stream_class),
172 bt_stream_class_get_id(stream->stream_class));
173 BT_ASSERT_PRE(stream->pos.fd < 0,
174 "Stream is a CTF writer stream: %!+s", stream);
175 stream_class = bt_stream_get_class(stream);
176 BT_ASSERT(stream_class);
177 trace = bt_stream_class_get_trace(stream_class);
178 BT_ASSERT(trace);
179 packet = g_new0(struct bt_packet, 1);
180 if (!packet) {
181 BT_LOGE_STR("Failed to allocate one packet object.");
182 goto end;
183 }
184
185 bt_object_init(packet, bt_packet_destroy);
186 packet->stream = bt_get(stream);
187
188 if (trace->packet_header_type) {
189 BT_LOGD("Creating initial packet header field: ft-addr=%p",
190 trace->packet_header_type);
191 packet->header = bt_field_create(trace->packet_header_type);
192 if (!packet->header) {
193 BT_LOGE_STR("Cannot create initial packet header field object.");
194 BT_PUT(packet);
195 goto end;
196 }
197 }
198
199 if (stream->stream_class->packet_context_type) {
200 BT_LOGD("Creating initial packet context field: ft-addr=%p",
201 stream->stream_class->packet_context_type);
202 packet->context = bt_field_create(
203 stream->stream_class->packet_context_type);
204 if (!packet->context) {
205 BT_LOGE_STR("Cannot create initial packet header field object.");
206 BT_PUT(packet);
207 goto end;
208 }
209 }
210
211 BT_LOGD("Created packet object: addr=%p", packet);
212
213 end:
214 BT_PUT(trace);
215 BT_PUT(stream_class);
216
217 return packet;
218 }
This page took 0.034358 seconds and 5 git commands to generate.