Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / lib / trace-ir / event.h
CommitLineData
56e18c4c
PP
1#ifndef BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
2#define BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
273b65be
JG
3
4/*
e2f7325d 5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be 7 *
273b65be
JG
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
44c440bc 27/* Protection: this file uses BT_LIB_LOG*() macros directly */
7151fb67 28#ifndef BT_LIB_LOG_SUPPORTED
c2d9d9cf 29# error Please include "lib/logging.h" before including this file.
44c440bc
PP
30#endif
31
578e048b 32#include "lib/assert-pre.h"
91d81473 33#include "common/macros.h"
3fadfbc0
MJ
34#include <babeltrace2/value.h>
35#include <babeltrace2/trace-ir/stream-class.h>
36#include <babeltrace2/trace-ir/stream.h>
3fadfbc0 37#include <babeltrace2/trace-ir/packet.h>
3fadfbc0 38#include <babeltrace2/trace-ir/field.h>
578e048b
MJ
39#include "lib/object.h"
40#include "common/assert.h"
273b65be 41#include <glib.h>
c4f23e30 42#include <stdbool.h>
273b65be 43
578e048b
MJ
44#include "event-class.h"
45#include "field.h"
46#include "field-wrapper.h"
47#include "packet.h"
48#include "stream.h"
49
bdb288b3
PP
50#define BT_ASSERT_PRE_DEV_EVENT_HOT(_event) \
51 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event *) (_event)), \
40f4ba76 52 "Event", ": %!+e", (_event))
dc3fffef 53
cb6f1f7d 54struct bt_event {
83509119 55 struct bt_object base;
862ca4ed
PP
56
57 /* Owned by this */
cb6f1f7d 58 struct bt_event_class *class;
862ca4ed 59
26fc5aed 60 /* Owned by this (can be `NULL`) */
44c440bc 61 struct bt_packet *packet;
862ca4ed 62
26fc5aed
PP
63 /* Owned by this */
64 struct bt_stream *stream;
65
44c440bc
PP
66 struct bt_field *common_context_field;
67 struct bt_field *specific_context_field;
cb6f1f7d 68 struct bt_field *payload_field;
44c440bc 69 bool frozen;
273b65be
JG
70};
71
273b65be 72BT_HIDDEN
cb6f1f7d 73void bt_event_destroy(struct bt_event *event);
273b65be 74
4ce9f9d0 75BT_HIDDEN
cb6f1f7d 76struct bt_event *bt_event_new(struct bt_event_class *event_class);
4ce9f9d0 77
f6ccaed9 78BT_HIDDEN
40f4ba76 79void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen);
f6ccaed9
PP
80
81#ifdef BT_DEV_MODE
6c677fb5 82# define bt_event_set_is_frozen _bt_event_set_is_frozen
f6ccaed9 83#else
6c677fb5 84# define bt_event_set_is_frozen(_event, _is_frozen)
f6ccaed9
PP
85#endif
86
91d81473 87__attribute__((unused))
3dca2276 88static inline
cb6f1f7d 89void _bt_event_reset_dev_mode(struct bt_event *event)
3dca2276 90{
98b15851 91 BT_ASSERT_DBG(event);
312c056a 92
44c440bc
PP
93 if (event->common_context_field) {
94 bt_field_set_is_frozen(
95 event->common_context_field, false);
96 bt_field_reset(
97 event->common_context_field);
312c056a
PP
98 }
99
44c440bc
PP
100 if (event->specific_context_field) {
101 bt_field_set_is_frozen(
102 event->specific_context_field, false);
103 bt_field_reset(event->specific_context_field);
312c056a
PP
104 }
105
106 if (event->payload_field) {
44c440bc 107 bt_field_set_is_frozen(
cb6f1f7d 108 event->payload_field, false);
44c440bc 109 bt_field_reset(event->payload_field);
6c677fb5 110 }
6c677fb5
PP
111}
112
113#ifdef BT_DEV_MODE
114# define bt_event_reset_dev_mode _bt_event_reset_dev_mode
115#else
116# define bt_event_reset_dev_mode(_x)
117#endif
312c056a 118
6c677fb5
PP
119static inline
120void bt_event_reset(struct bt_event *event)
121{
98b15851 122 BT_ASSERT_DBG(event);
44c440bc 123 BT_LIB_LOGD("Resetting event: %!+e", event);
6c677fb5 124 bt_event_set_is_frozen(event, false);
6871026b 125 bt_object_put_ref_no_null_check(&event->stream->base);
26fc5aed
PP
126 event->stream = NULL;
127
128 if (event->packet) {
6871026b 129 bt_object_put_ref_no_null_check(&event->packet->base);
26fc5aed
PP
130 event->packet = NULL;
131 }
6c677fb5
PP
132}
133
134static inline
135void bt_event_recycle(struct bt_event *event)
136{
137 struct bt_event_class *event_class;
138
98b15851 139 BT_ASSERT_DBG(event);
6c677fb5
PP
140 BT_LIB_LOGD("Recycling event: %!+e", event);
141
142 /*
143 * Those are the important ordered steps:
144 *
145 * 1. Reset the event object (put any permanent reference it
146 * has, unfreeze it and its fields in developer mode, etc.),
147 * but do NOT put its class's reference. This event class
148 * contains the pool to which we're about to recycle this
149 * event object, so we must guarantee its existence thanks
150 * to this existing reference.
151 *
152 * 2. Move the event class reference to our `event_class`
153 * variable so that we can set the event's class member
154 * to NULL before recycling it. We CANNOT do this after
65300d60 155 * we put the event class reference because this bt_object_put_ref()
6c677fb5
PP
156 * could destroy the event class, also destroying its
157 * event pool, thus also destroying our event object (this
158 * would result in an invalid write access).
159 *
160 * 3. Recycle the event object.
161 *
162 * 4. Put our event class reference.
163 */
164 bt_event_reset(event);
cb6f1f7d 165 event_class = event->class;
98b15851 166 BT_ASSERT_DBG(event_class);
cb6f1f7d 167 event->class = NULL;
6c677fb5 168 bt_object_pool_recycle_object(&event_class->event_pool, event);
6871026b 169 bt_object_put_ref_no_null_check(&event_class->base);
6c677fb5
PP
170}
171
172static inline
173void bt_event_set_packet(struct bt_event *event, struct bt_packet *packet)
174{
bdb288b3
PP
175 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
176 BT_ASSERT_PRE_DEV_NON_NULL(packet, "Packet");
177 BT_ASSERT_PRE_DEV_EVENT_HOT(event);
178 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
44c440bc 179 event->class) == packet->stream->class,
26e21a82 180 "Packet's stream class and event's stream class differ: "
44c440bc 181 "%![event-]+e, %![packet-]+a", event, packet);
98b15851
PP
182 BT_ASSERT_DBG(event->stream->class->supports_packets);
183 BT_ASSERT_DBG(!event->packet);
6c677fb5 184 event->packet = packet;
6871026b 185 bt_object_get_ref_no_null_check_no_parent_check(&event->packet->base);
3f7d4d90 186 BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a",
44c440bc 187 event, packet);
6c677fb5
PP
188}
189
26fc5aed
PP
190static inline
191void bt_event_set_stream(struct bt_event *event, struct bt_stream *stream)
192{
bdb288b3
PP
193 BT_ASSERT_PRE_DEV_NON_NULL(event, "Event");
194 BT_ASSERT_PRE_DEV_NON_NULL(stream, "Stream");
195 BT_ASSERT_PRE_DEV_EVENT_HOT(event);
196 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
26fc5aed
PP
197 event->class) == stream->class,
198 "Stream's class and event's stream class differ: "
199 "%![event-]+e, %![stream-]+s", event, stream);
98b15851 200 BT_ASSERT_DBG(!event->stream);
26fc5aed 201 event->stream = stream;
6871026b 202 bt_object_get_ref_no_null_check_no_parent_check(&event->stream->base);
26fc5aed
PP
203 BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s",
204 event, stream);
205}
206
6c677fb5
PP
207static inline
208struct bt_event *bt_event_create(struct bt_event_class *event_class,
26fc5aed 209 struct bt_packet *packet, struct bt_stream *stream)
6c677fb5
PP
210{
211 struct bt_event *event = NULL;
212
98b15851
PP
213 BT_ASSERT_DBG(event_class);
214 BT_ASSERT_DBG(stream);
6c677fb5 215 event = bt_object_pool_create_object(&event_class->event_pool);
91d81473 216 if (G_UNLIKELY(!event)) {
870631a2
PP
217 BT_LIB_LOGE_APPEND_CAUSE(
218 "Cannot allocate one event from event class's event pool: "
44c440bc 219 "%![ec-]+E", event_class);
6c677fb5
PP
220 goto end;
221 }
222
91d81473 223 if (G_LIKELY(!event->class)) {
cb6f1f7d 224 event->class = event_class;
6871026b 225 bt_object_get_ref_no_null_check(&event_class->base);
6c677fb5
PP
226 }
227
26fc5aed
PP
228 bt_event_set_stream(event, stream);
229
230 if (packet) {
98b15851 231 BT_ASSERT_DBG(packet);
26fc5aed
PP
232 bt_event_set_packet(event, packet);
233 }
234
6c677fb5
PP
235 goto end;
236
237end:
238 return event;
239}
240
56e18c4c 241#endif /* BABELTRACE_TRACE_IR_EVENT_INTERNAL_H */
This page took 0.08819 seconds and 4 git commands to generate.