| 1 | /* |
| 2 | * iterator.c |
| 3 | * |
| 4 | * Babeltrace Notification Iterator |
| 5 | * |
| 6 | * Copyright 2015 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 | #include <babeltrace/compiler-internal.h> |
| 30 | #include <babeltrace/ref.h> |
| 31 | #include <babeltrace/graph/component.h> |
| 32 | #include <babeltrace/graph/component-source-internal.h> |
| 33 | #include <babeltrace/graph/component-class-internal.h> |
| 34 | #include <babeltrace/graph/notification-iterator.h> |
| 35 | #include <babeltrace/graph/notification-iterator-internal.h> |
| 36 | #include <babeltrace/graph/notification-internal.h> |
| 37 | |
| 38 | static |
| 39 | void bt_notification_iterator_destroy(struct bt_object *obj) |
| 40 | { |
| 41 | struct bt_notification_iterator *iterator; |
| 42 | struct bt_component_class *comp_class; |
| 43 | |
| 44 | assert(obj); |
| 45 | iterator = container_of(obj, struct bt_notification_iterator, |
| 46 | base); |
| 47 | assert(iterator->component); |
| 48 | comp_class = iterator->component->class; |
| 49 | |
| 50 | /* Call user-defined destroy method */ |
| 51 | switch (comp_class->type) { |
| 52 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 53 | { |
| 54 | struct bt_component_class_source *source_class; |
| 55 | |
| 56 | source_class = container_of(comp_class, struct bt_component_class_source, parent); |
| 57 | |
| 58 | if (source_class->methods.iterator.finalize) { |
| 59 | source_class->methods.iterator.finalize( |
| 60 | bt_private_notification_iterator_from_notification_iterator(iterator)); |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 65 | { |
| 66 | struct bt_component_class_filter *filter_class; |
| 67 | |
| 68 | filter_class = container_of(comp_class, struct bt_component_class_filter, parent); |
| 69 | |
| 70 | if (filter_class->methods.iterator.finalize) { |
| 71 | filter_class->methods.iterator.finalize( |
| 72 | bt_private_notification_iterator_from_notification_iterator(iterator)); |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | default: |
| 77 | /* Unreachable */ |
| 78 | assert(0); |
| 79 | } |
| 80 | |
| 81 | BT_PUT(iterator->current_notification); |
| 82 | BT_PUT(iterator->component); |
| 83 | g_free(iterator); |
| 84 | } |
| 85 | |
| 86 | BT_HIDDEN |
| 87 | struct bt_notification_iterator *bt_notification_iterator_create( |
| 88 | struct bt_component *component) |
| 89 | { |
| 90 | enum bt_component_class_type type; |
| 91 | struct bt_notification_iterator *iterator = NULL; |
| 92 | |
| 93 | if (!component) { |
| 94 | goto end; |
| 95 | } |
| 96 | |
| 97 | type = bt_component_get_class_type(component); |
| 98 | switch (type) { |
| 99 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 100 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 101 | break; |
| 102 | default: |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | iterator = g_new0(struct bt_notification_iterator, 1); |
| 107 | if (!iterator) { |
| 108 | goto end; |
| 109 | } |
| 110 | |
| 111 | iterator->component = bt_get(component); |
| 112 | bt_object_init(iterator, bt_notification_iterator_destroy); |
| 113 | end: |
| 114 | return iterator; |
| 115 | } |
| 116 | |
| 117 | BT_HIDDEN |
| 118 | enum bt_notification_iterator_status bt_notification_iterator_validate( |
| 119 | struct bt_notification_iterator *iterator) |
| 120 | { |
| 121 | enum bt_notification_iterator_status ret = |
| 122 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 123 | |
| 124 | if (!iterator) { |
| 125 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
| 126 | goto end; |
| 127 | } |
| 128 | end: |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | void *bt_private_notification_iterator_get_user_data( |
| 133 | struct bt_private_notification_iterator *private_iterator) |
| 134 | { |
| 135 | struct bt_notification_iterator *iterator = |
| 136 | bt_notification_iterator_from_private(private_iterator); |
| 137 | |
| 138 | return iterator ? iterator->user_data : NULL; |
| 139 | } |
| 140 | |
| 141 | enum bt_notification_iterator_status |
| 142 | bt_private_notification_iterator_set_user_data( |
| 143 | struct bt_private_notification_iterator *private_iterator, |
| 144 | void *data) |
| 145 | { |
| 146 | enum bt_notification_iterator_status ret = |
| 147 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 148 | struct bt_notification_iterator *iterator = |
| 149 | bt_notification_iterator_from_private(private_iterator); |
| 150 | |
| 151 | if (!iterator) { |
| 152 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | iterator->user_data = data; |
| 157 | end: |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | struct bt_notification *bt_notification_iterator_get_notification( |
| 162 | struct bt_notification_iterator *iterator) |
| 163 | { |
| 164 | struct bt_notification *notification = NULL; |
| 165 | |
| 166 | if (!iterator) { |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | notification = bt_get(iterator->current_notification); |
| 171 | |
| 172 | end: |
| 173 | return notification; |
| 174 | } |
| 175 | |
| 176 | enum bt_notification_iterator_status |
| 177 | bt_notification_iterator_next(struct bt_notification_iterator *iterator) |
| 178 | { |
| 179 | struct bt_private_notification_iterator *priv_iterator = |
| 180 | bt_private_notification_iterator_from_notification_iterator(iterator); |
| 181 | bt_component_class_notification_iterator_next_method next_method = NULL; |
| 182 | struct bt_notification_iterator_next_return next_return = { |
| 183 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, |
| 184 | .notification = NULL, |
| 185 | }; |
| 186 | |
| 187 | if (!iterator) { |
| 188 | next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
| 189 | goto end; |
| 190 | } |
| 191 | |
| 192 | assert(iterator->component); |
| 193 | assert(iterator->component->class); |
| 194 | |
| 195 | switch (iterator->component->class->type) { |
| 196 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 197 | { |
| 198 | struct bt_component_class_source *source_class = |
| 199 | container_of(iterator->component->class, |
| 200 | struct bt_component_class_source, parent); |
| 201 | |
| 202 | assert(source_class->methods.iterator.next); |
| 203 | next_method = source_class->methods.iterator.next; |
| 204 | break; |
| 205 | } |
| 206 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 207 | { |
| 208 | struct bt_component_class_filter *filter_class = |
| 209 | container_of(iterator->component->class, |
| 210 | struct bt_component_class_filter, parent); |
| 211 | |
| 212 | assert(filter_class->methods.iterator.next); |
| 213 | next_method = filter_class->methods.iterator.next; |
| 214 | break; |
| 215 | } |
| 216 | default: |
| 217 | assert(false); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | assert(next_method); |
| 222 | next_return = next_method(priv_iterator); |
| 223 | if (next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 224 | if (!next_return.notification) { |
| 225 | next_return.status = |
| 226 | BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 227 | goto end; |
| 228 | } |
| 229 | |
| 230 | BT_MOVE(iterator->current_notification, |
| 231 | next_return.notification); |
| 232 | bt_notification_freeze(iterator->current_notification); |
| 233 | } |
| 234 | |
| 235 | end: |
| 236 | return next_return.status; |
| 237 | } |
| 238 | |
| 239 | struct bt_component *bt_notification_iterator_get_component( |
| 240 | struct bt_notification_iterator *iterator) |
| 241 | { |
| 242 | return bt_get(iterator->component); |
| 243 | } |
| 244 | |
| 245 | struct bt_private_component * |
| 246 | bt_private_notification_iterator_get_private_component( |
| 247 | struct bt_private_notification_iterator *private_iterator) |
| 248 | { |
| 249 | return bt_private_component_from_component( |
| 250 | bt_notification_iterator_get_component( |
| 251 | bt_notification_iterator_from_private(private_iterator))); |
| 252 | } |
| 253 | |
| 254 | enum bt_notification_iterator_status bt_notification_iterator_seek_time( |
| 255 | struct bt_notification_iterator *iterator, |
| 256 | enum bt_notification_iterator_seek_origin seek_origin, |
| 257 | int64_t time) |
| 258 | { |
| 259 | enum bt_notification_iterator_status ret = |
| 260 | BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED; |
| 261 | return ret; |
| 262 | } |