6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include <babeltrace/format.h>
41 * Format registry hash table contains the registered formats. Format
42 * registration is typically performed by a format plugin.
44 static GHashTable
*format_registry
;
45 static int format_refcount
;
48 static __attribute__((constructor
)) void format_init(void);
51 void format_refcount_inc(void)
57 void format_cleanup(void)
60 g_hash_table_destroy(format_registry
);
64 void format_refcount_dec(void)
66 if (!--format_refcount
)
70 struct format
*bt_lookup_format(bt_intern_str name
)
75 return g_hash_table_lookup(format_registry
,
76 (gconstpointer
) (unsigned long) name
);
79 static void show_format(gpointer key
, gpointer value
, gpointer user_data
)
81 struct walk_data
*data
= user_data
;
83 fprintf(data
->fp
, "%s%s", data
->iter
? ", " : "",
84 g_quark_to_string((GQuark
) (unsigned long) key
));
88 void bt_fprintf_format_list(FILE *fp
)
90 struct walk_data data
;
97 fprintf(fp
, "Formats available: ");
100 g_hash_table_foreach(format_registry
, show_format
, &data
);
102 fprintf(fp
, "<none>");
106 int bt_register_format(struct format
*format
)
114 if (bt_lookup_format(format
->name
))
117 format_refcount_inc();
118 g_hash_table_insert(format_registry
,
119 (gpointer
) (unsigned long) format
->name
,
124 void bt_unregister_format(struct format
*format
)
126 assert(bt_lookup_format(format
->name
));
127 g_hash_table_remove(format_registry
,
128 (gpointer
) (unsigned long) format
->name
);
129 format_refcount_dec();
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.
139 static __attribute__((constructor
))
140 void format_init(void)
144 format_refcount_inc();
145 format_registry
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
146 assert(format_registry
);
150 static __attribute__((destructor
))
151 void format_finalize(void)
153 format_refcount_dec();
This page took 0.053929 seconds and 4 git commands to generate.