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 | 25 | static |
e19c3d69 | 26 | struct type *lookup_type_scope(GQuark qname, struct declaration_scope *scope) |
d1708134 | 27 | { |
e19c3d69 | 28 | return g_hash_table_lookup(scope->types, |
4c8bfb7e | 29 | (gconstpointer) (unsigned long) qname); |
d1708134 MD |
30 | } |
31 | ||
e19c3d69 | 32 | struct type *lookup_type(GQuark qname, struct declaration_scope *scope) |
c054553d | 33 | { |
e19c3d69 | 34 | struct type *type; |
c054553d MD |
35 | |
36 | while (scope) { | |
e19c3d69 MD |
37 | type = lookup_type_scope(qname, scope); |
38 | if (type) | |
39 | return type; | |
c054553d MD |
40 | scope = scope->parent_scope; |
41 | } | |
42 | return NULL; | |
43 | } | |
44 | ||
e19c3d69 | 45 | static void free_type(struct type *type) |
c054553d | 46 | { |
e19c3d69 | 47 | type->type_free(type); |
c054553d MD |
48 | } |
49 | ||
e19c3d69 | 50 | static void free_declaration(struct declaration *declaration) |
90b676d7 | 51 | { |
e19c3d69 | 52 | declaration->p.declaration_free(declaration); |
90b676d7 MD |
53 | } |
54 | ||
e19c3d69 | 55 | int register_type(struct type *type, struct declaration_scope *scope) |
d1708134 | 56 | { |
c054553d | 57 | /* Only lookup in local scope */ |
e19c3d69 | 58 | if (lookup_type_scope(type->name, scope)) |
d1708134 MD |
59 | return -EEXIST; |
60 | ||
e19c3d69 MD |
61 | g_hash_table_insert(scope->types, |
62 | (gpointer) (unsigned long) type->name, | |
63 | type); | |
d1708134 MD |
64 | return 0; |
65 | } | |
66 | ||
e19c3d69 | 67 | void type_ref(struct type *type) |
4c8bfb7e | 68 | { |
e19c3d69 | 69 | type->ref++; |
4c8bfb7e MD |
70 | } |
71 | ||
e19c3d69 | 72 | void type_unref(struct type *type) |
4c8bfb7e | 73 | { |
e19c3d69 MD |
74 | if (!--type->ref) |
75 | free_type(type); | |
4c8bfb7e MD |
76 | } |
77 | ||
e19c3d69 | 78 | void declaration_ref(struct declaration *declaration) |
d1708134 | 79 | { |
e19c3d69 | 80 | declaration->ref++; |
d1708134 MD |
81 | } |
82 | ||
e19c3d69 | 83 | void declaration_unref(struct declaration *declaration) |
d1708134 | 84 | { |
e19c3d69 MD |
85 | if (!--declaration->ref) |
86 | free_declaration(declaration); | |
c054553d MD |
87 | } |
88 | ||
89 | struct declaration_scope * | |
e19c3d69 | 90 | new_declaration_scope(struct declaration_scope *parent_scope) |
c054553d MD |
91 | { |
92 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); | |
93 | ||
e19c3d69 | 94 | scope->types = g_hash_table_new_full(g_direct_hash, |
c054553d | 95 | g_direct_equal, NULL, |
e19c3d69 | 96 | (GDestroyNotify) type_unref); |
c054553d MD |
97 | scope->parent_scope = parent_scope; |
98 | return scope; | |
99 | } | |
100 | ||
101 | void free_declaration_scope(struct declaration_scope *scope) | |
102 | { | |
e19c3d69 | 103 | g_hash_table_destroy(scope->types); |
c054553d | 104 | g_free(scope); |
d1708134 | 105 | } |