lib: make graph API const-correct
[babeltrace.git] / include / babeltrace / graph / graph-internal.h
1 #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
2 #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
3
4 /*
5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <babeltrace/graph/graph.h>
29 #include <babeltrace/graph/connection-internal.h>
30 #include <babeltrace/graph/notification-const.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/object-pool-internal.h>
34 #include <babeltrace/assert-internal.h>
35 #include <stdlib.h>
36 #include <glib.h>
37
38 struct bt_component;
39 struct bt_port;
40
41 struct bt_graph {
42 /**
43 * A component graph contains components and point-to-point connection
44 * between these components.
45 *
46 * In terms of ownership:
47 * 1) The graph is the components' parent,
48 * 2) The graph is the connnections' parent,
49 * 3) Components share the ownership of their connections,
50 * 4) A connection holds weak references to its two component endpoints.
51 */
52 struct bt_object base;
53
54 /* Array of pointers to bt_connection. */
55 GPtrArray *connections;
56 /* Array of pointers to bt_component. */
57 GPtrArray *components;
58 /* Queue of pointers (weak references) to sink bt_components. */
59 GQueue *sinks_to_consume;
60
61 bool canceled;
62 bool in_remove_listener;
63 bool has_sink;
64
65 /*
66 * If this is false, then the public API's consuming
67 * functions (bt_graph_consume() and bt_graph_run()) return
68 * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check"
69 * functions always work.
70 *
71 * In bt_port_output_notification_iterator_create(), on success,
72 * this flag is cleared so that the iterator remains the only
73 * consumer for the graph's lifetime.
74 */
75 bool can_consume;
76
77 struct {
78 GArray *source_output_port_added;
79 GArray *filter_output_port_added;
80 GArray *filter_input_port_added;
81 GArray *sink_input_port_added;
82 GArray *source_output_port_removed;
83 GArray *filter_output_port_removed;
84 GArray *filter_input_port_removed;
85 GArray *sink_input_port_removed;
86 GArray *source_filter_ports_connected;
87 GArray *source_sink_ports_connected;
88 GArray *filter_sink_ports_connected;
89 GArray *source_filter_ports_disconnected;
90 GArray *source_sink_ports_disconnected;
91 GArray *filter_sink_ports_disconnected;
92 } listeners;
93
94 /* Pool of `struct bt_notification_event *` */
95 struct bt_object_pool event_notif_pool;
96
97 /* Pool of `struct bt_notification_packet_begin *` */
98 struct bt_object_pool packet_begin_notif_pool;
99
100 /* Pool of `struct bt_notification_packet_end *` */
101 struct bt_object_pool packet_end_notif_pool;
102
103 /*
104 * Array of `struct bt_notification *` (weak).
105 *
106 * This is an array of all the notifications ever created from
107 * this graph. Some of them can be in one of the pools above,
108 * some of them can be at large. Because each notification has a
109 * weak pointer to the graph containing its pool, we need to
110 * notify each notification that the graph is gone on graph
111 * destruction.
112 *
113 * TODO: When we support a maximum size for object pools,
114 * add a way for a notification to remove itself from this
115 * array (on destruction).
116 */
117 GPtrArray *notifications;
118 };
119
120 static inline
121 void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
122 {
123 BT_ASSERT(graph);
124 graph->can_consume = can_consume;
125 }
126
127 #ifdef BT_DEV_MODE
128 # define bt_graph_set_can_consume _bt_graph_set_can_consume
129 #else
130 # define bt_graph_set_can_consume(_graph, _can_consume)
131 #endif
132
133 BT_HIDDEN
134 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
135 struct bt_component_sink *sink);
136
137 BT_HIDDEN
138 void bt_graph_notify_port_added(struct bt_graph *graph, struct bt_port *port);
139
140 BT_HIDDEN
141 void bt_graph_notify_port_removed(struct bt_graph *graph,
142 struct bt_component *comp, struct bt_port *port);
143
144 BT_HIDDEN
145 void bt_graph_notify_ports_connected(struct bt_graph *graph,
146 struct bt_port *upstream_port, struct bt_port *downstream_port);
147
148 BT_HIDDEN
149 void bt_graph_notify_ports_disconnected(struct bt_graph *graph,
150 struct bt_component *upstream_comp,
151 struct bt_component *downstream_comp,
152 struct bt_port *upstream_port,
153 struct bt_port *downstream_port);
154
155 BT_HIDDEN
156 void bt_graph_remove_connection(struct bt_graph *graph,
157 struct bt_connection *connection);
158
159 /*
160 * This only works with a component which is not connected at this
161 * point.
162 *
163 * Also the reference count of `component` should be 0 when you call
164 * this function, which means only `graph` owns the component, so it
165 * is safe to destroy.
166 */
167 BT_HIDDEN
168 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
169 struct bt_component *component);
170
171 BT_HIDDEN
172 void bt_graph_add_notification(struct bt_graph *graph,
173 struct bt_notification *notif);
174
175 static inline
176 const char *bt_graph_status_string(enum bt_graph_status status)
177 {
178 switch (status) {
179 case BT_GRAPH_STATUS_CANCELED:
180 return "BT_GRAPH_STATUS_CANCELED";
181 case BT_GRAPH_STATUS_AGAIN:
182 return "BT_GRAPH_STATUS_AGAIN";
183 case BT_GRAPH_STATUS_END:
184 return "BT_GRAPH_STATUS_END";
185 case BT_GRAPH_STATUS_OK:
186 return "BT_GRAPH_STATUS_OK";
187 case BT_GRAPH_STATUS_NO_SINK:
188 return "BT_GRAPH_STATUS_NO_SINK";
189 case BT_GRAPH_STATUS_ERROR:
190 return "BT_GRAPH_STATUS_ERROR";
191 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
192 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
193 case BT_GRAPH_STATUS_NOMEM:
194 return "BT_GRAPH_STATUS_NOMEM";
195 default:
196 return "(unknown)";
197 }
198 }
199
200 #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.033173 seconds and 4 git commands to generate.