Commit | Line | Data |
---|---|---|
de713ce0 JG |
1 | /* |
2 | * component.c | |
3 | * | |
4 | * Babeltrace Plugin Component | |
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 | ||
47e5a032 | 29 | #include <babeltrace/plugin/component.h> |
de713ce0 JG |
30 | #include <babeltrace/plugin/component-internal.h> |
31 | #include <babeltrace/babeltrace-internal.h> | |
32 | #include <babeltrace/compiler.h> | |
33 | ||
34 | static void bt_component_destroy(struct bt_ctf_ref *ref); | |
35 | ||
36 | const char *bt_component_get_name(struct bt_component *component) | |
37 | { | |
38 | const char *ret = NULL; | |
39 | ||
40 | if (!component) { | |
41 | goto end; | |
42 | } | |
43 | ||
44 | ret = component->name->str; | |
45 | end: | |
46 | return ret; | |
47 | } | |
48 | ||
49 | enum bt_component_status bt_component_set_name(struct bt_component *component, | |
50 | const char *name) | |
51 | { | |
52 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
53 | ||
54 | if (!component || !name || name[0] == '\0') { | |
55 | ret = BT_COMPONENT_STATUS_INVAL; | |
56 | goto end; | |
57 | } | |
58 | ||
59 | g_string_assign(component->name, name); | |
60 | end: | |
61 | return ret; | |
62 | } | |
63 | ||
64 | enum bt_component_type bt_component_get_type(struct bt_component *component) | |
65 | { | |
66 | enum bt_component_type type = BT_COMPONENT_TYPE_UNKNOWN; | |
67 | ||
68 | if (!component) { | |
69 | goto end; | |
70 | } | |
71 | ||
72 | type = component->type; | |
73 | end: | |
74 | return type; | |
75 | } | |
76 | ||
77 | enum bt_component_status bt_component_set_error_stream( | |
78 | struct bt_component *component, FILE *stream) | |
79 | { | |
80 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
81 | ||
82 | if (!component) { | |
83 | ret = BT_COMPONENT_STATUS_INVAL; | |
84 | goto end; | |
85 | } | |
86 | ||
87 | component->error_stream = stream; | |
88 | end: | |
89 | return ret; | |
90 | } | |
91 | ||
92 | void bt_component_get(struct bt_component *component) | |
93 | { | |
94 | if (!component) { | |
95 | return; | |
96 | } | |
97 | ||
98 | bt_ctf_ref_get(&component->ref_count); | |
99 | } | |
100 | ||
101 | void bt_component_put(struct bt_component *component) | |
102 | { | |
103 | if (!component) { | |
104 | return; | |
105 | } | |
106 | ||
107 | bt_ctf_ref_put(&component->ref_count, bt_component_destroy); | |
108 | } | |
109 | ||
110 | BT_HIDDEN | |
0d884c50 JG |
111 | enum bt_component_status bt_component_init(struct bt_component *component, |
112 | const char *name, void *user_data, | |
113 | bt_component_destroy_cb user_destroy_func, | |
de713ce0 JG |
114 | enum bt_component_type component_type, |
115 | bt_component_destroy_cb component_destroy) | |
116 | { | |
117 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
118 | ||
119 | if (!component || !name || name[0] == '\0' || | |
120 | !user_destroy_func || !user_data || !component_destroy) { | |
121 | ret = BT_COMPONENT_STATUS_INVAL; | |
122 | goto end; | |
123 | } | |
124 | ||
125 | bt_ctf_ref_init(&component->ref_count); | |
126 | component->type = component_type; | |
127 | component->user_data = user_data; | |
128 | component->user_data_destroy = user_destroy_func; | |
129 | component->destroy = component_destroy; | |
130 | ||
131 | component->name = g_string_new(name); | |
132 | if (!component->name) { | |
133 | ret = BT_COMPONENT_STATUS_NOMEM; | |
134 | goto end; | |
135 | } | |
136 | end: | |
137 | return ret; | |
138 | } | |
139 | ||
140 | void *bt_component_get_private_data(struct bt_component *component) | |
141 | { | |
0d884c50 | 142 | void *ret = NULL; |
de713ce0 JG |
143 | |
144 | if (!component) { | |
145 | goto end; | |
146 | } | |
147 | ||
148 | ret = component->user_data; | |
149 | end: | |
150 | return ret; | |
151 | } | |
152 | ||
153 | enum bt_component_status | |
154 | bt_component_set_private_data(struct bt_component *component, | |
155 | void *data) | |
156 | { | |
157 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
158 | ||
159 | if (!component) { | |
160 | ret = BT_COMPONENT_STATUS_INVAL; | |
161 | goto end; | |
162 | } | |
163 | ||
164 | component->user_data = data; | |
165 | end: | |
166 | return ret; | |
167 | } | |
168 | ||
169 | static | |
170 | void bt_component_destroy(struct bt_ctf_ref *ref) | |
171 | { | |
172 | struct bt_component *component = NULL; | |
173 | ||
174 | if (!ref) { | |
175 | return; | |
176 | } | |
177 | ||
178 | component = container_of(ref, struct bt_component, ref_count); | |
179 | ||
180 | /** | |
181 | * User data is destroyed first, followed by the concrete component | |
182 | * instance. | |
183 | */ | |
184 | assert(!component->user_data || component->user_data_destroy); | |
185 | component->user_data_destroy(component->user_data); | |
186 | ||
187 | g_string_free(component->name, TRUE); | |
188 | ||
189 | assert(component->destroy); | |
190 | component->destroy(component); | |
191 | } |