961337800a1e45472a2abd175de21448726884d5
[babeltrace.git] / types / types.c
1 /*
2 * types.c
3 *
4 * BabelTrace - Converter
5 *
6 * Types registry.
7 *
8 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/format.h>
22 #include <glib.h>
23 #include <errno.h>
24
25 static
26 struct type_class *lookup_type_class_scope(GQuark qname,
27 struct declaration_scope *scope)
28 {
29 return g_hash_table_lookup(scope->type_classes,
30 (gconstpointer) (unsigned long) qname);
31 }
32
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)
53 {
54 type->p.type_free(type);
55 }
56
57 int register_type_class(struct type_class *type_class,
58 struct declaration_scope *scope)
59 {
60 /* Only lookup in local scope */
61 if (lookup_type_class_scope(type_class->name, scope))
62 return -EEXIST;
63
64 g_hash_table_insert(scope->type_classes,
65 (gpointer) (unsigned long) type_class->name,
66 type_class);
67 return 0;
68 }
69
70 void type_class_ref(struct type_class *type_class)
71 {
72 type_class->ref++;
73 }
74
75 void type_class_unref(struct type_class *type_class)
76 {
77 if (!--type_class->ref)
78 free_type_class(type_class);
79 }
80
81 void type_ref(struct type *type)
82 {
83 type->ref++;
84 }
85
86 void type_unref(struct type *type)
87 {
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);
108 }
This page took 0.03133 seconds and 3 git commands to generate.