4d09e5db079837e04df86450d3772399648e2d2a
[babeltrace.git] / lib / registry.c
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
21 #include <babeltrace/format.h>
22 #include <glib.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <assert.h>
26
27 struct walk_data {
28 FILE *fp;
29 int iter;
30 };
31
32 /*
33 * Format registry hash table contains the registered formats. Format
34 * registration is typically performed by a format plugin.
35 */
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 }
61
62 struct format *bt_lookup_format(bt_intern_str name)
63 {
64 if (!init_done)
65 return NULL;
66
67 return g_hash_table_lookup(format_registry,
68 (gconstpointer) (unsigned long) name);
69 }
70
71 static void show_format(gpointer key, gpointer value, gpointer user_data)
72 {
73 struct walk_data *data = user_data;
74
75 fprintf(data->fp, "%s%s", data->iter ? ", " : "",
76 g_quark_to_string((GQuark) (unsigned long) key));
77 data->iter++;
78 }
79
80 void bt_fprintf_format_list(FILE *fp)
81 {
82 struct walk_data data;
83
84 assert(fp);
85
86 data.fp = fp;
87 data.iter = 0;
88
89 fprintf(fp, "Formats available: ");
90 if (!init_done)
91 return;
92 g_hash_table_foreach(format_registry, show_format, &data);
93 if (data.iter == 0)
94 fprintf(fp, "<none>");
95 fprintf(fp, ".\n");
96 }
97
98 int bt_register_format(struct format *format)
99 {
100 if (!format)
101 return -EINVAL;
102
103 if (!init_done)
104 format_init();
105
106 if (bt_lookup_format(format->name))
107 return -EEXIST;
108
109 format_refcount_inc();
110 g_hash_table_insert(format_registry,
111 (gpointer) (unsigned long) format->name,
112 format);
113 return 0;
114 }
115
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))
132 void format_init(void)
133 {
134 if (init_done)
135 return;
136 format_refcount_inc();
137 format_registry = g_hash_table_new(g_direct_hash, g_direct_equal);
138 assert(format_registry);
139 init_done = 1;
140 }
141
142 static __attribute__((destructor))
143 void format_finalize(void)
144 {
145 format_refcount_dec();
146 }
This page took 0.046022 seconds and 3 git commands to generate.