I/O structures accessors generation (work in progress)
[babeltrace.git] / types / types.c
CommitLineData
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 25static
ac88af75 26struct type *
64893f33 27 lookup_type_scope(GQuark type_name, struct type_scope *scope)
d1708134 28{
e19c3d69 29 return g_hash_table_lookup(scope->types,
ac88af75 30 (gconstpointer) (unsigned long) type_name);
d1708134
MD
31}
32
64893f33 33struct type *lookup_type(GQuark type_name, struct type_scope *scope)
c054553d 34{
e19c3d69 35 struct type *type;
c054553d
MD
36
37 while (scope) {
ac88af75 38 type = lookup_type_scope(type_name, scope);
e19c3d69
MD
39 if (type)
40 return type;
c054553d
MD
41 scope = scope->parent_scope;
42 }
43 return NULL;
44}
45
64893f33 46int register_type(GQuark name, struct type *type, struct type_scope *scope)
d1708134 47{
64893f33 48 if (!name)
6ee5115e
MD
49 return -EPERM;
50
c054553d 51 /* Only lookup in local scope */
64893f33 52 if (lookup_type_scope(name, scope))
d1708134
MD
53 return -EEXIST;
54
e19c3d69 55 g_hash_table_insert(scope->types,
64893f33 56 (gpointer) (unsigned long) name,
e19c3d69 57 type);
ac88af75
MD
58 type_ref(type);
59 return 0;
60}
61
62static
63struct declaration *
64 lookup_declaration_scope(GQuark field_name, struct declaration_scope *scope)
65{
66 return g_hash_table_lookup(scope->declarations,
67 (gconstpointer) (unsigned long) field_name);
68}
69
70struct declaration *
71 lookup_declaration(GQuark field_name, struct declaration_scope *scope)
72{
73 struct declaration *declaration;
74
75 while (scope) {
76 declaration = lookup_declaration_scope(field_name, scope);
77 if (declaration)
78 return declaration;
79 scope = scope->parent_scope;
80 }
81 return NULL;
82}
83
84int register_declaration(GQuark field_name, struct declaration *declaration,
85 struct declaration_scope *scope)
86{
87 if (!field_name)
88 return -EPERM;
89
90 /* Only lookup in local scope */
91 if (lookup_declaration_scope(field_name, scope))
92 return -EEXIST;
93
94 g_hash_table_insert(scope->declarations,
95 (gpointer) (unsigned long) field_name,
96 declaration);
97 declaration_ref(declaration);
d1708134
MD
98 return 0;
99}
100
e19c3d69 101void type_ref(struct type *type)
4c8bfb7e 102{
e19c3d69 103 type->ref++;
4c8bfb7e
MD
104}
105
e19c3d69 106void type_unref(struct type *type)
4c8bfb7e 107{
e19c3d69 108 if (!--type->ref)
ac88af75 109 type->type_free(type);
4c8bfb7e
MD
110}
111
e19c3d69 112void declaration_ref(struct declaration *declaration)
d1708134 113{
e19c3d69 114 declaration->ref++;
d1708134
MD
115}
116
e19c3d69 117void declaration_unref(struct declaration *declaration)
d1708134 118{
e19c3d69 119 if (!--declaration->ref)
ac88af75 120 declaration->type->declaration_free(declaration);
c054553d
MD
121}
122
64893f33
MD
123struct type_scope *
124 new_type_scope(struct type_scope *parent_scope)
c054553d 125{
64893f33 126 struct type_scope *scope = g_new(struct type_scope, 1);
c054553d 127
e19c3d69 128 scope->types = g_hash_table_new_full(g_direct_hash,
c054553d 129 g_direct_equal, NULL,
e19c3d69 130 (GDestroyNotify) type_unref);
64893f33
MD
131 scope->parent_scope = parent_scope;
132 return scope;
133}
134
135void free_type_scope(struct type_scope *scope)
136{
137 g_hash_table_destroy(scope->types);
138 g_free(scope);
139}
140
141struct declaration_scope *
142 new_declaration_scope(struct declaration_scope *parent_scope)
143{
144 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
145
ac88af75
MD
146 scope->declarations = g_hash_table_new_full(g_direct_hash,
147 g_direct_equal, NULL,
148 (GDestroyNotify) declaration_unref);
c054553d
MD
149 scope->parent_scope = parent_scope;
150 return scope;
151}
152
153void free_declaration_scope(struct declaration_scope *scope)
154{
ac88af75 155 g_hash_table_destroy(scope->declarations);
c054553d 156 g_free(scope);
d1708134 157}
This page took 0.029889 seconds and 4 git commands to generate.