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.
21 #include <babeltrace/format.h>
33 * Format registry hash table contains the registered formats. Format
34 * registration is typically performed by a format plugin.
36 static GHashTable
*format_registry
;
37 static int format_refcount
;
40 static __attribute__((constructor
)) void format_init(void);
43 void format_refcount_inc(void)
49 void format_cleanup(void)
52 g_hash_table_destroy(format_registry
);
56 void format_refcount_dec(void)
58 if (!--format_refcount
)
62 struct format
*bt_lookup_format(bt_intern_str name
)
67 return g_hash_table_lookup(format_registry
,
68 (gconstpointer
) (unsigned long) name
);
71 static void show_format(gpointer key
, gpointer value
, gpointer user_data
)
73 struct walk_data
*data
= user_data
;
75 fprintf(data
->fp
, "%s%s", data
->iter
? ", " : "",
76 g_quark_to_string((GQuark
) (unsigned long) key
));
80 void bt_fprintf_format_list(FILE *fp
)
82 struct walk_data data
;
89 fprintf(fp
, "Formats available: ");
92 g_hash_table_foreach(format_registry
, show_format
, &data
);
94 fprintf(fp
, "<none>");
98 int bt_register_format(struct format
*format
)
106 if (bt_lookup_format(format
->name
))
109 format_refcount_inc();
110 g_hash_table_insert(format_registry
,
111 (gpointer
) (unsigned long) format
->name
,
116 void bt_unregister_format(struct format
*format
)
118 assert(bt_lookup_format(format
->name
));
119 g_hash_table_remove(format_registry
,
120 (gpointer
) (unsigned long) format
->name
);
121 format_refcount_dec();
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.
131 static __attribute__((constructor
))
132 void format_init(void)
136 format_refcount_inc();
137 format_registry
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
138 assert(format_registry
);
142 static __attribute__((destructor
))
143 void format_finalize(void)
145 format_refcount_dec();
This page took 0.032704 seconds and 4 git commands to generate.