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