| 1 | /* |
| 2 | * BabelTrace |
| 3 | * |
| 4 | * Format Registry |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@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 | |
| 29 | #include <babeltrace/format.h> |
| 30 | #include <glib.h> |
| 31 | #include <errno.h> |
| 32 | #include <stdio.h> |
| 33 | #include <assert.h> |
| 34 | |
| 35 | struct walk_data { |
| 36 | FILE *fp; |
| 37 | int iter; |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Format registry hash table contains the registered formats. Format |
| 42 | * registration is typically performed by a format plugin. |
| 43 | */ |
| 44 | static GHashTable *format_registry; |
| 45 | static int format_refcount; |
| 46 | static int init_done; |
| 47 | |
| 48 | static __attribute__((constructor)) void format_init(void); |
| 49 | |
| 50 | static |
| 51 | void format_refcount_inc(void) |
| 52 | { |
| 53 | format_refcount++; |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | void format_cleanup(void) |
| 58 | { |
| 59 | if (format_registry) |
| 60 | g_hash_table_destroy(format_registry); |
| 61 | } |
| 62 | |
| 63 | static |
| 64 | void format_refcount_dec(void) |
| 65 | { |
| 66 | if (!--format_refcount) |
| 67 | format_cleanup(); |
| 68 | } |
| 69 | |
| 70 | struct bt_format *bt_lookup_format(bt_intern_str name) |
| 71 | { |
| 72 | if (!init_done) |
| 73 | return NULL; |
| 74 | |
| 75 | return g_hash_table_lookup(format_registry, |
| 76 | (gconstpointer) GUINT_TO_POINTER(name)); |
| 77 | } |
| 78 | |
| 79 | static void show_format(gpointer key, gpointer value, gpointer user_data) |
| 80 | { |
| 81 | struct walk_data *data = user_data; |
| 82 | |
| 83 | fprintf(data->fp, "%s%s", data->iter ? ", " : "", |
| 84 | g_quark_to_string((GQuark) GPOINTER_TO_UINT(key))); |
| 85 | data->iter++; |
| 86 | } |
| 87 | |
| 88 | void bt_fprintf_format_list(FILE *fp) |
| 89 | { |
| 90 | struct walk_data data; |
| 91 | |
| 92 | assert(fp); |
| 93 | |
| 94 | data.fp = fp; |
| 95 | data.iter = 0; |
| 96 | |
| 97 | fprintf(fp, "Formats available: "); |
| 98 | if (!init_done) |
| 99 | return; |
| 100 | g_hash_table_foreach(format_registry, show_format, &data); |
| 101 | if (data.iter == 0) |
| 102 | fprintf(fp, "<none>"); |
| 103 | fprintf(fp, ".\n"); |
| 104 | } |
| 105 | |
| 106 | int bt_register_format(struct bt_format *format) |
| 107 | { |
| 108 | if (!format) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | if (!init_done) |
| 112 | format_init(); |
| 113 | |
| 114 | if (bt_lookup_format(format->name)) |
| 115 | return -EEXIST; |
| 116 | |
| 117 | format_refcount_inc(); |
| 118 | g_hash_table_insert(format_registry, |
| 119 | GUINT_TO_POINTER(format->name), |
| 120 | format); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | void bt_unregister_format(struct bt_format *format) |
| 125 | { |
| 126 | assert(bt_lookup_format(format->name)); |
| 127 | g_hash_table_remove(format_registry, |
| 128 | GUINT_TO_POINTER(format->name)); |
| 129 | format_refcount_dec(); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * We cannot assume that the constructor and destructor order will be |
| 134 | * right: another library might be loaded before us, and initialize us |
| 135 | * from bt_register_format(). This is why we use a reference count to |
| 136 | * handle cleanup of this module. The format_finalize destructor |
| 137 | * refcount decrement matches format_init refcount increment. |
| 138 | */ |
| 139 | static __attribute__((constructor)) |
| 140 | void format_init(void) |
| 141 | { |
| 142 | if (init_done) |
| 143 | return; |
| 144 | format_refcount_inc(); |
| 145 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
| 146 | assert(format_registry); |
| 147 | init_done = 1; |
| 148 | } |
| 149 | |
| 150 | static __attribute__((destructor)) |
| 151 | void format_finalize(void) |
| 152 | { |
| 153 | format_refcount_dec(); |
| 154 | } |