| 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 *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 | |
| 32 | struct 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 | |
| 45 | static void free_type(struct type *type) |
| 46 | { |
| 47 | type->type_free(type); |
| 48 | } |
| 49 | |
| 50 | static void free_declaration(struct declaration *declaration) |
| 51 | { |
| 52 | declaration->type->declaration_free(declaration); |
| 53 | } |
| 54 | |
| 55 | int register_type(struct type *type, struct declaration_scope *scope) |
| 56 | { |
| 57 | if (!type->name) |
| 58 | return -EPERM; |
| 59 | |
| 60 | /* Only lookup in local scope */ |
| 61 | if (lookup_type_scope(type->name, scope)) |
| 62 | return -EEXIST; |
| 63 | |
| 64 | g_hash_table_insert(scope->types, |
| 65 | (gpointer) (unsigned long) type->name, |
| 66 | type); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | void type_ref(struct type *type) |
| 71 | { |
| 72 | type->ref++; |
| 73 | } |
| 74 | |
| 75 | void type_unref(struct type *type) |
| 76 | { |
| 77 | if (!--type->ref) |
| 78 | free_type(type); |
| 79 | } |
| 80 | |
| 81 | void declaration_ref(struct declaration *declaration) |
| 82 | { |
| 83 | declaration->ref++; |
| 84 | } |
| 85 | |
| 86 | void declaration_unref(struct declaration *declaration) |
| 87 | { |
| 88 | if (!--declaration->ref) |
| 89 | free_declaration(declaration); |
| 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->types = g_hash_table_new_full(g_direct_hash, |
| 98 | g_direct_equal, NULL, |
| 99 | (GDestroyNotify) type_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->types); |
| 107 | g_free(scope); |
| 108 | } |