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