Move types.c to types/
[babeltrace.git] / formats / ctf / types / enum.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Enumeration mapping strings (quarks) from/to integers.
5 *
de0ba614 6 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 7 *
de0ba614
MD
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
6dc2ca62
MD
21 */
22
23#include <ctf/ctf-types.h>
24#include <stdint.h>
25#include <glib.h>
26
27struct enum_table {
28 GHashTable *value_to_quark; /* Tuples (value, GQuark) */
29 GHashTable *quark_to_value; /* Tuples (GQuark, value) */
30};
31
32#if (__WORDSIZE == 32)
33GQuark enum_uint_to_quark(const struct enum_table *table, uint64_t v)
34{
35 gconstpointer q = g_hash_table_lookup(table->value_to_quark, &v);
36 return (GQuark) (unsigned long) q;
37}
38
39GQuark enum_int_to_quark(const struct enum_table *table, uint64_t v)
40{
41 gconstpointer q = g_hash_table_lookup(table->value_to_quark, &v);
42 return (GQuark) (unsigned long) q;
43}
44
45uint64_t enum_quark_to_uint(size_t len, int byte_order, GQuark q)
46{
47 gconstpointer v = g_hash_table_lookup(table->quark_to_value,
48 (gconstpointer) q);
49 return *(const uint64_t *) v;
50}
51
52int64_t enum_quark_to_int(size_t len, int byte_order, GQuark q)
53{
54 gconstpointer v = g_hash_table_lookup(table->quark_to_value,
55 (gconstpointer) q);
56 return *(const int64_t *) v;
57}
58
59guint enum_val_hash(gconstpointer key)
60{
61 int64_t ukey = *(const int64_t *)key;
62
63 return (guint)ukey ^ (guint)(ukey >> 32);
64}
65
66gboolean enum_val_equal(gconstpointer a, gconstpointer b)
67{
68 int64_t ua = *(const int64_t *)a;
69 int64_t ub = *(const int64_t *)b;
70
71 return ua == ub;
72}
73
74void enum_val_free(void *ptr)
75{
76 g_free(ptr);
77}
78
79void enum_signed_insert(struct enum_table *table, int64_t v, GQuark q)
80{
81 int64_t *valuep = g_new(int64_t, 1);
82
83 g_hash_table_insert(table->value_to_quark, valuep,
84 (gpointer) (unsigned long) q);
85 g_hash_table_insert(table->quark_to_value, (gpointer) (unsigned long) q,
86 valuep);
87}
88
89void enum_unsigned_insert(struct enum_table *table, uint64_t v, GQuark q)
90{
91 uint64_t *valuep = g_new(uint64_t, 1);
92
93 g_hash_table_insert(table->value_to_quark, valuep,
94 (gpointer) (unsigned long) q);
95 g_hash_table_insert(table->quark_to_value, (gpointer) (unsigned long) q,
96 valuep);
97}
98#else /* __WORDSIZE != 32 */
99GQuark enum_uint_to_quark(const struct enum_table *table, uint64_t v)
100{
101 gconstpointer q = g_hash_table_lookup(table->value_to_quark,
102 (gconstpointer) v);
103 return (GQuark) (unsigned long) q;
104}
105
106GQuark enum_int_to_quark(const struct enum_table *table, uint64_t v)
107{
108 gconstpointer q = g_hash_table_lookup(table->value_to_quark,
109 (gconstpointer) v);
110 return (GQuark) (unsigned long) q;
111}
112
113uint64_t enum_quark_to_uint(size_t len, int byte_order, GQuark q)
114{
115 gconstpointer v = g_hash_table_lookup(table->quark_to_value,
116 (gconstpointer) (unsigned long) q);
117 return *(const uint64_t *) v;
118}
119
120int64_t enum_quark_to_int(size_t len, int byte_order, GQuark q)
121{
122 gconstpointer v = g_hash_table_lookup(table->quark_to_value,
123 (gconstpointer) (unsigned long) q);
124 return *(const int64_t *) v;
125}
126
127guint enum_val_hash(gconstpointer key)
128{
129 return g_direct_hash(key);
130}
131
132gboolean enum_val_equal(gconstpointer a, gconstpointer b)
133{
134 return g_direct_equal(a, b);
135}
136
137void enum_val_free(void *ptr)
138{
139}
140
141void enum_signed_insert(struct enum_table *table, int64_t v, GQuark q)
142{
143 g_hash_table_insert(table->value_to_quark, (gpointer) v,
144 (gpointer) (unsigned long) q);
145 g_hash_table_insert(table->quark_to_value, (gpointer) (unsigned long) q,
146 valuep);
147}
148
149void enum_unsigned_insert(struct enum_table *table, uint64_t v, GQuark q)
150{
151 g_hash_table_insert(table->value_to_quark, (gpointer) v,
152 (gpointer) (unsigned long) q);
153 g_hash_table_insert(table->quark_to_value, (gpointer) (unsigned long) q,
154 valuep);
155}
156#endif /* __WORDSIZE != 32 */
157
158struct enum_table *enum_new(void)
159{
160 struct enum_table *table;
161
162 table = g_new(struct enum_table, 1);
d1708134
MD
163 table->value_to_quark = g_hash_table(enum_val_hash, enum_val_equal);
164 table->quark_to_value = g_hash_table_new_full(g_direct_hash,
165 g_direct_equal,
166 NULL, enum_val_free);
6dc2ca62
MD
167}
168
169void enum_destroy(struct enum_table *table)
170{
171 g_hash_table_destroy(table->value_to_quark);
172 g_hash_table_destroy(table->quark_to_value);
173 g_free(table);
174}
This page took 0.029104 seconds and 4 git commands to generate.