Commit | Line | Data |
---|---|---|
adc315b8 JG |
1 | #ifndef BABELTRACE_CTF_IR_EVENT_INTERNAL_H |
2 | #define BABELTRACE_CTF_IR_EVENT_INTERNAL_H | |
273b65be JG |
3 | |
4 | /* | |
272df73e | 5 | * Babeltrace - CTF IR: Event internal |
273b65be | 6 | * |
de9dd397 | 7 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
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 | ||
44c440bc PP |
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 | ||
3dca2276 | 35 | #include <babeltrace/assert-pre-internal.h> |
273b65be | 36 | #include <babeltrace/babeltrace-internal.h> |
dac5c838 | 37 | #include <babeltrace/values.h> |
44c440bc | 38 | #include <babeltrace/ctf-ir/clock-value-internal.h> |
123fbdec JG |
39 | #include <babeltrace/ctf-ir/stream-class.h> |
40 | #include <babeltrace/ctf-ir/stream.h> | |
26e21a82 | 41 | #include <babeltrace/ctf-ir/stream-internal.h> |
5c3b707d | 42 | #include <babeltrace/ctf-ir/packet.h> |
6c677fb5 | 43 | #include <babeltrace/ctf-ir/packet-internal.h> |
3dca2276 PP |
44 | #include <babeltrace/ctf-ir/fields.h> |
45 | #include <babeltrace/ctf-ir/fields-internal.h> | |
46 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
312c056a | 47 | #include <babeltrace/ctf-ir/field-wrapper-internal.h> |
83509119 | 48 | #include <babeltrace/object-internal.h> |
f6ccaed9 | 49 | #include <babeltrace/assert-internal.h> |
273b65be JG |
50 | #include <glib.h> |
51 | ||
44c440bc PP |
52 | #define BT_ASSERT_PRE_EVENT_HOT(_event) \ |
53 | BT_ASSERT_PRE_HOT((_event), "Event", ": %!+e", (_event)) | |
dc3fffef | 54 | |
cb6f1f7d | 55 | struct bt_event { |
83509119 | 56 | struct bt_object base; |
cb6f1f7d | 57 | struct bt_event_class *class; |
44c440bc | 58 | struct bt_packet *packet; |
312c056a | 59 | struct bt_field_wrapper *header_field; |
44c440bc PP |
60 | struct bt_field *common_context_field; |
61 | struct bt_field *specific_context_field; | |
cb6f1f7d | 62 | struct bt_field *payload_field; |
44c440bc PP |
63 | struct bt_clock_value *default_cv; |
64 | bool frozen; | |
273b65be JG |
65 | }; |
66 | ||
273b65be | 67 | BT_HIDDEN |
cb6f1f7d | 68 | void bt_event_destroy(struct bt_event *event); |
273b65be | 69 | |
4ce9f9d0 | 70 | BT_HIDDEN |
cb6f1f7d | 71 | struct bt_event *bt_event_new(struct bt_event_class *event_class); |
4ce9f9d0 | 72 | |
f6ccaed9 | 73 | BT_HIDDEN |
6c677fb5 | 74 | void _bt_event_set_is_frozen(struct bt_event *event, bool is_frozen); |
f6ccaed9 PP |
75 | |
76 | #ifdef BT_DEV_MODE | |
6c677fb5 | 77 | # define bt_event_set_is_frozen _bt_event_set_is_frozen |
f6ccaed9 | 78 | #else |
6c677fb5 | 79 | # define bt_event_set_is_frozen(_event, _is_frozen) |
f6ccaed9 PP |
80 | #endif |
81 | ||
cb6f1f7d | 82 | BT_UNUSED |
3dca2276 | 83 | static inline |
cb6f1f7d | 84 | void _bt_event_reset_dev_mode(struct bt_event *event) |
3dca2276 | 85 | { |
cb6f1f7d | 86 | BT_ASSERT(event); |
312c056a PP |
87 | |
88 | if (event->header_field) { | |
44c440bc | 89 | bt_field_set_is_frozen( |
cb6f1f7d | 90 | event->header_field->field, false); |
44c440bc | 91 | bt_field_reset( |
cb6f1f7d | 92 | event->header_field->field); |
312c056a PP |
93 | } |
94 | ||
44c440bc PP |
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); | |
312c056a PP |
100 | } |
101 | ||
44c440bc PP |
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); | |
312c056a PP |
106 | } |
107 | ||
108 | if (event->payload_field) { | |
44c440bc | 109 | bt_field_set_is_frozen( |
cb6f1f7d | 110 | event->payload_field, false); |
44c440bc | 111 | bt_field_reset(event->payload_field); |
6c677fb5 | 112 | } |
6c677fb5 PP |
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 | |
312c056a | 120 | |
6c677fb5 PP |
121 | static inline |
122 | void bt_event_reset(struct bt_event *event) | |
123 | { | |
124 | BT_ASSERT(event); | |
44c440bc | 125 | BT_LIB_LOGD("Resetting event: %!+e", event); |
6c677fb5 | 126 | bt_event_set_is_frozen(event, false); |
44c440bc PP |
127 | |
128 | if (event->default_cv) { | |
129 | bt_clock_value_reset(event->default_cv); | |
130 | } | |
131 | ||
6c677fb5 PP |
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_put() | |
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); | |
cb6f1f7d | 167 | event_class = event->class; |
6c677fb5 | 168 | BT_ASSERT(event_class); |
cb6f1f7d | 169 | event->class = NULL; |
6c677fb5 | 170 | bt_object_pool_recycle_object(&event_class->event_pool, event); |
cb6f1f7d | 171 | bt_object_put_no_null_check(&event_class->base); |
6c677fb5 PP |
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"); | |
44c440bc | 179 | BT_ASSERT_PRE_EVENT_HOT(event); |
26e21a82 | 180 | BT_ASSERT_PRE(bt_event_class_borrow_stream_class( |
44c440bc | 181 | event->class) == packet->stream->class, |
26e21a82 | 182 | "Packet's stream class and event's stream class differ: " |
44c440bc | 183 | "%![event-]+e, %![packet-]+a", event, packet); |
26e21a82 | 184 | |
6c677fb5 PP |
185 | BT_ASSERT(!event->packet); |
186 | event->packet = packet; | |
187 | bt_object_get_no_null_check_no_parent_check(&event->packet->base); | |
44c440bc PP |
188 | BT_LIB_LOGV("Set event's packet: %![event-]+e, %![packet-]+a", |
189 | event, packet); | |
6c677fb5 PP |
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: " | |
44c440bc | 202 | "%![ec-]+E", event_class); |
6c677fb5 PP |
203 | goto end; |
204 | } | |
205 | ||
44c440bc | 206 | if (likely(!event->class)) { |
cb6f1f7d PP |
207 | event->class = event_class; |
208 | bt_object_get_no_null_check(&event_class->base); | |
6c677fb5 PP |
209 | } |
210 | ||
211 | BT_ASSERT(packet); | |
212 | bt_event_set_packet(event, packet); | |
213 | goto end; | |
214 | ||
215 | end: | |
216 | return event; | |
217 | } | |
218 | ||
adc315b8 | 219 | #endif /* BABELTRACE_CTF_IR_EVENT_INTERNAL_H */ |