Commit | Line | Data |
---|---|---|
fc93b2bd MD |
1 | /* |
2 | * BabelTrace | |
3 | * | |
4 | * Format Registry | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
fc93b2bd | 9 | * |
ccd7e1c8 MD |
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: | |
fc93b2bd | 16 | * |
ccd7e1c8 MD |
17 | * The above copyright notice and this permission notice shall be included in |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
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. | |
fc93b2bd MD |
27 | */ |
28 | ||
4c8bfb7e | 29 | #include <babeltrace/format.h> |
fc93b2bd MD |
30 | #include <glib.h> |
31 | #include <errno.h> | |
7fb21036 | 32 | #include <stdio.h> |
3122e6f0 | 33 | #include <assert.h> |
fc93b2bd | 34 | |
d2b8ea6b MD |
35 | struct walk_data { |
36 | FILE *fp; | |
37 | int iter; | |
38 | }; | |
39 | ||
fc93b2bd MD |
40 | /* |
41 | * Format registry hash table contains the registered formats. Format | |
42 | * registration is typically performed by a format plugin. | |
fc93b2bd | 43 | */ |
95febab3 MD |
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 | } | |
fc93b2bd | 69 | |
37b99bdb | 70 | struct bt_format *bt_lookup_format(bt_intern_str name) |
fc93b2bd MD |
71 | { |
72 | if (!init_done) | |
73 | return NULL; | |
7f89ddce | 74 | |
fc93b2bd | 75 | return g_hash_table_lookup(format_registry, |
34861b9d | 76 | (gconstpointer) (unsigned long) name); |
fc93b2bd MD |
77 | } |
78 | ||
7fb21036 MD |
79 | static void show_format(gpointer key, gpointer value, gpointer user_data) |
80 | { | |
d2b8ea6b | 81 | struct walk_data *data = user_data; |
7fb21036 | 82 | |
d2b8ea6b | 83 | fprintf(data->fp, "%s%s", data->iter ? ", " : "", |
7fb21036 | 84 | g_quark_to_string((GQuark) (unsigned long) key)); |
d2b8ea6b | 85 | data->iter++; |
7fb21036 MD |
86 | } |
87 | ||
88 | void bt_fprintf_format_list(FILE *fp) | |
89 | { | |
d2b8ea6b MD |
90 | struct walk_data data; |
91 | ||
7f89ddce MD |
92 | assert(fp); |
93 | ||
d2b8ea6b MD |
94 | data.fp = fp; |
95 | data.iter = 0; | |
96 | ||
97 | fprintf(fp, "Formats available: "); | |
7fb21036 MD |
98 | if (!init_done) |
99 | return; | |
d2b8ea6b MD |
100 | g_hash_table_foreach(format_registry, show_format, &data); |
101 | if (data.iter == 0) | |
102 | fprintf(fp, "<none>"); | |
103 | fprintf(fp, ".\n"); | |
7fb21036 MD |
104 | } |
105 | ||
37b99bdb | 106 | int bt_register_format(struct bt_format *format) |
fc93b2bd | 107 | { |
7f89ddce MD |
108 | if (!format) |
109 | return -EINVAL; | |
110 | ||
fc93b2bd MD |
111 | if (!init_done) |
112 | format_init(); | |
113 | ||
4c8bfb7e | 114 | if (bt_lookup_format(format->name)) |
fc93b2bd MD |
115 | return -EEXIST; |
116 | ||
95febab3 | 117 | format_refcount_inc(); |
fc93b2bd | 118 | g_hash_table_insert(format_registry, |
4c8bfb7e | 119 | (gpointer) (unsigned long) format->name, |
fc93b2bd MD |
120 | format); |
121 | return 0; | |
122 | } | |
123 | ||
37b99bdb | 124 | void bt_unregister_format(struct bt_format *format) |
95febab3 MD |
125 | { |
126 | assert(bt_lookup_format(format->name)); | |
127 | g_hash_table_remove(format_registry, | |
128 | (gpointer) (unsigned long) 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)) | |
fc93b2bd MD |
140 | void format_init(void) |
141 | { | |
0860ab48 MD |
142 | if (init_done) |
143 | return; | |
95febab3 | 144 | format_refcount_inc(); |
fc93b2bd MD |
145 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
146 | assert(format_registry); | |
147 | init_done = 1; | |
148 | } | |
149 | ||
95febab3 | 150 | static __attribute__((destructor)) |
4c8bfb7e | 151 | void format_finalize(void) |
fc93b2bd | 152 | { |
95febab3 | 153 | format_refcount_dec(); |
fc93b2bd | 154 | } |