ctf plugin: notif iter: use "borrow" functions for metadata where possible
[babeltrace.git] / lib / ctf-ir / event-class.c
1 /*
2 * event-class.c
3 *
4 * Babeltrace CTF IR - Event class
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "EVENT-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/fields-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/event-class.h>
36 #include <babeltrace/ctf-ir/event-class-internal.h>
37 #include <babeltrace/ctf-ir/stream-class.h>
38 #include <babeltrace/ctf-ir/stream-class-internal.h>
39 #include <babeltrace/ctf-ir/trace-internal.h>
40 #include <babeltrace/ctf-ir/validation-internal.h>
41 #include <babeltrace/ctf-ir/utils.h>
42 #include <babeltrace/ctf-ir/utils-internal.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/ctf-ir/attributes-internal.h>
45 #include <babeltrace/compiler-internal.h>
46 #include <babeltrace/endian-internal.h>
47 #include <babeltrace/types.h>
48 #include <babeltrace/values-internal.h>
49 #include <babeltrace/assert-internal.h>
50 #include <inttypes.h>
51 #include <stdlib.h>
52
53 BT_HIDDEN
54 void bt_event_class_common_finalize(struct bt_object *obj)
55 {
56 struct bt_event_class_common *event_class;
57
58 event_class = container_of(obj, struct bt_event_class_common, base);
59 BT_LOGD("Finalizing common event class: addr=%p, name=\"%s\", id=%" PRId64,
60 event_class, bt_event_class_common_get_name(event_class),
61 bt_event_class_common_get_id(event_class));
62
63 if (event_class->name) {
64 g_string_free(event_class->name, TRUE);
65 }
66
67 if (event_class->emf_uri) {
68 g_string_free(event_class->emf_uri, TRUE);
69 }
70
71 BT_LOGD_STR("Putting context field type.");
72 bt_put(event_class->context_field_type);
73 BT_LOGD_STR("Putting payload field type.");
74 bt_put(event_class->payload_field_type);
75 }
76
77 BT_HIDDEN
78 int bt_event_class_common_initialize(struct bt_event_class_common *event_class,
79 const char *name, bt_object_release_func release_func,
80 bt_field_type_structure_create_func ft_struct_create_func)
81 {
82 int ret = 0;
83
84 BT_LOGD("Initializing common event class object: name=\"%s\"",
85 name);
86 bt_object_init(event_class, release_func);
87 event_class->payload_field_type = ft_struct_create_func();
88 if (!event_class->payload_field_type) {
89 BT_LOGE_STR("Cannot create event class's initial payload field type object.");
90 goto error;
91 }
92
93 event_class->id = -1;
94 event_class->name = g_string_new(name);
95 if (!event_class->name) {
96 BT_LOGE_STR("Failed to allocate a GString.");
97 goto error;
98 }
99
100 event_class->emf_uri = g_string_new(NULL);
101 if (!event_class->emf_uri) {
102 BT_LOGE_STR("Failed to allocate a GString.");
103 goto error;
104 }
105
106 event_class->log_level = BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED;
107 BT_LOGD("Initialized common event class object: addr=%p, name=\"%s\"",
108 event_class, bt_event_class_common_get_name(event_class));
109 return ret;
110
111 error:
112 ret = -1;
113 return ret;
114 }
115
116 static
117 void bt_event_class_destroy(struct bt_object *obj)
118 {
119 BT_LOGD("Destroying event class: addr=%p", obj);
120 bt_event_class_common_finalize(obj);
121 g_free(obj);
122 }
123
124 struct bt_event_class *bt_event_class_create(const char *name)
125 {
126 int ret;
127 struct bt_event_class *event_class = NULL;
128
129 if (!name) {
130 BT_LOGW_STR("Invalid parameter: name is NULL.");
131 goto error;
132 }
133
134 BT_LOGD("Creating event class object: name=\"%s\"",
135 name);
136 event_class = g_new0(struct bt_event_class, 1);
137 if (!event_class) {
138 BT_LOGE_STR("Failed to allocate one event class.");
139 goto error;
140 }
141
142 ret = bt_event_class_common_initialize(BT_TO_COMMON(event_class),
143 name, bt_event_class_destroy,
144 (bt_field_type_structure_create_func)
145 bt_field_type_structure_create);
146 if (ret) {
147 goto error;
148 }
149
150 BT_LOGD("Created event class object: addr=%p, name=\"%s\"",
151 event_class, bt_event_class_get_name(event_class));
152 goto end;
153
154 error:
155 bt_put(event_class);
156
157 end:
158 return event_class;
159 }
160
161 const char *bt_event_class_get_name(struct bt_event_class *event_class)
162 {
163 return bt_event_class_common_get_name(BT_TO_COMMON(event_class));
164 }
165
166 int64_t bt_event_class_get_id(struct bt_event_class *event_class)
167 {
168 return bt_event_class_common_get_id(BT_TO_COMMON(event_class));
169 }
170
171 int bt_event_class_set_id(struct bt_event_class *event_class, uint64_t id)
172 {
173 return bt_event_class_common_set_id(BT_TO_COMMON(event_class), id);
174 }
175
176 enum bt_event_class_log_level bt_event_class_get_log_level(
177 struct bt_event_class *event_class)
178 {
179 return bt_event_class_common_get_log_level(BT_TO_COMMON(event_class));
180 }
181
182 int bt_event_class_set_log_level(struct bt_event_class *event_class,
183 enum bt_event_class_log_level log_level)
184 {
185 return bt_event_class_common_set_log_level(BT_TO_COMMON(event_class),
186 log_level);
187 }
188
189 const char *bt_event_class_get_emf_uri(struct bt_event_class *event_class)
190 {
191 return bt_event_class_common_get_emf_uri(BT_TO_COMMON(event_class));
192 }
193
194 int bt_event_class_set_emf_uri(struct bt_event_class *event_class,
195 const char *emf_uri)
196 {
197 return bt_event_class_common_set_emf_uri(BT_TO_COMMON(event_class),
198 emf_uri);
199 }
200
201 struct bt_stream_class *bt_event_class_borrow_stream_class(
202 struct bt_event_class *event_class)
203 {
204 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
205 return BT_FROM_COMMON(
206 bt_event_class_common_borrow_stream_class(BT_TO_COMMON(
207 event_class)));
208 }
209
210 struct bt_field_type *bt_event_class_borrow_payload_field_type(
211 struct bt_event_class *event_class)
212 {
213 return BT_FROM_COMMON(bt_event_class_common_borrow_payload_field_type(
214 BT_TO_COMMON(event_class)));
215 }
216
217 int bt_event_class_set_payload_field_type(struct bt_event_class *event_class,
218 struct bt_field_type *field_type)
219 {
220 return bt_event_class_common_set_payload_field_type(
221 BT_TO_COMMON(event_class), (void *) field_type);
222 }
223
224 struct bt_field_type *bt_event_class_borrow_context_field_type(
225 struct bt_event_class *event_class)
226 {
227 return BT_FROM_COMMON(bt_event_class_common_borrow_context_field_type(
228 BT_TO_COMMON(event_class)));
229 }
230
231 int bt_event_class_set_context_field_type(
232 struct bt_event_class *event_class,
233 struct bt_field_type *field_type)
234 {
235 return bt_event_class_common_set_context_field_type(
236 BT_TO_COMMON(event_class), (void *) field_type);
237 }
238
239 BT_HIDDEN
240 void bt_event_class_common_freeze(struct bt_event_class_common *event_class)
241 {
242 BT_ASSERT(event_class);
243
244 if (event_class->frozen) {
245 return;
246 }
247
248 BT_LOGD("Freezing event class: addr=%p, name=\"%s\", id=%" PRId64,
249 event_class, bt_event_class_common_get_name(event_class),
250 bt_event_class_common_get_id(event_class));
251 event_class->frozen = 1;
252 BT_LOGD_STR("Freezing event class's context field type.");
253 bt_field_type_common_freeze(event_class->context_field_type);
254 BT_LOGD_STR("Freezing event class's payload field type.");
255 bt_field_type_common_freeze(event_class->payload_field_type);
256 }
257
258 BT_HIDDEN
259 int bt_event_class_common_validate_single_clock_class(
260 struct bt_event_class_common *event_class,
261 struct bt_clock_class **expected_clock_class)
262 {
263 int ret = 0;
264
265 BT_ASSERT(event_class);
266 BT_ASSERT(expected_clock_class);
267 ret = bt_field_type_common_validate_single_clock_class(
268 event_class->context_field_type,
269 expected_clock_class);
270 if (ret) {
271 BT_LOGW("Event class's context field type "
272 "is not recursively mapped to the "
273 "expected clock class: "
274 "event-class-addr=%p, "
275 "event-class-name=\"%s\", "
276 "event-class-id=%" PRId64 ", "
277 "ft-addr=%p",
278 event_class,
279 bt_event_class_common_get_name(event_class),
280 event_class->id,
281 event_class->context_field_type);
282 goto end;
283 }
284
285 ret = bt_field_type_common_validate_single_clock_class(
286 event_class->payload_field_type,
287 expected_clock_class);
288 if (ret) {
289 BT_LOGW("Event class's payload field type "
290 "is not recursively mapped to the "
291 "expected clock class: "
292 "event-class-addr=%p, "
293 "event-class-name=\"%s\", "
294 "event-class-id=%" PRId64 ", "
295 "ft-addr=%p",
296 event_class,
297 bt_event_class_common_get_name(event_class),
298 event_class->id,
299 event_class->payload_field_type);
300 goto end;
301 }
302
303 end:
304 return ret;
305 }
This page took 0.034733 seconds and 4 git commands to generate.