lib: make values API const-correct
[babeltrace.git] / lib / trace-ir / event.c
CommitLineData
273b65be 1/*
de9dd397 2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
7b010242
PP
25#define BT_LOG_TAG "EVENT"
26#include <babeltrace/lib-logging-internal.h>
27
8deee039 28#include <babeltrace/assert-pre-internal.h>
108b91d0 29#include <babeltrace/trace-ir/fields-internal.h>
939190b3 30#include <babeltrace/trace-ir/field-classes-internal.h>
108b91d0
PP
31#include <babeltrace/trace-ir/clock-class.h>
32#include <babeltrace/trace-ir/clock-value.h>
33#include <babeltrace/trace-ir/clock-value-internal.h>
34#include <babeltrace/trace-ir/clock-class-internal.h>
9e550e5f 35#include <babeltrace/trace-ir/private-event.h>
108b91d0
PP
36#include <babeltrace/trace-ir/event-internal.h>
37#include <babeltrace/trace-ir/event-class.h>
38#include <babeltrace/trace-ir/event-class-internal.h>
39#include <babeltrace/trace-ir/stream-class.h>
40#include <babeltrace/trace-ir/stream-class-internal.h>
41#include <babeltrace/trace-ir/stream-internal.h>
42#include <babeltrace/trace-ir/packet.h>
43#include <babeltrace/trace-ir/packet-internal.h>
44#include <babeltrace/trace-ir/trace.h>
45#include <babeltrace/trace-ir/trace-internal.h>
46#include <babeltrace/trace-ir/packet-internal.h>
8138bfe1 47#include <babeltrace/object.h>
108b91d0 48#include <babeltrace/trace-ir/attributes-internal.h>
3d9990ac 49#include <babeltrace/compiler-internal.h>
8b45963b 50#include <babeltrace/assert-internal.h>
7b010242 51#include <inttypes.h>
273b65be 52
8deee039 53BT_HIDDEN
7b33a0e0 54void _bt_event_set_is_frozen(struct bt_event *event, bool is_frozen)
8deee039 55{
8deee039 56 BT_ASSERT(event);
7b33a0e0
PP
57 BT_LIB_LOGD("Setting event's frozen state: %!+e, is-frozen=%d",
58 event, is_frozen);
a6918753
PP
59
60 if (event->header_field) {
7b33a0e0
PP
61 BT_LOGD_STR("Setting event's header field's frozen state.");
62 bt_field_set_is_frozen(
c5a24b0a 63 event->header_field->field, is_frozen);
a6918753
PP
64 }
65
7b33a0e0
PP
66 if (event->common_context_field) {
67 BT_LOGD_STR("Setting event's common context field's frozen state.");
68 bt_field_set_is_frozen(
69 event->common_context_field, is_frozen);
a6918753
PP
70 }
71
7b33a0e0
PP
72 if (event->specific_context_field) {
73 BT_LOGD_STR("Setting event's specific context field's frozen state.");
74 bt_field_set_is_frozen(event->specific_context_field,
c5a24b0a 75 is_frozen);
a6918753
PP
76 }
77
78 if (event->payload_field) {
7b33a0e0
PP
79 BT_LOGD_STR("Setting event's payload field's frozen state.");
80 bt_field_set_is_frozen(event->payload_field,
c5a24b0a 81 is_frozen);
a6918753
PP
82 }
83
c5a24b0a 84 event->frozen = is_frozen;
7b33a0e0 85 BT_LOGD_STR("Setting event's packet's frozen state.");
18acc6f8 86 bt_packet_set_is_frozen(event->packet, is_frozen);
8deee039
PP
87}
88
a6918753 89static
7b33a0e0 90void recycle_event_header_field(struct bt_field_wrapper *field_wrapper,
a6918753
PP
91 struct bt_stream_class *stream_class)
92{
93 BT_ASSERT(field_wrapper);
94 BT_LIB_LOGD("Recycling event header field: "
95 "addr=%p, %![sc-]+S, %![field-]+f", field_wrapper,
96 stream_class, field_wrapper->field);
97 bt_object_pool_recycle_object(
98 &stream_class->event_header_field_pool,
99 field_wrapper);
100}
101
7b33a0e0 102static inline
a6918753 103struct bt_field_wrapper *create_event_header_field(
7b33a0e0 104 struct bt_stream_class *stream_class)
a6918753
PP
105{
106 struct bt_field_wrapper *field_wrapper = NULL;
107
108 field_wrapper = bt_field_wrapper_create(
7b33a0e0 109 &stream_class->event_header_field_pool,
939190b3 110 bt_stream_class_borrow_event_header_field_class(stream_class));
a6918753
PP
111 if (!field_wrapper) {
112 goto error;
113 }
114
115 goto end;
116
117error:
118 if (field_wrapper) {
7b33a0e0 119 recycle_event_header_field(field_wrapper, stream_class);
a6918753
PP
120 field_wrapper = NULL;
121 }
122
123end:
124 return field_wrapper;
125}
126
127BT_HIDDEN
128struct bt_event *bt_event_new(struct bt_event_class *event_class)
8deee039 129{
8deee039 130 struct bt_event *event = NULL;
a6918753 131 struct bt_stream_class *stream_class;
939190b3 132 struct bt_field_class *fc;
8deee039 133
7b33a0e0 134 BT_ASSERT(event_class);
8deee039
PP
135 event = g_new0(struct bt_event, 1);
136 if (!event) {
137 BT_LOGE_STR("Failed to allocate one event.");
138 goto error;
139 }
140
7b33a0e0 141 bt_object_init_unique(&event->base);
a6918753
PP
142 stream_class = bt_event_class_borrow_stream_class(event_class);
143 BT_ASSERT(stream_class);
7b33a0e0 144
939190b3 145 if (bt_stream_class_borrow_event_header_field_class(stream_class)) {
7b33a0e0
PP
146 event->header_field = create_event_header_field(stream_class);
147 if (!event->header_field) {
148 BT_LOGE_STR("Cannot create event header field.");
149 goto error;
150 }
151 }
152
939190b3 153 fc = bt_stream_class_borrow_event_common_context_field_class(
7b33a0e0 154 stream_class);
939190b3
PP
155 if (fc) {
156 event->common_context_field = bt_field_create(fc);
7b33a0e0
PP
157 if (!event->common_context_field) {
158 /* bt_field_create() logs errors */
159 goto error;
160 }
161 }
162
939190b3
PP
163 fc = bt_event_class_borrow_specific_context_field_class(event_class);
164 if (fc) {
165 event->specific_context_field = bt_field_create(fc);
7b33a0e0
PP
166 if (!event->specific_context_field) {
167 /* bt_field_create() logs errors */
168 goto error;
169 }
170 }
171
939190b3
PP
172 fc = bt_event_class_borrow_payload_field_class(event_class);
173 if (fc) {
174 event->payload_field = bt_field_create(fc);
7b33a0e0
PP
175 if (!event->payload_field) {
176 /* bt_field_create() logs errors */
177 goto error;
178 }
179 }
180
181 if (stream_class->default_clock_class) {
182 event->default_cv = bt_clock_value_create(
183 stream_class->default_clock_class);
184 if (!event->default_cv) {
185 /* bt_clock_value_create() logs errors */
186 goto error;
187 }
a6918753
PP
188 }
189
8deee039
PP
190 goto end;
191
192error:
a6918753
PP
193 if (event) {
194 bt_event_destroy(event);
195 event = NULL;
196 }
197
198end:
199 return event;
200}
201
5fe68922 202struct bt_event_class *bt_event_borrow_class(struct bt_event *event)
2f100782 203{
8b45963b 204 BT_ASSERT_PRE_NON_NULL(event, "Event");
18acc6f8 205 return event->class;
2f100782
JG
206}
207
8b45963b 208struct bt_stream *bt_event_borrow_stream(struct bt_event *event)
8b45963b
PP
209{
210 BT_ASSERT_PRE_NON_NULL(event, "Event");
5fe68922 211 return event->packet ? event->packet->stream : NULL;
8b45963b
PP
212}
213
7b33a0e0 214struct bt_field *bt_event_borrow_header_field(struct bt_event *event)
e5e6eb3a 215{
18acc6f8 216 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0 217 return event->header_field ? event->header_field->field : NULL;
e5e6eb3a
JG
218}
219
96854e6a 220struct bt_private_field *bt_private_event_borrow_header_field(
9e550e5f
PP
221 struct bt_private_event *event)
222{
223 return (void *) bt_event_borrow_header_field((void *) event);
224}
225
7b33a0e0 226struct bt_field *bt_event_borrow_common_context_field(struct bt_event *event)
662e778c 227{
18acc6f8 228 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0 229 return event->common_context_field;
662e778c
JG
230}
231
96854e6a 232struct bt_private_field *bt_private_event_borrow_common_context_field(
9e550e5f
PP
233 struct bt_private_event *event)
234{
235 return (void *) bt_event_borrow_common_context_field((void *) event);
236}
237
7b33a0e0 238struct bt_field *bt_event_borrow_specific_context_field(struct bt_event *event)
f655a84d 239{
18acc6f8 240 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0 241 return event->specific_context_field;
f655a84d
JG
242}
243
96854e6a 244struct bt_private_field *bt_private_event_borrow_specific_context_field(
9e550e5f
PP
245 struct bt_private_event *event)
246{
247 return (void *) bt_event_borrow_specific_context_field((void *) event);
248}
249
7b33a0e0 250struct bt_field *bt_event_borrow_payload_field(struct bt_event *event)
5fd2e9fd 251{
18acc6f8 252 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0 253 return event->payload_field;
5fd2e9fd
PP
254}
255
96854e6a 256struct bt_private_field *bt_private_event_borrow_payload_field(
9e550e5f
PP
257 struct bt_private_event *event)
258{
259 return (void *) bt_event_borrow_payload_field((void *) event);
260}
261
a6918753
PP
262static
263void release_event_header_field(struct bt_field_wrapper *field_wrapper,
18acc6f8 264 struct bt_event *event)
273b65be 265{
7b33a0e0 266 if (!event->class) {
a6918753
PP
267 bt_field_wrapper_destroy(field_wrapper);
268 } else {
269 struct bt_stream_class *stream_class =
7b33a0e0 270 bt_event_class_borrow_stream_class(event->class);
a6918753
PP
271
272 BT_ASSERT(stream_class);
7b33a0e0 273 recycle_event_header_field(field_wrapper, stream_class);
a6918753 274 }
273b65be
JG
275}
276
7b33a0e0
PP
277BT_HIDDEN
278void bt_event_destroy(struct bt_event *event)
18acc6f8 279{
7b33a0e0
PP
280 BT_ASSERT(event);
281 BT_LIB_LOGD("Destroying event: %!+e", event);
18acc6f8
PP
282
283 if (event->header_field) {
284 BT_LOGD_STR("Releasing event's header field.");
7b33a0e0 285 release_event_header_field(event->header_field, event);
1248f5ea 286 event->header_field = NULL;
18acc6f8
PP
287 }
288
7b33a0e0
PP
289 if (event->common_context_field) {
290 BT_LOGD_STR("Destroying event's stream event context field.");
291 bt_field_destroy(event->common_context_field);
1248f5ea 292 event->common_context_field = NULL;
18acc6f8
PP
293 }
294
7b33a0e0
PP
295 if (event->specific_context_field) {
296 BT_LOGD_STR("Destroying event's context field.");
297 bt_field_destroy(event->specific_context_field);
1248f5ea 298 event->specific_context_field = NULL;
18acc6f8
PP
299 }
300
301 if (event->payload_field) {
7b33a0e0
PP
302 BT_LOGD_STR("Destroying event's payload field.");
303 bt_field_destroy(event->payload_field);
1248f5ea 304 event->payload_field = NULL;
18acc6f8
PP
305 }
306
7b33a0e0 307 BT_LOGD_STR("Putting event's class.");
8138bfe1 308 bt_object_put_ref(event->class);
7b33a0e0
PP
309
310 if (event->default_cv) {
311 bt_clock_value_recycle(event->default_cv);
1248f5ea 312 event->default_cv = NULL;
18acc6f8 313 }
18acc6f8 314
03039f21 315 BT_LOGD_STR("Putting event's packet.");
1248f5ea 316 BT_OBJECT_PUT_REF_AND_RESET(event->packet);
273b65be
JG
317 g_free(event);
318}
319
c8bbf821 320void bt_private_event_set_default_clock_value(
9e550e5f 321 struct bt_private_event *priv_event, uint64_t value_cycles)
8fc063a2 322{
9e550e5f 323 struct bt_event *event = (void *) priv_event;
7b33a0e0
PP
324 struct bt_stream_class *sc;
325
8fc063a2 326 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0
PP
327 BT_ASSERT_PRE_EVENT_HOT(event);
328 sc = bt_event_class_borrow_stream_class_inline(event->class);
329 BT_ASSERT(sc);
330 BT_ASSERT_PRE(sc->default_clock_class,
331 "Event's stream class has no default clock class: "
332 "%![ev-]+e, %![sc-]+S", event, sc);
333 BT_ASSERT(event->default_cv);
334 bt_clock_value_set_value_inline(event->default_cv, value_cycles);
335 BT_LIB_LOGV("Set event's default clock value: %![event-]+e, "
336 "value=%" PRIu64, event, value_cycles);
8fc063a2
PP
337}
338
7b33a0e0
PP
339enum bt_clock_value_status bt_event_borrow_default_clock_value(
340 struct bt_event *event, struct bt_clock_value **clock_value)
78586d8a 341{
8b45963b 342 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0
PP
343 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value (output)");
344 *clock_value = event->default_cv;
345 return BT_CLOCK_VALUE_STATUS_KNOWN;
1556a1af
JG
346}
347
5fe68922 348struct bt_packet *bt_event_borrow_packet(struct bt_event *event)
5c0f40f4 349{
8b45963b 350 BT_ASSERT_PRE_NON_NULL(event, "Event");
7b33a0e0 351 return event->packet;
5c0f40f4
JG
352}
353
9e550e5f
PP
354struct bt_private_packet *bt_private_event_borrow_packet(
355 struct bt_private_event *event)
356{
357 return (void *) bt_event_borrow_packet((void *) event);
358}
359
96854e6a 360int bt_private_event_move_header_field(
9e550e5f
PP
361 struct bt_private_event *priv_event,
362 struct bt_private_event_header_field *priv_header_field)
a6918753
PP
363{
364 struct bt_stream_class *stream_class;
9e550e5f
PP
365 struct bt_event *event = (void *) priv_event;
366 struct bt_event_class *event_class = (void *) event_class;
367 struct bt_field_wrapper *field_wrapper = (void *) priv_header_field;
a6918753
PP
368
369 BT_ASSERT_PRE_NON_NULL(event, "Event");
370 BT_ASSERT_PRE_NON_NULL(field_wrapper, "Header field");
7b33a0e0
PP
371 BT_ASSERT_PRE_EVENT_HOT(event);
372 stream_class = bt_event_class_borrow_stream_class_inline(event->class);
939190b3
PP
373 BT_ASSERT_PRE(stream_class->event_header_fc,
374 "Stream class has no event header field classe: %!+S",
a6918753
PP
375 stream_class);
376
377 /* Recycle current header field: always exists */
18acc6f8 378 BT_ASSERT(event->header_field);
7b33a0e0 379 recycle_event_header_field(event->header_field, stream_class);
a6918753
PP
380
381 /* Move new field */
7b33a0e0 382 event->header_field = field_wrapper;
a6918753
PP
383 return 0;
384}
This page took 0.087587 seconds and 4 git commands to generate.