Commit | Line | Data |
---|---|---|
fc93b2bd MD |
1 | /* |
2 | * BabelTrace | |
3 | * | |
4 | * Format Registry | |
5 | * | |
ccd7e1c8 | 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fc93b2bd | 7 | * |
ccd7e1c8 MD |
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: | |
fc93b2bd | 14 | * |
ccd7e1c8 MD |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. | |
fc93b2bd MD |
17 | */ |
18 | ||
19 | #include <glib.h> | |
20 | #include <errno.h> | |
21 | ||
22 | static int init_done; | |
23 | void __attribute__((constructor)) format_init(void); | |
24 | void __attribute__((destructor)) format_finalize(void); | |
25 | ||
26 | /* | |
27 | * Format registry hash table contains the registered formats. Format | |
28 | * registration is typically performed by a format plugin. | |
29 | * TODO: support plugin unload (unregistration of formats). | |
30 | */ | |
31 | GHashTable *format_registry; | |
32 | ||
33 | struct format *bt_lookup_format(GQuark qname) | |
34 | { | |
35 | if (!init_done) | |
36 | return NULL; | |
37 | return g_hash_table_lookup(format_registry, | |
38 | (gconstpointer) (unsigned long) qname) | |
39 | } | |
40 | ||
41 | int bt_register_format(const struct format *format) | |
42 | { | |
43 | if (!init_done) | |
44 | format_init(); | |
45 | ||
46 | if (bt_lookup_format(qname)) | |
47 | return -EEXIST; | |
48 | ||
49 | g_hash_table_insert(format_registry, | |
50 | (gconstpointer) (unsigned long) format->name, | |
51 | format); | |
52 | return 0; | |
53 | } | |
54 | ||
55 | void format_init(void) | |
56 | { | |
57 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); | |
58 | assert(format_registry); | |
59 | init_done = 1; | |
60 | } | |
61 | ||
62 | int format_finalize(void) | |
63 | { | |
64 | g_hash_table_destroy(format_registry); | |
65 | } |