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