| 1 | /* |
| 2 | * BabelTrace |
| 3 | * |
| 4 | * Format Registry |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | */ |
| 18 | |
| 19 | #include <babeltrace/format.h> |
| 20 | #include <glib.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | static int init_done; |
| 24 | void __attribute__((constructor)) format_init(void); |
| 25 | void __attribute__((destructor)) format_finalize(void); |
| 26 | |
| 27 | /* |
| 28 | * Format registry hash table contains the registered formats. Format |
| 29 | * registration is typically performed by a format plugin. |
| 30 | * TODO: support plugin unload (unregistration of formats). |
| 31 | */ |
| 32 | GHashTable *format_registry; |
| 33 | |
| 34 | struct format *bt_lookup_format(GQuark qname) |
| 35 | { |
| 36 | if (!init_done) |
| 37 | return NULL; |
| 38 | return g_hash_table_lookup(format_registry, |
| 39 | (gconstpointer) (unsigned long) qname); |
| 40 | } |
| 41 | |
| 42 | int bt_register_format(struct format *format) |
| 43 | { |
| 44 | if (!init_done) |
| 45 | format_init(); |
| 46 | |
| 47 | if (bt_lookup_format(format->name)) |
| 48 | return -EEXIST; |
| 49 | |
| 50 | g_hash_table_insert(format_registry, |
| 51 | (gpointer) (unsigned long) format->name, |
| 52 | format); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void format_init(void) |
| 57 | { |
| 58 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 59 | assert(format_registry); |
| 60 | init_done = 1; |
| 61 | } |
| 62 | |
| 63 | void format_finalize(void) |
| 64 | { |
| 65 | g_hash_table_destroy(format_registry); |
| 66 | } |