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