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