| 1 | /* |
| 2 | * Common Trace Format - Converter |
| 3 | * |
| 4 | * Types registry. |
| 5 | * |
| 6 | * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <ctf/ctf-types.h> |
| 24 | #include <glib.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | struct type_class { |
| 28 | GQuark qname; |
| 29 | void (*read)(); |
| 30 | size_t (*write)(); |
| 31 | }; |
| 32 | |
| 33 | struct type { |
| 34 | struct type_class *class; |
| 35 | size_t alignment; /* type alignment, in bits */ |
| 36 | ssize_t len; /* type length, in bits. -1 for dynamic size. */ |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Type class hash table contains the registered type classes. Type class |
| 41 | * registration is typically performed by a plugin. |
| 42 | * TODO: support plugin unload (unregistration of type classes). |
| 43 | */ |
| 44 | GHashTable *type_classes; |
| 45 | |
| 46 | struct type_class *ctf_lookup_type_class(GQuark qname) |
| 47 | { |
| 48 | return g_hash_table_lookup(type_classes, |
| 49 | (gconstpointer) (unsigned long) qname) |
| 50 | } |
| 51 | |
| 52 | int ctf_register_type_class(const char *name, |
| 53 | void (*read)(), |
| 54 | void (*write)()) |
| 55 | { |
| 56 | struct type_class tc = g_new(struct type_class, 1); |
| 57 | GQuark qname = g_quark_from_string(name); |
| 58 | |
| 59 | if (ctf_lookup_type_class(qname)) |
| 60 | return -EEXIST; |
| 61 | |
| 62 | g_hash_table_insert(type_classes, |
| 63 | (gconstpointer) (unsigned long) qname, |
| 64 | tc); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int ctf_init_types(void) |
| 69 | { |
| 70 | type_classes = g_hash_table_new_full(g_direct_hash, g_direct_equal, |
| 71 | NULL, g_free); |
| 72 | if (!type_classes) |
| 73 | return -ENOMEM; |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int ctf_finalize_types(void) |
| 78 | { |
| 79 | g_hash_table_destroy(type_classes); |
| 80 | } |