| 1 | /* |
| 2 | * BabelTrace |
| 3 | * |
| 4 | * Format Registry |
| 5 | * |
| 6 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <glib.h> |
| 24 | #include <errno.h> |
| 25 | |
| 26 | static int init_done; |
| 27 | void __attribute__((constructor)) format_init(void); |
| 28 | void __attribute__((destructor)) format_finalize(void); |
| 29 | |
| 30 | /* |
| 31 | * Format registry hash table contains the registered formats. Format |
| 32 | * registration is typically performed by a format plugin. |
| 33 | * TODO: support plugin unload (unregistration of formats). |
| 34 | */ |
| 35 | GHashTable *format_registry; |
| 36 | |
| 37 | struct format *bt_lookup_format(GQuark qname) |
| 38 | { |
| 39 | if (!init_done) |
| 40 | return NULL; |
| 41 | return g_hash_table_lookup(format_registry, |
| 42 | (gconstpointer) (unsigned long) qname) |
| 43 | } |
| 44 | |
| 45 | int bt_register_format(const struct format *format) |
| 46 | { |
| 47 | if (!init_done) |
| 48 | format_init(); |
| 49 | |
| 50 | if (bt_lookup_format(qname)) |
| 51 | return -EEXIST; |
| 52 | |
| 53 | g_hash_table_insert(format_registry, |
| 54 | (gconstpointer) (unsigned long) format->name, |
| 55 | format); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void format_init(void) |
| 60 | { |
| 61 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 62 | assert(format_registry); |
| 63 | init_done = 1; |
| 64 | } |
| 65 | |
| 66 | int format_finalize(void) |
| 67 | { |
| 68 | g_hash_table_destroy(format_registry); |
| 69 | } |