Commit | Line | Data |
---|---|---|
6dc2ca62 | 1 | /* |
ccd7e1c8 MD |
2 | * types.c |
3 | * | |
d79865b9 | 4 | * BabelTrace - Converter |
6dc2ca62 MD |
5 | * |
6 | * Types registry. | |
7 | * | |
ccd7e1c8 | 8 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
de0ba614 | 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: | |
de0ba614 | 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. | |
6dc2ca62 MD |
19 | */ |
20 | ||
4c8bfb7e | 21 | #include <babeltrace/format.h> |
6dc2ca62 | 22 | #include <glib.h> |
d1708134 MD |
23 | #include <errno.h> |
24 | ||
d1708134 | 25 | /* |
698f0fe4 MD |
26 | * Type hash table contains the registered types. Type registration is typically |
27 | * performed by a type plugin. | |
28 | * TODO: support plugin unload (unregistration of types). | |
d1708134 | 29 | */ |
4c8bfb7e | 30 | GHashTable *type_classes; |
d1708134 | 31 | |
4c8bfb7e | 32 | struct type_class *lookup_type(GQuark qname) |
d1708134 MD |
33 | { |
34 | return g_hash_table_lookup(type_classes, | |
4c8bfb7e | 35 | (gconstpointer) (unsigned long) qname); |
d1708134 MD |
36 | } |
37 | ||
90b676d7 MD |
38 | static void free_type(struct type_class *type_class) |
39 | { | |
40 | type_class->free(type_class); | |
41 | } | |
42 | ||
4c8bfb7e | 43 | int register_type(struct type_class *type_class) |
d1708134 | 44 | { |
be85c1c7 | 45 | if (lookup_type(type_class->name)) |
d1708134 MD |
46 | return -EEXIST; |
47 | ||
48 | g_hash_table_insert(type_classes, | |
4c8bfb7e | 49 | (gpointer) (unsigned long) type_class->name, |
698f0fe4 | 50 | type_class); |
d1708134 MD |
51 | return 0; |
52 | } | |
53 | ||
4c8bfb7e MD |
54 | void type_ref(struct type_class *type_class) |
55 | { | |
56 | type_class->ref++; | |
57 | } | |
58 | ||
59 | void type_unref(struct type_class *type_class) | |
60 | { | |
61 | if (!--type_class->ref) | |
62 | free_type(type_class); | |
63 | } | |
64 | ||
65 | int init_types(void) | |
d1708134 MD |
66 | { |
67 | type_classes = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
4c8bfb7e | 68 | NULL, (GDestroyNotify) free_type); |
d1708134 MD |
69 | if (!type_classes) |
70 | return -ENOMEM; | |
71 | return 0; | |
72 | } | |
73 | ||
4c8bfb7e | 74 | int finalize_types(void) |
d1708134 MD |
75 | { |
76 | g_hash_table_destroy(type_classes); | |
be85c1c7 | 77 | return 0; |
d1708134 | 78 | } |