2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #ifndef BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
9 #define BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
11 /* Protection: this file uses BT_LIB_LOG*() macros directly */
12 #ifndef BT_LIB_LOG_SUPPORTED
13 # error Please include "lib/logging.h" before including this file.
16 #include "lib/assert-cond.h"
17 #include "common/macros.h"
18 #include <babeltrace2/value.h>
19 #include <babeltrace2/trace-ir/stream-class.h>
20 #include <babeltrace2/trace-ir/stream.h>
21 #include <babeltrace2/trace-ir/packet.h>
22 #include <babeltrace2/trace-ir/field.h>
23 #include "lib/object.h"
24 #include "common/assert.h"
28 #include "event-class.h"
30 #include "field-wrapper.h"
34 #define BT_ASSERT_PRE_DEV_EVENT_HOT(_event) \
35 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event *) (_event)), \
36 "Event", ": %!+e", (_event))
39 struct bt_object base
;
42 struct bt_event_class
*class;
44 /* Owned by this (can be `NULL`) */
45 struct bt_packet
*packet
;
48 struct bt_stream
*stream
;
50 struct bt_field
*common_context_field
;
51 struct bt_field
*specific_context_field
;
52 struct bt_field
*payload_field
;
57 void bt_event_destroy(struct bt_event
*event
);
60 struct bt_event
*bt_event_new(struct bt_event_class
*event_class
);
63 void _bt_event_set_is_frozen(const struct bt_event
*event
, bool is_frozen
);
66 # define bt_event_set_is_frozen _bt_event_set_is_frozen
68 # define bt_event_set_is_frozen(_event, _is_frozen)
71 __attribute__((unused
))
73 void _bt_event_reset_dev_mode(struct bt_event
*event
)
77 if (event
->common_context_field
) {
78 bt_field_set_is_frozen(
79 event
->common_context_field
, false);
81 event
->common_context_field
);
84 if (event
->specific_context_field
) {
85 bt_field_set_is_frozen(
86 event
->specific_context_field
, false);
87 bt_field_reset(event
->specific_context_field
);
90 if (event
->payload_field
) {
91 bt_field_set_is_frozen(
92 event
->payload_field
, false);
93 bt_field_reset(event
->payload_field
);
98 # define bt_event_reset_dev_mode _bt_event_reset_dev_mode
100 # define bt_event_reset_dev_mode(_x)
104 void bt_event_reset(struct bt_event
*event
)
106 BT_ASSERT_DBG(event
);
107 BT_LIB_LOGD("Resetting event: %!+e", event
);
108 bt_event_set_is_frozen(event
, false);
109 bt_object_put_ref_no_null_check(&event
->stream
->base
);
110 event
->stream
= NULL
;
113 bt_object_put_ref_no_null_check(&event
->packet
->base
);
114 event
->packet
= NULL
;
119 void bt_event_recycle(struct bt_event
*event
)
121 struct bt_event_class
*event_class
;
123 BT_ASSERT_DBG(event
);
124 BT_LIB_LOGD("Recycling event: %!+e", event
);
127 * Those are the important ordered steps:
129 * 1. Reset the event object (put any permanent reference it
130 * has, unfreeze it and its fields in developer mode, etc.),
131 * but do NOT put its class's reference. This event class
132 * contains the pool to which we're about to recycle this
133 * event object, so we must guarantee its existence thanks
134 * to this existing reference.
136 * 2. Move the event class reference to our `event_class`
137 * variable so that we can set the event's class member
138 * to NULL before recycling it. We CANNOT do this after
139 * we put the event class reference because this bt_object_put_ref()
140 * could destroy the event class, also destroying its
141 * event pool, thus also destroying our event object (this
142 * would result in an invalid write access).
144 * 3. Recycle the event object.
146 * 4. Put our event class reference.
148 bt_event_reset(event
);
149 event_class
= event
->class;
150 BT_ASSERT_DBG(event_class
);
152 bt_object_pool_recycle_object(&event_class
->event_pool
, event
);
153 bt_object_put_ref_no_null_check(&event_class
->base
);
157 void bt_event_set_packet(struct bt_event
*event
, struct bt_packet
*packet
)
159 BT_ASSERT_PRE_DEV_NON_NULL(event
, "Event");
160 BT_ASSERT_PRE_DEV_NON_NULL(packet
, "Packet");
161 BT_ASSERT_PRE_DEV_EVENT_HOT(event
);
162 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
163 event
->class) == packet
->stream
->class,
164 "Packet's stream class and event's stream class differ: "
165 "%![event-]+e, %![packet-]+a", event
, packet
);
166 BT_ASSERT_DBG(event
->stream
->class->supports_packets
);
167 BT_ASSERT_DBG(!event
->packet
);
168 event
->packet
= packet
;
169 bt_object_get_ref_no_null_check_no_parent_check(&event
->packet
->base
);
170 BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a",
175 void bt_event_set_stream(struct bt_event
*event
, struct bt_stream
*stream
)
177 BT_ASSERT_PRE_DEV_NON_NULL(event
, "Event");
178 BT_ASSERT_PRE_DEV_NON_NULL(stream
, "Stream");
179 BT_ASSERT_PRE_DEV_EVENT_HOT(event
);
180 BT_ASSERT_PRE_DEV(bt_event_class_borrow_stream_class(
181 event
->class) == stream
->class,
182 "Stream's class and event's stream class differ: "
183 "%![event-]+e, %![stream-]+s", event
, stream
);
184 BT_ASSERT_DBG(!event
->stream
);
185 event
->stream
= stream
;
186 bt_object_get_ref_no_null_check_no_parent_check(&event
->stream
->base
);
187 BT_LIB_LOGD("Set event's stream: %![event-]+e, %![stream-]+s",
192 struct bt_event
*bt_event_create(struct bt_event_class
*event_class
,
193 struct bt_packet
*packet
, struct bt_stream
*stream
)
195 struct bt_event
*event
= NULL
;
197 BT_ASSERT_DBG(event_class
);
198 BT_ASSERT_DBG(stream
);
199 event
= bt_object_pool_create_object(&event_class
->event_pool
);
200 if (G_UNLIKELY(!event
)) {
201 BT_LIB_LOGE_APPEND_CAUSE(
202 "Cannot allocate one event from event class's event pool: "
203 "%![ec-]+E", event_class
);
207 if (G_LIKELY(!event
->class)) {
208 event
->class = event_class
;
209 bt_object_get_ref_no_null_check(&event_class
->base
);
212 bt_event_set_stream(event
, stream
);
215 BT_ASSERT_DBG(packet
);
216 bt_event_set_packet(event
, packet
);
225 #endif /* BABELTRACE_TRACE_IR_EVENT_INTERNAL_H */
This page took 0.033657 seconds and 4 git commands to generate.