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 | static int init_done; |
33 | void __attribute__((constructor)) format_init(void); | |
34 | void __attribute__((destructor)) format_finalize(void); | |
35 | ||
36 | /* | |
37 | * Format registry hash table contains the registered formats. Format | |
38 | * registration is typically performed by a format plugin. | |
39 | * TODO: support plugin unload (unregistration of formats). | |
40 | */ | |
41 | GHashTable *format_registry; | |
42 | ||
34861b9d | 43 | struct format *bt_lookup_format(bt_intern_str name) |
fc93b2bd MD |
44 | { |
45 | if (!init_done) | |
46 | return NULL; | |
7f89ddce | 47 | |
fc93b2bd | 48 | return g_hash_table_lookup(format_registry, |
34861b9d | 49 | (gconstpointer) (unsigned long) name); |
fc93b2bd MD |
50 | } |
51 | ||
7fb21036 MD |
52 | static void show_format(gpointer key, gpointer value, gpointer user_data) |
53 | { | |
d2b8ea6b | 54 | struct walk_data *data = user_data; |
7fb21036 | 55 | |
d2b8ea6b | 56 | fprintf(data->fp, "%s%s", data->iter ? ", " : "", |
7fb21036 | 57 | g_quark_to_string((GQuark) (unsigned long) key)); |
d2b8ea6b | 58 | data->iter++; |
7fb21036 MD |
59 | } |
60 | ||
61 | void bt_fprintf_format_list(FILE *fp) | |
62 | { | |
d2b8ea6b MD |
63 | struct walk_data data; |
64 | ||
7f89ddce MD |
65 | assert(fp); |
66 | ||
d2b8ea6b MD |
67 | data.fp = fp; |
68 | data.iter = 0; | |
69 | ||
70 | fprintf(fp, "Formats available: "); | |
7fb21036 MD |
71 | if (!init_done) |
72 | return; | |
d2b8ea6b MD |
73 | g_hash_table_foreach(format_registry, show_format, &data); |
74 | if (data.iter == 0) | |
75 | fprintf(fp, "<none>"); | |
76 | fprintf(fp, ".\n"); | |
7fb21036 MD |
77 | } |
78 | ||
4c8bfb7e | 79 | int bt_register_format(struct format *format) |
fc93b2bd | 80 | { |
7f89ddce MD |
81 | if (!format) |
82 | return -EINVAL; | |
83 | ||
fc93b2bd MD |
84 | if (!init_done) |
85 | format_init(); | |
86 | ||
4c8bfb7e | 87 | if (bt_lookup_format(format->name)) |
fc93b2bd MD |
88 | return -EEXIST; |
89 | ||
90 | g_hash_table_insert(format_registry, | |
4c8bfb7e | 91 | (gpointer) (unsigned long) format->name, |
fc93b2bd MD |
92 | format); |
93 | return 0; | |
94 | } | |
95 | ||
96 | void format_init(void) | |
97 | { | |
0860ab48 MD |
98 | if (init_done) |
99 | return; | |
fc93b2bd MD |
100 | format_registry = g_hash_table_new(g_direct_hash, g_direct_equal); |
101 | assert(format_registry); | |
102 | init_done = 1; | |
103 | } | |
104 | ||
4c8bfb7e | 105 | void format_finalize(void) |
fc93b2bd MD |
106 | { |
107 | g_hash_table_destroy(format_registry); | |
108 | } |