lib: fully detach CTF IR and CTF writer implementations
[babeltrace.git] / include / babeltrace / ctf-writer / event-internal.h
CommitLineData
3dca2276
PP
1#ifndef BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H
2#define BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H
3
4/*
5 * BabelTrace - CTF Writer: Event
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 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
31 */
32
16ca5ff0
PP
33#include <babeltrace/assert-internal.h>
34#include <babeltrace/assert-pre-internal.h>
3dca2276 35#include <babeltrace/babeltrace-internal.h>
16ca5ff0
PP
36#include <babeltrace/ctf-writer/event-class-internal.h>
37#include <babeltrace/ctf-writer/event-internal.h>
3dca2276 38#include <babeltrace/ctf-writer/field-types.h>
16ca5ff0
PP
39#include <babeltrace/ctf-writer/field-wrapper-internal.h>
40#include <babeltrace/ctf-writer/fields-internal.h>
41#include <babeltrace/ctf-writer/fields.h>
42#include <babeltrace/ctf-writer/stream-class.h>
43#include <babeltrace/ctf-writer/stream-internal.h>
44#include <babeltrace/ctf-writer/stream.h>
45#include <babeltrace/ctf-writer/validation-internal.h>
46#include <babeltrace/object-internal.h>
47#include <babeltrace/values.h>
3dca2276
PP
48
49struct bt_ctf_stream_class;
50struct bt_ctf_stream_pos;
51struct metadata_context;
52
16ca5ff0
PP
53struct bt_ctf_event_common {
54 struct bt_object base;
55 struct bt_ctf_event_class_common *class;
56 struct bt_ctf_field_wrapper *header_field;
57 struct bt_ctf_field_common *stream_event_context_field;
58 struct bt_ctf_field_common *context_field;
59 struct bt_ctf_field_common *payload_field;
60 int frozen;
61};
62
63BT_HIDDEN
64int _bt_ctf_event_common_validate(struct bt_ctf_event_common *event);
65
66BT_HIDDEN
67void _bt_ctf_event_common_set_is_frozen(struct bt_ctf_event_common *event,
68 bool is_frozen);
69
70#ifdef BT_DEV_MODE
71# define bt_ctf_event_common_validate _bt_ctf_event_common_validate
72# define bt_ctf_event_common_set_is_frozen _bt_ctf_event_common_set_is_frozen
73#else
74# define bt_ctf_event_common_validate(_event) 0
75# define bt_ctf_event_common_set_is_frozen(_event, _is_frozen)
76#endif
77
78#define BT_ASSERT_PRE_EVENT_COMMON_HOT(_event, _name) \
79 BT_ASSERT_PRE_HOT((_event), (_name), ": event-addr=%p", (_event))
80
81static inline
82struct bt_ctf_event_class_common *bt_ctf_event_common_borrow_class(
83 struct bt_ctf_event_common *event)
84{
85 BT_ASSERT(event);
86 return event->class;
87}
88
89typedef void *(*create_field_func)(void *);
90typedef void (*release_field_func)(void *);
91typedef void *(*create_header_field_func)(void *, void *);
92typedef void (*release_header_field_func)(void *, void *);
93
94BT_HIDDEN
95int bt_ctf_event_common_initialize(struct bt_ctf_event_common *event,
96 struct bt_ctf_event_class_common *event_class,
97 struct bt_ctf_clock_class *init_expected_clock_class,
98 bool is_shared_with_parent, bt_object_release_func release_func,
99 bt_ctf_validation_flag_copy_field_type_func field_type_copy_func,
100 bool must_be_in_trace,
101 int (*map_clock_classes_func)(struct bt_ctf_stream_class_common *stream_class,
102 struct bt_ctf_field_type_common *packet_context_field_type,
103 struct bt_ctf_field_type_common *event_header_field_type),
104 create_field_func create_field_func,
105 release_field_func release_field_func,
106 create_header_field_func create_header_field_func,
107 release_header_field_func release_header_field_func);
108
109static inline
110struct bt_ctf_field_common *bt_ctf_event_common_borrow_payload(
111 struct bt_ctf_event_common *event)
112{
113 struct bt_ctf_field_common *payload = NULL;
114
115 BT_ASSERT_PRE_NON_NULL(event, "Event");
116
117 if (!event->payload_field) {
118 BT_LOGV("Event has no current payload field: addr=%p, "
119 "event-class-name=\"%s\", event-class-id=%" PRId64,
120 event, bt_ctf_event_class_common_get_name(event->class),
121 bt_ctf_event_class_common_get_id(event->class));
122 goto end;
123 }
124
125 payload = event->payload_field;
126
127end:
128 return payload;
129}
130
131static inline
132struct bt_ctf_field_common *bt_ctf_event_common_borrow_header(
133 struct bt_ctf_event_common *event)
134{
135 struct bt_ctf_field_common *header = NULL;
136
137 BT_ASSERT_PRE_NON_NULL(event, "Event");
138
139 if (!event->header_field) {
140 BT_LOGV("Event has no current header field: addr=%p, "
141 "event-class-name=\"%s\", event-class-id=%" PRId64,
142 event, bt_ctf_event_class_common_get_name(event->class),
143 bt_ctf_event_class_common_get_id(event->class));
144 goto end;
145 }
146
147 header = event->header_field->field;
148
149end:
150 return header;
151}
152
153static inline
154struct bt_ctf_field_common *bt_ctf_event_common_borrow_context(
155 struct bt_ctf_event_common *event)
156{
157 struct bt_ctf_field_common *context = NULL;
158
159 BT_ASSERT_PRE_NON_NULL(event, "Event");
160
161 if (!event->context_field) {
162 BT_LOGV("Event has no current context field: addr=%p, "
163 "event-class-name=\"%s\", event-class-id=%" PRId64,
164 event, bt_ctf_event_class_common_get_name(event->class),
165 bt_ctf_event_class_common_get_id(event->class));
166 goto end;
167 }
168
169 context = event->context_field;
170
171end:
172 return context;
173}
174
175static inline
176struct bt_ctf_field_common *bt_ctf_event_common_borrow_stream_event_context(
177 struct bt_ctf_event_common *event)
178{
179 struct bt_ctf_field_common *stream_event_context = NULL;
180
181 BT_ASSERT_PRE_NON_NULL(event, "Event");
182
183 if (!event->stream_event_context_field) {
184 BT_LOGV("Event has no current stream event context field: addr=%p, "
185 "event-class-name=\"%s\", event-class-id=%" PRId64,
186 event, bt_ctf_event_class_common_get_name(event->class),
187 bt_ctf_event_class_common_get_id(event->class));
188 goto end;
189 }
190
191 stream_event_context = event->stream_event_context_field;
192
193end:
194 return stream_event_context;
195}
196
197static inline
198void bt_ctf_event_common_finalize(struct bt_object *obj,
199 void (*field_release_func)(void *),
200 void (*header_field_release_func)(void *, struct bt_ctf_event_common *))
201{
202 struct bt_ctf_event_common *event = (void *) obj;
203
204 BT_LOGD("Destroying event: addr=%p, "
205 "event-class-name=\"%s\", event-class-id=%" PRId64,
206 event,
207 event->class ? bt_ctf_event_class_common_get_name(event->class) : NULL,
208 event->class ? bt_ctf_event_class_common_get_id(event->class) : INT64_C(-1));
209
210 if (event->header_field) {
211 BT_LOGD_STR("Releasing event's header field.");
212 header_field_release_func(event->header_field, event);
213 }
214
215 if (event->stream_event_context_field) {
216 BT_LOGD_STR("Releasing event's stream event context field.");
217 field_release_func(event->stream_event_context_field);
218 }
219
220 if (event->context_field) {
221 BT_LOGD_STR("Releasing event's context field.");
222 field_release_func(event->context_field);
223 }
224
225 if (event->payload_field) {
226 BT_LOGD_STR("Releasing event's payload field.");
227 field_release_func(event->payload_field);
228 }
229
230 /*
231 * Leave this after calling header_field_release_func() because
232 * this function receives the event object and could need its
233 * class to perform some cleanup.
234 */
235 if (!event->base.parent) {
236 /*
237 * Event was keeping a reference to its class since it shared no
238 * common ancestor with it to guarantee they would both have the
239 * same lifetime.
240 */
241 bt_put(event->class);
242 }
243}
244
3dca2276 245struct bt_ctf_event {
16ca5ff0 246 struct bt_ctf_event_common common;
3dca2276
PP
247};
248
249struct bt_ctf_event_class {
16ca5ff0 250 struct bt_ctf_event_class_common common;
3dca2276
PP
251};
252
253BT_HIDDEN
254int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
255 struct metadata_context *context);
256
257BT_HIDDEN
258int bt_ctf_event_serialize(struct bt_ctf_event *event,
259 struct bt_ctf_stream_pos *pos,
260 enum bt_ctf_byte_order native_byte_order);
261
262static inline
263struct bt_ctf_stream_class *bt_ctf_event_class_borrow_stream_class(
264 struct bt_ctf_event_class *event_class)
265{
16ca5ff0
PP
266 return BT_CTF_FROM_COMMON(bt_ctf_event_class_common_borrow_stream_class(
267 BT_CTF_TO_COMMON(event_class)));
3dca2276
PP
268}
269
270#endif /* BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H */
This page took 0.033529 seconds and 4 git commands to generate.