Commit | Line | Data |
---|---|---|
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 PP |
27 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
28 | #ifndef BABELTRACE_LIB_LOGGING_INTERNAL_H | |
ea5679f7 | 29 | # error Please include "lib/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 | ||
7b33a0e0 | 49 | #define BT_ASSERT_PRE_EVENT_HOT(_event) \ |
78cf9df6 PP |
50 | BT_ASSERT_PRE_HOT(((const struct bt_event *) (_event)), \ |
51 | "Event", ": %!+e", (_event)) | |
dc3fffef | 52 | |
18acc6f8 | 53 | struct bt_event { |
83509119 | 54 | struct bt_object base; |
10b7a2e4 PP |
55 | |
56 | /* Owned by this */ | |
18acc6f8 | 57 | struct bt_event_class *class; |
10b7a2e4 PP |
58 | |
59 | /* Owned by this */ | |
7b33a0e0 | 60 | struct bt_packet *packet; |
10b7a2e4 | 61 | |
7b33a0e0 PP |
62 | struct bt_field *common_context_field; |
63 | struct bt_field *specific_context_field; | |
18acc6f8 | 64 | struct bt_field *payload_field; |
7b33a0e0 | 65 | bool frozen; |
273b65be JG |
66 | }; |
67 | ||
273b65be | 68 | BT_HIDDEN |
18acc6f8 | 69 | void bt_event_destroy(struct bt_event *event); |
273b65be | 70 | |
4ce9f9d0 | 71 | BT_HIDDEN |
18acc6f8 | 72 | struct bt_event *bt_event_new(struct bt_event_class *event_class); |
4ce9f9d0 | 73 | |
8b45963b | 74 | BT_HIDDEN |
78cf9df6 | 75 | void _bt_event_set_is_frozen(const struct bt_event *event, bool is_frozen); |
8b45963b PP |
76 | |
77 | #ifdef BT_DEV_MODE | |
c5a24b0a | 78 | # define bt_event_set_is_frozen _bt_event_set_is_frozen |
8b45963b | 79 | #else |
c5a24b0a | 80 | # define bt_event_set_is_frozen(_event, _is_frozen) |
8b45963b PP |
81 | #endif |
82 | ||
85e7137b | 83 | __attribute__((unused)) |
8deee039 | 84 | static inline |
18acc6f8 | 85 | void _bt_event_reset_dev_mode(struct bt_event *event) |
8deee039 | 86 | { |
18acc6f8 | 87 | BT_ASSERT(event); |
a6918753 | 88 | |
7b33a0e0 PP |
89 | if (event->common_context_field) { |
90 | bt_field_set_is_frozen( | |
91 | event->common_context_field, false); | |
92 | bt_field_reset( | |
93 | event->common_context_field); | |
a6918753 PP |
94 | } |
95 | ||
7b33a0e0 PP |
96 | if (event->specific_context_field) { |
97 | bt_field_set_is_frozen( | |
98 | event->specific_context_field, false); | |
99 | bt_field_reset(event->specific_context_field); | |
a6918753 PP |
100 | } |
101 | ||
102 | if (event->payload_field) { | |
7b33a0e0 | 103 | bt_field_set_is_frozen( |
18acc6f8 | 104 | event->payload_field, false); |
7b33a0e0 | 105 | bt_field_reset(event->payload_field); |
c5a24b0a | 106 | } |
c5a24b0a PP |
107 | } |
108 | ||
109 | #ifdef BT_DEV_MODE | |
110 | # define bt_event_reset_dev_mode _bt_event_reset_dev_mode | |
111 | #else | |
112 | # define bt_event_reset_dev_mode(_x) | |
113 | #endif | |
a6918753 | 114 | |
c5a24b0a PP |
115 | static inline |
116 | void bt_event_reset(struct bt_event *event) | |
117 | { | |
118 | BT_ASSERT(event); | |
7b33a0e0 | 119 | BT_LIB_LOGD("Resetting event: %!+e", event); |
c5a24b0a | 120 | bt_event_set_is_frozen(event, false); |
c5a24b0a PP |
121 | bt_object_put_no_null_check(&event->packet->base); |
122 | event->packet = NULL; | |
123 | } | |
124 | ||
125 | static inline | |
126 | void bt_event_recycle(struct bt_event *event) | |
127 | { | |
128 | struct bt_event_class *event_class; | |
129 | ||
130 | BT_ASSERT(event); | |
131 | BT_LIB_LOGD("Recycling event: %!+e", event); | |
132 | ||
133 | /* | |
134 | * Those are the important ordered steps: | |
135 | * | |
136 | * 1. Reset the event object (put any permanent reference it | |
137 | * has, unfreeze it and its fields in developer mode, etc.), | |
138 | * but do NOT put its class's reference. This event class | |
139 | * contains the pool to which we're about to recycle this | |
140 | * event object, so we must guarantee its existence thanks | |
141 | * to this existing reference. | |
142 | * | |
143 | * 2. Move the event class reference to our `event_class` | |
144 | * variable so that we can set the event's class member | |
145 | * to NULL before recycling it. We CANNOT do this after | |
8138bfe1 | 146 | * we put the event class reference because this bt_object_put_ref() |
c5a24b0a PP |
147 | * could destroy the event class, also destroying its |
148 | * event pool, thus also destroying our event object (this | |
149 | * would result in an invalid write access). | |
150 | * | |
151 | * 3. Recycle the event object. | |
152 | * | |
153 | * 4. Put our event class reference. | |
154 | */ | |
155 | bt_event_reset(event); | |
18acc6f8 | 156 | event_class = event->class; |
c5a24b0a | 157 | BT_ASSERT(event_class); |
18acc6f8 | 158 | event->class = NULL; |
c5a24b0a | 159 | bt_object_pool_recycle_object(&event_class->event_pool, event); |
18acc6f8 | 160 | bt_object_put_no_null_check(&event_class->base); |
c5a24b0a PP |
161 | } |
162 | ||
163 | static inline | |
164 | void bt_event_set_packet(struct bt_event *event, struct bt_packet *packet) | |
165 | { | |
166 | BT_ASSERT_PRE_NON_NULL(event, "Event"); | |
167 | BT_ASSERT_PRE_NON_NULL(packet, "Packet"); | |
7b33a0e0 | 168 | BT_ASSERT_PRE_EVENT_HOT(event); |
22e3b27d | 169 | BT_ASSERT_PRE(bt_event_class_borrow_stream_class( |
7b33a0e0 | 170 | event->class) == packet->stream->class, |
22e3b27d | 171 | "Packet's stream class and event's stream class differ: " |
7b33a0e0 | 172 | "%![event-]+e, %![packet-]+a", event, packet); |
22e3b27d | 173 | |
c5a24b0a PP |
174 | BT_ASSERT(!event->packet); |
175 | event->packet = packet; | |
176 | bt_object_get_no_null_check_no_parent_check(&event->packet->base); | |
a684a357 | 177 | BT_LIB_LOGD("Set event's packet: %![event-]+e, %![packet-]+a", |
7b33a0e0 | 178 | event, packet); |
c5a24b0a PP |
179 | } |
180 | ||
181 | static inline | |
182 | struct bt_event *bt_event_create(struct bt_event_class *event_class, | |
183 | struct bt_packet *packet) | |
184 | { | |
185 | struct bt_event *event = NULL; | |
186 | ||
187 | BT_ASSERT(event_class); | |
188 | event = bt_object_pool_create_object(&event_class->event_pool); | |
85e7137b | 189 | if (G_UNLIKELY(!event)) { |
c5a24b0a | 190 | BT_LIB_LOGE("Cannot allocate one event from event class's event pool: " |
7b33a0e0 | 191 | "%![ec-]+E", event_class); |
c5a24b0a PP |
192 | goto end; |
193 | } | |
194 | ||
85e7137b | 195 | if (G_LIKELY(!event->class)) { |
18acc6f8 PP |
196 | event->class = event_class; |
197 | bt_object_get_no_null_check(&event_class->base); | |
c5a24b0a PP |
198 | } |
199 | ||
200 | BT_ASSERT(packet); | |
201 | bt_event_set_packet(event, packet); | |
202 | goto end; | |
203 | ||
204 | end: | |
205 | return event; | |
206 | } | |
207 | ||
108b91d0 | 208 | #endif /* BABELTRACE_TRACE_IR_EVENT_INTERNAL_H */ |