Check for NULL user destroy in iterator
[babeltrace.git] / lib / plugin-system / iterator.c
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.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/plugin/component.h>
32 #include <babeltrace/plugin/source-internal.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/plugin/notification/iterator-internal.h>
35
36 static
37 void bt_notification_iterator_destroy(struct bt_object *obj)
38 {
39 struct bt_notification_iterator *iterator;
40
41 assert(obj);
42 iterator = container_of(obj, struct bt_notification_iterator,
43 base);
44 assert(iterator->user_destroy || !iterator->user_data);
45 if (iterator->user_destroy) {
46 iterator->user_destroy(iterator);
47 }
48 g_free(iterator);
49 }
50
51 BT_HIDDEN
52 struct bt_notification_iterator *bt_notification_iterator_create(
53 struct bt_component *component)
54 {
55 struct bt_notification_iterator *iterator = NULL;
56
57 if (!component ||
58 bt_component_get_type(component) != BT_COMPONENT_TYPE_SOURCE) {
59 goto end;
60 }
61
62 iterator = g_new0(struct bt_notification_iterator, 1);
63 if (!iterator) {
64 goto end;
65 }
66
67 bt_object_init(iterator, bt_notification_iterator_destroy);
68 end:
69 return iterator;
70 }
71
72 BT_HIDDEN
73 enum bt_notification_iterator_status bt_notification_iterator_validate(
74 struct bt_notification_iterator *iterator)
75 {
76 enum bt_notification_iterator_status ret =
77 BT_NOTIFICATION_ITERATOR_STATUS_OK;
78
79 if (!iterator || !iterator->get || !iterator->next) {
80 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
81 goto end;
82 }
83 end:
84 return ret;
85 }
86
87 enum bt_notification_iterator_status bt_notification_iterator_set_get_cb(
88 struct bt_notification_iterator *iterator,
89 bt_notification_iterator_get_cb get)
90 {
91 enum bt_notification_iterator_status ret =
92 BT_NOTIFICATION_ITERATOR_STATUS_OK;
93
94 if (!iterator || !get) {
95 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
96 goto end;
97 }
98
99 iterator->get = get;
100 end:
101 return ret;
102 }
103
104 enum bt_notification_iterator_status
105 bt_notification_iterator_set_next_cb(struct bt_notification_iterator *iterator,
106 bt_notification_iterator_next_cb next)
107 {
108 enum bt_notification_iterator_status ret =
109 BT_NOTIFICATION_ITERATOR_STATUS_OK;
110
111 if (!iterator || !next) {
112 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
113 goto end;
114 }
115
116 iterator->next = next;
117 end:
118 return ret;
119 }
120
121 enum bt_notification_iterator_status
122 bt_notification_iterator_set_destroy_cb(
123 struct bt_notification_iterator *iterator,
124 bt_notification_iterator_destroy_cb destroy)
125 {
126 enum bt_notification_iterator_status ret =
127 BT_NOTIFICATION_ITERATOR_STATUS_OK;
128
129 if (!iterator || !destroy) {
130 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
131 goto end;
132 }
133
134 iterator->user_destroy = destroy;
135 end:
136 return ret;
137 }
138
139 void *bt_notification_iterator_get_private_data(
140 struct bt_notification_iterator *iterator)
141 {
142 return iterator ? iterator->user_data : NULL;
143 }
144
145 enum bt_notification_iterator_status
146 bt_notification_iterator_set_private_data(
147 struct bt_notification_iterator *iterator, void *data)
148 {
149 enum bt_notification_iterator_status ret =
150 BT_NOTIFICATION_ITERATOR_STATUS_OK;
151
152 if (!iterator || !data) {
153 ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL;
154 goto end;
155 }
156
157 iterator->user_data = data;
158 end:
159 return ret;
160 }
This page took 0.034014 seconds and 5 git commands to generate.