lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[babeltrace.git] / src / lib / trace-ir / event.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #ifndef BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
9 #define BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
10
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.
14 #endif
15
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"
25 #include <glib.h>
26 #include <stdbool.h>
27
28 #include "event-class.h"
29 #include "field.h"
30 #include "field-wrapper.h"
31 #include "packet.h"
32 #include "stream.h"
33
34 #define BT_ASSERT_PRE_DEV_EVENT_HOT(_event) \
35 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event *) (_event)), \
36 "Event", ": %!+e", (_event))
37
38 struct bt_event {
39 struct bt_object base;
40
41 /* Owned by this */
42 struct bt_event_class *class;
43
44 /* Owned by this (can be `NULL`) */
45 struct bt_packet *packet;
46
47 /* Owned by this */
48 struct bt_stream *stream;
49
50 struct bt_field *common_context_field;
51 struct bt_field *specific_context_field;
52 struct bt_field *payload_field;
53 bool frozen;
54 };
55
56 BT_HIDDEN
57 void bt_event_destroy(struct bt_event *event);
58
59 BT_HIDDEN
60 struct bt_event *bt_event_new(struct bt_event_class *event_class);
61
62 BT_HIDDEN
63 void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen);
64
65 #ifdef BT_DEV_MODE
66 # define bt_event_set_is_frozen _bt_event_set_is_frozen
67 #else
68 # define bt_event_set_is_frozen(_event, _is_frozen)
69 #endif
70
71 __attribute__((unused))
72 static inline
73 void _bt_event_reset_dev_mode(struct bt_event *event)
74 {
75 BT_ASSERT_DBG(event);
76
77 if (event->common_context_field) {
78 bt_field_set_is_frozen(
79 event->common_context_field, false);
80 bt_field_reset(
81 event->common_context_field);
82 }
83
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);
88 }
89
90 if (event->payload_field) {
91 bt_field_set_is_frozen(
92 event->payload_field, false);
93 bt_field_reset(event->payload_field);
94 }
95 }
96
97 #ifdef BT_DEV_MODE
98 # define bt_event_reset_dev_mode _bt_event_reset_dev_mode
99 #else
100 # define bt_event_reset_dev_mode(_x)
101 #endif
102
103 static inline
104 void bt_event_reset(struct bt_event *event)
105 {
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;
111
112 if (event->packet) {
113 bt_object_put_ref_no_null_check(&event->packet->base);
114 event->packet = NULL;
115 }
116 }
117
118 static inline
119 void bt_event_recycle(struct bt_event *event)
120 {
121 struct bt_event_class *event_class;
122
123 BT_ASSERT_DBG(event);
124 BT_LIB_LOGD("Recycling event: %!+e", event);
125
126 /*
127 * Those are the important ordered steps:
128 *
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.
135 *
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).
143 *
144 * 3. Recycle the event object.
145 *
146 * 4. Put our event class reference.
147 */
148 bt_event_reset(event);
149 event_class = event->class;
150 BT_ASSERT_DBG(event_class);
151 event->class = NULL;
152 bt_object_pool_recycle_object(&event_class->event_pool, event);
153 bt_object_put_ref_no_null_check(&event_class->base);
154 }
155
156 static inline
157 void bt_event_set_packet(struct bt_event *event, struct bt_packet *packet)
158 {
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",
171 event, packet);
172 }
173
174 static inline
175 void bt_event_set_stream(struct bt_event *event, struct bt_stream *stream)
176 {
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",
188 event, stream);
189 }
190
191 static inline
192 struct bt_event *bt_event_create(struct bt_event_class *event_class,
193 struct bt_packet *packet, struct bt_stream *stream)
194 {
195 struct bt_event *event = NULL;
196
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);
204 goto end;
205 }
206
207 if (G_LIKELY(!event->class)) {
208 event->class = event_class;
209 bt_object_get_ref_no_null_check(&event_class->base);
210 }
211
212 bt_event_set_stream(event, stream);
213
214 if (packet) {
215 BT_ASSERT_DBG(packet);
216 bt_event_set_packet(event, packet);
217 }
218
219 goto end;
220
221 end:
222 return event;
223 }
224
225 #endif /* BABELTRACE_TRACE_IR_EVENT_INTERNAL_H */
This page took 0.033657 seconds and 4 git commands to generate.