Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[babeltrace.git] / include / babeltrace / trace-ir / event-internal.h
1 #ifndef BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
2 #define BABELTRACE_TRACE_IR_EVENT_INTERNAL_H
3
4 /*
5 * Babeltrace - Trace IR: Event internal
6 *
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 /* Protection: this file uses BT_LIB_LOG*() macros directly */
31 #ifndef BABELTRACE_LIB_LOGGING_INTERNAL_H
32 # error Please define include <babeltrace/lib-logging-internal.h> before including this file.
33 #endif
34
35 #include <babeltrace/assert-pre-internal.h>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/values.h>
38 #include <babeltrace/trace-ir/clock-value-internal.h>
39 #include <babeltrace/trace-ir/stream-class.h>
40 #include <babeltrace/trace-ir/stream.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/fields.h>
45 #include <babeltrace/trace-ir/fields-internal.h>
46 #include <babeltrace/trace-ir/event-class-internal.h>
47 #include <babeltrace/trace-ir/field-wrapper-internal.h>
48 #include <babeltrace/object-internal.h>
49 #include <babeltrace/assert-internal.h>
50 #include <glib.h>
51
52 #define BT_ASSERT_PRE_EVENT_HOT(_event) \
53 BT_ASSERT_PRE_HOT((_event), "Event", ": %!+e", (_event))
54
55 struct bt_event {
56 struct bt_object base;
57 struct bt_event_class *class;
58 struct bt_packet *packet;
59 struct bt_field_wrapper *header_field;
60 struct bt_field *common_context_field;
61 struct bt_field *specific_context_field;
62 struct bt_field *payload_field;
63 struct bt_clock_value *default_cv;
64 bool frozen;
65 };
66
67 BT_HIDDEN
68 void bt_event_destroy(struct bt_event *event);
69
70 BT_HIDDEN
71 struct bt_event *bt_event_new(struct bt_event_class *event_class);
72
73 BT_HIDDEN
74 void _bt_event_set_is_frozen(struct bt_event *event, bool is_frozen);
75
76 #ifdef BT_DEV_MODE
77 # define bt_event_set_is_frozen _bt_event_set_is_frozen
78 #else
79 # define bt_event_set_is_frozen(_event, _is_frozen)
80 #endif
81
82 BT_UNUSED
83 static inline
84 void _bt_event_reset_dev_mode(struct bt_event *event)
85 {
86 BT_ASSERT(event);
87
88 if (event->header_field) {
89 bt_field_set_is_frozen(
90 event->header_field->field, false);
91 bt_field_reset(
92 event->header_field->field);
93 }
94
95 if (event->common_context_field) {
96 bt_field_set_is_frozen(
97 event->common_context_field, false);
98 bt_field_reset(
99 event->common_context_field);
100 }
101
102 if (event->specific_context_field) {
103 bt_field_set_is_frozen(
104 event->specific_context_field, false);
105 bt_field_reset(event->specific_context_field);
106 }
107
108 if (event->payload_field) {
109 bt_field_set_is_frozen(
110 event->payload_field, false);
111 bt_field_reset(event->payload_field);
112 }
113 }
114
115 #ifdef BT_DEV_MODE
116 # define bt_event_reset_dev_mode _bt_event_reset_dev_mode
117 #else
118 # define bt_event_reset_dev_mode(_x)
119 #endif
120
121 static inline
122 void bt_event_reset(struct bt_event *event)
123 {
124 BT_ASSERT(event);
125 BT_LIB_LOGD("Resetting event: %!+e", event);
126 bt_event_set_is_frozen(event, false);
127
128 if (event->default_cv) {
129 bt_clock_value_reset(event->default_cv);
130 }
131
132 bt_object_put_no_null_check(&event->packet->base);
133 event->packet = NULL;
134 }
135
136 static inline
137 void bt_event_recycle(struct bt_event *event)
138 {
139 struct bt_event_class *event_class;
140
141 BT_ASSERT(event);
142 BT_LIB_LOGD("Recycling event: %!+e", event);
143
144 /*
145 * Those are the important ordered steps:
146 *
147 * 1. Reset the event object (put any permanent reference it
148 * has, unfreeze it and its fields in developer mode, etc.),
149 * but do NOT put its class's reference. This event class
150 * contains the pool to which we're about to recycle this
151 * event object, so we must guarantee its existence thanks
152 * to this existing reference.
153 *
154 * 2. Move the event class reference to our `event_class`
155 * variable so that we can set the event's class member
156 * to NULL before recycling it. We CANNOT do this after
157 * we put the event class reference because this bt_object_put_ref()
158 * could destroy the event class, also destroying its
159 * event pool, thus also destroying our event object (this
160 * would result in an invalid write access).
161 *
162 * 3. Recycle the event object.
163 *
164 * 4. Put our event class reference.
165 */
166 bt_event_reset(event);
167 event_class = event->class;
168 BT_ASSERT(event_class);
169 event->class = NULL;
170 bt_object_pool_recycle_object(&event_class->event_pool, event);
171 bt_object_put_no_null_check(&event_class->base);
172 }
173
174 static inline
175 void bt_event_set_packet(struct bt_event *event, struct bt_packet *packet)
176 {
177 BT_ASSERT_PRE_NON_NULL(event, "Event");
178 BT_ASSERT_PRE_NON_NULL(packet, "Packet");
179 BT_ASSERT_PRE_EVENT_HOT(event);
180 BT_ASSERT_PRE(bt_event_class_borrow_stream_class(
181 event->class) == packet->stream->class,
182 "Packet's stream class and event's stream class differ: "
183 "%![event-]+e, %![packet-]+a", event, packet);
184
185 BT_ASSERT(!event->packet);
186 event->packet = packet;
187 bt_object_get_no_null_check_no_parent_check(&event->packet->base);
188 BT_LIB_LOGV("Set event's packet: %![event-]+e, %![packet-]+a",
189 event, packet);
190 }
191
192 static inline
193 struct bt_event *bt_event_create(struct bt_event_class *event_class,
194 struct bt_packet *packet)
195 {
196 struct bt_event *event = NULL;
197
198 BT_ASSERT(event_class);
199 event = bt_object_pool_create_object(&event_class->event_pool);
200 if (unlikely(!event)) {
201 BT_LIB_LOGE("Cannot allocate one event from event class's event pool: "
202 "%![ec-]+E", event_class);
203 goto end;
204 }
205
206 if (likely(!event->class)) {
207 event->class = event_class;
208 bt_object_get_no_null_check(&event_class->base);
209 }
210
211 BT_ASSERT(packet);
212 bt_event_set_packet(event, packet);
213 goto end;
214
215 end:
216 return event;
217 }
218
219 #endif /* BABELTRACE_TRACE_IR_EVENT_INTERNAL_H */
This page took 0.033906 seconds and 4 git commands to generate.