Commit | Line | Data |
---|---|---|
6dc2ca62 | 1 | /* |
ccd7e1c8 MD |
2 | * types.c |
3 | * | |
d79865b9 | 4 | * BabelTrace - Converter |
6dc2ca62 MD |
5 | * |
6 | * Types registry. | |
7 | * | |
c054553d | 8 | * Copyright 2010, 2011 - 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 | ||
c054553d MD |
25 | static |
26 | struct type_class *lookup_type_class_scope(GQuark qname, | |
27 | struct declaration_scope *scope) | |
d1708134 | 28 | { |
c054553d | 29 | return g_hash_table_lookup(scope->type_classes, |
4c8bfb7e | 30 | (gconstpointer) (unsigned long) qname); |
d1708134 MD |
31 | } |
32 | ||
c054553d MD |
33 | struct type_class *lookup_type_class(GQuark qname, |
34 | struct declaration_scope *scope) | |
35 | { | |
36 | struct type_class *tc; | |
37 | ||
38 | while (scope) { | |
39 | tc = lookup_type_class_scope(qname, scope); | |
40 | if (tc) | |
41 | return tc; | |
42 | scope = scope->parent_scope; | |
43 | } | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | static void free_type_class(struct type_class *type_class) | |
48 | { | |
49 | type_class->class_free(type_class); | |
50 | } | |
51 | ||
52 | static void free_type(struct type *type) | |
90b676d7 | 53 | { |
c054553d | 54 | type->p.type_free(type); |
90b676d7 MD |
55 | } |
56 | ||
c054553d MD |
57 | int register_type_class(struct type_class *type_class, |
58 | struct declaration_scope *scope) | |
d1708134 | 59 | { |
c054553d MD |
60 | /* Only lookup in local scope */ |
61 | if (lookup_type_class_scope(type_class->name, scope)) | |
d1708134 MD |
62 | return -EEXIST; |
63 | ||
c054553d | 64 | g_hash_table_insert(scope->type_classes, |
4c8bfb7e | 65 | (gpointer) (unsigned long) type_class->name, |
698f0fe4 | 66 | type_class); |
d1708134 MD |
67 | return 0; |
68 | } | |
69 | ||
c054553d | 70 | void type_class_ref(struct type_class *type_class) |
4c8bfb7e MD |
71 | { |
72 | type_class->ref++; | |
73 | } | |
74 | ||
c054553d | 75 | void type_class_unref(struct type_class *type_class) |
4c8bfb7e MD |
76 | { |
77 | if (!--type_class->ref) | |
c054553d | 78 | free_type_class(type_class); |
4c8bfb7e MD |
79 | } |
80 | ||
c054553d | 81 | void type_ref(struct type *type) |
d1708134 | 82 | { |
c054553d | 83 | type->ref++; |
d1708134 MD |
84 | } |
85 | ||
c054553d | 86 | void type_unref(struct type *type) |
d1708134 | 87 | { |
c054553d MD |
88 | if (!--type->ref) |
89 | free_type(type); | |
90 | } | |
91 | ||
92 | struct declaration_scope * | |
93 | new_declaration_scope(struct declaration_scope *parent_scope) | |
94 | { | |
95 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); | |
96 | ||
97 | scope->type_classes = g_hash_table_new_full(g_direct_hash, | |
98 | g_direct_equal, NULL, | |
99 | (GDestroyNotify) type_class_unref); | |
100 | scope->parent_scope = parent_scope; | |
101 | return scope; | |
102 | } | |
103 | ||
104 | void free_declaration_scope(struct declaration_scope *scope) | |
105 | { | |
106 | g_hash_table_destroy(scope->type_classes); | |
107 | g_free(scope); | |
d1708134 | 108 | } |