| 1 | #ifndef BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H |
| 2 | #define BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * BabelTrace - Plug-in Internal |
| 6 | * |
| 7 | * Copyright 2015 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/babeltrace-internal.h> |
| 31 | #include <babeltrace/plugin/plugin-dev.h> |
| 32 | #include <babeltrace/object-internal.h> |
| 33 | #include <stdbool.h> |
| 34 | #include <glib.h> |
| 35 | |
| 36 | enum bt_plugin_type { |
| 37 | BT_PLUGIN_TYPE_SO = 0, |
| 38 | BT_PLUGIN_TYPE_PYTHON = 1, |
| 39 | }; |
| 40 | |
| 41 | struct bt_plugin { |
| 42 | struct bt_object base; |
| 43 | enum bt_plugin_type type; |
| 44 | bool frozen; |
| 45 | |
| 46 | /* Array of pointers to bt_component_class (owned by this) */ |
| 47 | GPtrArray *comp_classes; |
| 48 | |
| 49 | /* Info (owned by this) */ |
| 50 | struct { |
| 51 | GString *path; |
| 52 | GString *name; |
| 53 | GString *author; |
| 54 | GString *license; |
| 55 | GString *description; |
| 56 | struct { |
| 57 | unsigned int major; |
| 58 | unsigned int minor; |
| 59 | unsigned int patch; |
| 60 | GString *extra; |
| 61 | } version; |
| 62 | bool path_set; |
| 63 | bool name_set; |
| 64 | bool author_set; |
| 65 | bool license_set; |
| 66 | bool description_set; |
| 67 | bool version_set; |
| 68 | } info; |
| 69 | |
| 70 | /* Value depends on the specific plugin type */ |
| 71 | void *spec_data; |
| 72 | void (*destroy_spec_data)(struct bt_plugin *); |
| 73 | }; |
| 74 | |
| 75 | struct bt_plugin_set { |
| 76 | struct bt_object base; |
| 77 | |
| 78 | /* Array of struct bt_plugin * */ |
| 79 | GPtrArray *plugins; |
| 80 | }; |
| 81 | |
| 82 | static inline |
| 83 | void bt_plugin_destroy(struct bt_object *obj) |
| 84 | { |
| 85 | struct bt_plugin *plugin; |
| 86 | |
| 87 | assert(obj); |
| 88 | plugin = container_of(obj, struct bt_plugin, base); |
| 89 | |
| 90 | if (plugin->destroy_spec_data) { |
| 91 | plugin->destroy_spec_data(plugin); |
| 92 | } |
| 93 | |
| 94 | if (plugin->comp_classes) { |
| 95 | g_ptr_array_free(plugin->comp_classes, TRUE); |
| 96 | } |
| 97 | |
| 98 | if (plugin->info.name) { |
| 99 | g_string_free(plugin->info.name, TRUE); |
| 100 | } |
| 101 | |
| 102 | if (plugin->info.path) { |
| 103 | g_string_free(plugin->info.path, TRUE); |
| 104 | } |
| 105 | |
| 106 | if (plugin->info.description) { |
| 107 | g_string_free(plugin->info.description, TRUE); |
| 108 | } |
| 109 | |
| 110 | if (plugin->info.author) { |
| 111 | g_string_free(plugin->info.author, TRUE); |
| 112 | } |
| 113 | |
| 114 | if (plugin->info.license) { |
| 115 | g_string_free(plugin->info.license, TRUE); |
| 116 | } |
| 117 | |
| 118 | if (plugin->info.version.extra) { |
| 119 | g_string_free(plugin->info.version.extra, TRUE); |
| 120 | } |
| 121 | |
| 122 | g_free(plugin); |
| 123 | } |
| 124 | |
| 125 | static inline |
| 126 | struct bt_plugin *bt_plugin_create_empty(enum bt_plugin_type type) |
| 127 | { |
| 128 | struct bt_plugin *plugin = NULL; |
| 129 | |
| 130 | plugin = g_new0(struct bt_plugin, 1); |
| 131 | if (!plugin) { |
| 132 | goto error; |
| 133 | } |
| 134 | |
| 135 | bt_object_init(plugin, bt_plugin_destroy); |
| 136 | plugin->type = type; |
| 137 | |
| 138 | /* Create empty array of component classes */ |
| 139 | plugin->comp_classes = |
| 140 | g_ptr_array_new_with_free_func((GDestroyNotify) bt_put); |
| 141 | if (!plugin->comp_classes) { |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | /* Create empty info */ |
| 146 | plugin->info.name = g_string_new(NULL); |
| 147 | if (!plugin->info.name) { |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | plugin->info.path = g_string_new(NULL); |
| 152 | if (!plugin->info.path) { |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | plugin->info.description = g_string_new(NULL); |
| 157 | if (!plugin->info.description) { |
| 158 | goto error; |
| 159 | } |
| 160 | |
| 161 | plugin->info.author = g_string_new(NULL); |
| 162 | if (!plugin->info.author) { |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | plugin->info.license = g_string_new(NULL); |
| 167 | if (!plugin->info.license) { |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | plugin->info.version.extra = g_string_new(NULL); |
| 172 | if (!plugin->info.version.extra) { |
| 173 | goto error; |
| 174 | } |
| 175 | |
| 176 | goto end; |
| 177 | |
| 178 | error: |
| 179 | BT_PUT(plugin); |
| 180 | |
| 181 | end: |
| 182 | return plugin; |
| 183 | } |
| 184 | |
| 185 | static inline |
| 186 | void bt_plugin_set_path(struct bt_plugin *plugin, const char *path) |
| 187 | { |
| 188 | assert(plugin); |
| 189 | assert(path); |
| 190 | g_string_assign(plugin->info.path, path); |
| 191 | plugin->info.path_set = true; |
| 192 | } |
| 193 | |
| 194 | static inline |
| 195 | void bt_plugin_set_name(struct bt_plugin *plugin, const char *name) |
| 196 | { |
| 197 | assert(plugin); |
| 198 | assert(name); |
| 199 | g_string_assign(plugin->info.name, name); |
| 200 | plugin->info.name_set = true; |
| 201 | } |
| 202 | |
| 203 | static inline |
| 204 | void bt_plugin_set_description(struct bt_plugin *plugin, |
| 205 | const char *description) |
| 206 | { |
| 207 | assert(plugin); |
| 208 | assert(description); |
| 209 | g_string_assign(plugin->info.description, description); |
| 210 | plugin->info.description_set = true; |
| 211 | } |
| 212 | |
| 213 | static inline |
| 214 | void bt_plugin_set_author(struct bt_plugin *plugin, const char *author) |
| 215 | { |
| 216 | assert(plugin); |
| 217 | assert(author); |
| 218 | g_string_assign(plugin->info.author, author); |
| 219 | plugin->info.author_set = true; |
| 220 | } |
| 221 | |
| 222 | static inline |
| 223 | void bt_plugin_set_license(struct bt_plugin *plugin, const char *license) |
| 224 | { |
| 225 | assert(plugin); |
| 226 | assert(license); |
| 227 | g_string_assign(plugin->info.license, license); |
| 228 | plugin->info.license_set = true; |
| 229 | } |
| 230 | |
| 231 | static inline |
| 232 | void bt_plugin_set_version(struct bt_plugin *plugin, unsigned int major, |
| 233 | unsigned int minor, unsigned int patch, const char *extra) |
| 234 | { |
| 235 | assert(plugin); |
| 236 | plugin->info.version.major = major; |
| 237 | plugin->info.version.minor = minor; |
| 238 | plugin->info.version.patch = patch; |
| 239 | |
| 240 | if (extra) { |
| 241 | g_string_assign(plugin->info.version.extra, extra); |
| 242 | } |
| 243 | |
| 244 | plugin->info.version_set = true; |
| 245 | } |
| 246 | |
| 247 | static inline |
| 248 | void bt_plugin_freeze(struct bt_plugin *plugin) |
| 249 | { |
| 250 | assert(plugin); |
| 251 | plugin->frozen = true; |
| 252 | } |
| 253 | |
| 254 | static |
| 255 | void bt_plugin_set_destroy(struct bt_object *obj) |
| 256 | { |
| 257 | struct bt_plugin_set *plugin_set = |
| 258 | container_of(obj, struct bt_plugin_set, base); |
| 259 | |
| 260 | if (!plugin_set) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if (plugin_set->plugins) { |
| 265 | g_ptr_array_free(plugin_set->plugins, TRUE); |
| 266 | } |
| 267 | |
| 268 | g_free(plugin_set); |
| 269 | } |
| 270 | |
| 271 | static inline |
| 272 | struct bt_plugin_set *bt_plugin_set_create(void) |
| 273 | { |
| 274 | struct bt_plugin_set *plugin_set = g_new0(struct bt_plugin_set, 1); |
| 275 | |
| 276 | if (!plugin_set) { |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | bt_object_init(plugin_set, bt_plugin_set_destroy); |
| 281 | |
| 282 | plugin_set->plugins = g_ptr_array_new_with_free_func( |
| 283 | (GDestroyNotify) bt_put); |
| 284 | if (!plugin_set->plugins) { |
| 285 | BT_PUT(plugin_set); |
| 286 | goto end; |
| 287 | } |
| 288 | |
| 289 | end: |
| 290 | return plugin_set; |
| 291 | } |
| 292 | |
| 293 | static inline |
| 294 | void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set, |
| 295 | struct bt_plugin *plugin) |
| 296 | { |
| 297 | assert(plugin_set); |
| 298 | assert(plugin); |
| 299 | g_ptr_array_add(plugin_set->plugins, bt_get(plugin)); |
| 300 | } |
| 301 | |
| 302 | #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */ |