| 1 | #ifndef BABELTRACE_CTF_IR_EVENT_INTERNAL_H |
| 2 | #define BABELTRACE_CTF_IR_EVENT_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Babeltrace - CTF IR: Event internal |
| 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 | |
| 30 | #include <babeltrace/assert-pre-internal.h> |
| 31 | #include <babeltrace/babeltrace-internal.h> |
| 32 | #include <babeltrace/values.h> |
| 33 | #include <babeltrace/ctf-ir/stream-class.h> |
| 34 | #include <babeltrace/ctf-ir/stream.h> |
| 35 | #include <babeltrace/ctf-ir/packet.h> |
| 36 | #include <babeltrace/ctf-ir/fields.h> |
| 37 | #include <babeltrace/ctf-ir/fields-internal.h> |
| 38 | #include <babeltrace/ctf-ir/event-class-internal.h> |
| 39 | #include <babeltrace/ctf-ir/field-wrapper-internal.h> |
| 40 | #include <babeltrace/ctf-ir/validation-internal.h> |
| 41 | #include <babeltrace/object-internal.h> |
| 42 | #include <babeltrace/assert-internal.h> |
| 43 | #include <glib.h> |
| 44 | |
| 45 | struct bt_stream_pos; |
| 46 | |
| 47 | struct bt_event_common { |
| 48 | struct bt_object base; |
| 49 | struct bt_event_class_common *class; |
| 50 | struct bt_field_wrapper *header_field; |
| 51 | struct bt_field_common *stream_event_context_field; |
| 52 | struct bt_field_common *context_field; |
| 53 | struct bt_field_common *payload_field; |
| 54 | int frozen; |
| 55 | }; |
| 56 | |
| 57 | struct bt_event { |
| 58 | struct bt_event_common common; |
| 59 | |
| 60 | /* Maps clock classes to bt_clock_value. */ |
| 61 | GHashTable *clock_values; |
| 62 | struct bt_packet *packet; |
| 63 | }; |
| 64 | |
| 65 | BT_HIDDEN |
| 66 | int _bt_event_common_validate(struct bt_event_common *event); |
| 67 | |
| 68 | BT_HIDDEN |
| 69 | void _bt_event_common_freeze(struct bt_event_common *event); |
| 70 | |
| 71 | BT_HIDDEN |
| 72 | void _bt_event_freeze(struct bt_event *event); |
| 73 | |
| 74 | #ifdef BT_DEV_MODE |
| 75 | # define bt_event_common_validate _bt_event_common_validate |
| 76 | # define bt_event_common_freeze _bt_event_common_freeze |
| 77 | # define bt_event_freeze _bt_event_freeze |
| 78 | #else |
| 79 | # define bt_event_common_validate(_event) 0 |
| 80 | # define bt_event_common_freeze(_event) |
| 81 | # define bt_event_freeze(_event) |
| 82 | #endif |
| 83 | |
| 84 | #define BT_ASSERT_PRE_EVENT_COMMON_HOT(_event, _name) \ |
| 85 | BT_ASSERT_PRE_HOT((_event), (_name), ": +%!+_e", (_event)) |
| 86 | |
| 87 | static inline |
| 88 | struct bt_event_class_common *bt_event_common_borrow_class( |
| 89 | struct bt_event_common *event) |
| 90 | { |
| 91 | BT_ASSERT(event); |
| 92 | return event->class; |
| 93 | } |
| 94 | |
| 95 | BT_HIDDEN |
| 96 | int bt_event_common_initialize(struct bt_event_common *event, |
| 97 | struct bt_event_class_common *event_class, |
| 98 | struct bt_clock_class *init_expected_clock_class, |
| 99 | bt_object_release_func release_func, |
| 100 | bt_validation_flag_copy_field_type_func field_type_copy_func, |
| 101 | bool must_be_in_trace, |
| 102 | int (*map_clock_classes_func)(struct bt_stream_class_common *stream_class, |
| 103 | struct bt_field_type_common *packet_context_field_type, |
| 104 | struct bt_field_type_common *event_header_field_type), |
| 105 | void *(*create_field_func)(void *), |
| 106 | void (*release_field_func)(void *), |
| 107 | void *(*create_header_field_func)(void *, void *), |
| 108 | void (*release_header_field_func)(void *)); |
| 109 | |
| 110 | static inline |
| 111 | struct bt_field_common *bt_event_common_borrow_payload( |
| 112 | struct bt_event_common *event) |
| 113 | { |
| 114 | struct bt_field_common *payload = NULL; |
| 115 | |
| 116 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 117 | |
| 118 | if (!event->payload_field) { |
| 119 | BT_LOGV("Event has no current payload field: addr=%p, " |
| 120 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 121 | event, bt_event_class_common_get_name(event->class), |
| 122 | bt_event_class_common_get_id(event->class)); |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | payload = event->payload_field; |
| 127 | |
| 128 | end: |
| 129 | return payload; |
| 130 | } |
| 131 | |
| 132 | static inline |
| 133 | struct bt_field_common *bt_event_common_borrow_header( |
| 134 | struct bt_event_common *event) |
| 135 | { |
| 136 | struct bt_field_common *header = NULL; |
| 137 | |
| 138 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 139 | |
| 140 | if (!event->header_field) { |
| 141 | BT_LOGV("Event has no current header field: addr=%p, " |
| 142 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 143 | event, bt_event_class_common_get_name(event->class), |
| 144 | bt_event_class_common_get_id(event->class)); |
| 145 | goto end; |
| 146 | } |
| 147 | |
| 148 | header = event->header_field->field; |
| 149 | |
| 150 | end: |
| 151 | return header; |
| 152 | } |
| 153 | |
| 154 | static inline |
| 155 | struct bt_field_common *bt_event_common_borrow_context( |
| 156 | struct bt_event_common *event) |
| 157 | { |
| 158 | struct bt_field_common *context = NULL; |
| 159 | |
| 160 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 161 | |
| 162 | if (!event->context_field) { |
| 163 | BT_LOGV("Event has no current context field: addr=%p, " |
| 164 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 165 | event, bt_event_class_common_get_name(event->class), |
| 166 | bt_event_class_common_get_id(event->class)); |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | context = event->context_field; |
| 171 | |
| 172 | end: |
| 173 | return context; |
| 174 | } |
| 175 | |
| 176 | static inline |
| 177 | struct bt_field_common *bt_event_common_borrow_stream_event_context( |
| 178 | struct bt_event_common *event) |
| 179 | { |
| 180 | struct bt_field_common *stream_event_context = NULL; |
| 181 | |
| 182 | BT_ASSERT_PRE_NON_NULL(event, "Event"); |
| 183 | |
| 184 | if (!event->stream_event_context_field) { |
| 185 | BT_LOGV("Event has no current stream event context field: addr=%p, " |
| 186 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 187 | event, bt_event_class_common_get_name(event->class), |
| 188 | bt_event_class_common_get_id(event->class)); |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | stream_event_context = event->stream_event_context_field; |
| 193 | |
| 194 | end: |
| 195 | return stream_event_context; |
| 196 | } |
| 197 | |
| 198 | static inline |
| 199 | void bt_event_common_finalize(struct bt_object *obj, |
| 200 | void (*field_release_func)(void *), |
| 201 | void (*header_field_release_func)(void *, struct bt_event_common *)) |
| 202 | { |
| 203 | struct bt_event_common *event = (void *) obj; |
| 204 | |
| 205 | BT_LOGD("Destroying event: addr=%p, " |
| 206 | "event-class-name=\"%s\", event-class-id=%" PRId64, |
| 207 | event, |
| 208 | event->class ? bt_event_class_common_get_name(event->class) : NULL, |
| 209 | event->class ? bt_event_class_common_get_id(event->class) : INT64_C(-1)); |
| 210 | |
| 211 | if (event->header_field) { |
| 212 | BT_LOGD_STR("Releasing event's header field."); |
| 213 | header_field_release_func(event->header_field, event); |
| 214 | } |
| 215 | |
| 216 | if (event->stream_event_context_field) { |
| 217 | BT_LOGD_STR("Releasing event's stream event context field."); |
| 218 | field_release_func(event->stream_event_context_field); |
| 219 | } |
| 220 | |
| 221 | if (event->context_field) { |
| 222 | BT_LOGD_STR("Releasing event's context field."); |
| 223 | field_release_func(event->context_field); |
| 224 | } |
| 225 | |
| 226 | if (event->payload_field) { |
| 227 | BT_LOGD_STR("Releasing event's payload field."); |
| 228 | field_release_func(event->payload_field); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Leave this after calling header_field_release_func() because |
| 233 | * this function receives the event object and could need its |
| 234 | * class to perform some cleanup. |
| 235 | */ |
| 236 | if (!event->base.parent) { |
| 237 | /* |
| 238 | * Event was keeping a reference to its class since it shared no |
| 239 | * common ancestor with it to guarantee they would both have the |
| 240 | * same lifetime. |
| 241 | */ |
| 242 | bt_put(event->class); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | BT_HIDDEN |
| 247 | struct bt_event *bt_event_new(struct bt_event_class *event_class); |
| 248 | |
| 249 | BT_HIDDEN |
| 250 | struct bt_event *bt_event_create(struct bt_event_class *event_class, |
| 251 | struct bt_packet *packet); |
| 252 | |
| 253 | BT_HIDDEN |
| 254 | void bt_event_recycle(struct bt_event *event); |
| 255 | |
| 256 | BT_HIDDEN |
| 257 | void bt_event_destroy(struct bt_event *event); |
| 258 | |
| 259 | BT_HIDDEN |
| 260 | int bt_event_set_packet(struct bt_event *event, struct bt_packet *packet); |
| 261 | |
| 262 | #endif /* BABELTRACE_CTF_IR_EVENT_INTERNAL_H */ |