Build fixes.
[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
e19c3d69 26struct 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 32struct 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 45static void free_type(struct type *type)
c054553d 46{
e19c3d69 47 type->type_free(type);
c054553d
MD
48}
49
e19c3d69 50static void free_declaration(struct declaration *declaration)
90b676d7 51{
6ee5115e 52 declaration->type->declaration_free(declaration);
90b676d7
MD
53}
54
e19c3d69 55int register_type(struct type *type, struct declaration_scope *scope)
d1708134 56{
6ee5115e
MD
57 if (!type->name)
58 return -EPERM;
59
c054553d 60 /* Only lookup in local scope */
e19c3d69 61 if (lookup_type_scope(type->name, scope))
d1708134
MD
62 return -EEXIST;
63
e19c3d69
MD
64 g_hash_table_insert(scope->types,
65 (gpointer) (unsigned long) type->name,
66 type);
d1708134
MD
67 return 0;
68}
69
e19c3d69 70void type_ref(struct type *type)
4c8bfb7e 71{
e19c3d69 72 type->ref++;
4c8bfb7e
MD
73}
74
e19c3d69 75void type_unref(struct type *type)
4c8bfb7e 76{
e19c3d69
MD
77 if (!--type->ref)
78 free_type(type);
4c8bfb7e
MD
79}
80
e19c3d69 81void declaration_ref(struct declaration *declaration)
d1708134 82{
e19c3d69 83 declaration->ref++;
d1708134
MD
84}
85
e19c3d69 86void declaration_unref(struct declaration *declaration)
d1708134 87{
e19c3d69
MD
88 if (!--declaration->ref)
89 free_declaration(declaration);
c054553d
MD
90}
91
92struct declaration_scope *
e19c3d69 93 new_declaration_scope(struct declaration_scope *parent_scope)
c054553d
MD
94{
95 struct declaration_scope *scope = g_new(struct declaration_scope, 1);
96
e19c3d69 97 scope->types = g_hash_table_new_full(g_direct_hash,
c054553d 98 g_direct_equal, NULL,
e19c3d69 99 (GDestroyNotify) type_unref);
c054553d
MD
100 scope->parent_scope = parent_scope;
101 return scope;
102}
103
104void free_declaration_scope(struct declaration_scope *scope)
105{
e19c3d69 106 g_hash_table_destroy(scope->types);
c054553d 107 g_free(scope);
d1708134 108}
This page took 0.02708 seconds and 4 git commands to generate.