507d0c35e9ef0d26bffcbe1cb9a3eedaa04858e5
4 * Enumeration mapping strings (quarks) from/to integers.
6 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <ctf/ctf-types.h>
28 GHashTable
*value_to_quark
; /* Tuples (value, GQuark) */
29 GHashTable
*quark_to_value
; /* Tuples (GQuark, value) */
32 #if (__WORDSIZE == 32)
33 GQuark
enum_uint_to_quark(const struct enum_table
*table
, uint64_t v
)
35 gconstpointer q
= g_hash_table_lookup(table
->value_to_quark
, &v
);
36 return (GQuark
) (unsigned long) q
;
39 GQuark
enum_int_to_quark(const struct enum_table
*table
, uint64_t v
)
41 gconstpointer q
= g_hash_table_lookup(table
->value_to_quark
, &v
);
42 return (GQuark
) (unsigned long) q
;
45 uint64_t enum_quark_to_uint(size_t len
, int byte_order
, GQuark q
)
47 gconstpointer v
= g_hash_table_lookup(table
->quark_to_value
,
49 return *(const uint64_t *) v
;
52 int64_t enum_quark_to_int(size_t len
, int byte_order
, GQuark q
)
54 gconstpointer v
= g_hash_table_lookup(table
->quark_to_value
,
56 return *(const int64_t *) v
;
59 guint
enum_val_hash(gconstpointer key
)
61 int64_t ukey
= *(const int64_t *)key
;
63 return (guint
)ukey
^ (guint
)(ukey
>> 32);
66 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
68 int64_t ua
= *(const int64_t *)a
;
69 int64_t ub
= *(const int64_t *)b
;
74 void enum_val_free(void *ptr
)
79 void enum_signed_insert(struct enum_table
*table
, int64_t v
, GQuark q
)
81 int64_t *valuep
= g_new(int64_t, 1);
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
,
89 void enum_unsigned_insert(struct enum_table
*table
, uint64_t v
, GQuark q
)
91 uint64_t *valuep
= g_new(uint64_t, 1);
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
,
98 #else /* __WORDSIZE != 32 */
99 GQuark
enum_uint_to_quark(const struct enum_table
*table
, uint64_t v
)
101 gconstpointer q
= g_hash_table_lookup(table
->value_to_quark
,
103 return (GQuark
) (unsigned long) q
;
106 GQuark
enum_int_to_quark(const struct enum_table
*table
, uint64_t v
)
108 gconstpointer q
= g_hash_table_lookup(table
->value_to_quark
,
110 return (GQuark
) (unsigned long) q
;
113 uint64_t enum_quark_to_uint(size_t len
, int byte_order
, GQuark q
)
115 gconstpointer v
= g_hash_table_lookup(table
->quark_to_value
,
116 (gconstpointer
) (unsigned long) q
);
117 return *(const uint64_t *) v
;
120 int64_t enum_quark_to_int(size_t len
, int byte_order
, GQuark q
)
122 gconstpointer v
= g_hash_table_lookup(table
->quark_to_value
,
123 (gconstpointer
) (unsigned long) q
);
124 return *(const int64_t *) v
;
127 guint
enum_val_hash(gconstpointer key
)
129 return g_direct_hash(key
);
132 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
134 return g_direct_equal(a
, b
);
137 void enum_val_free(void *ptr
)
141 void enum_signed_insert(struct enum_table
*table
, int64_t v
, GQuark q
)
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
,
149 void enum_unsigned_insert(struct enum_table
*table
, uint64_t v
, GQuark q
)
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
,
156 #endif /* __WORDSIZE != 32 */
158 struct enum_table
*enum_new(void)
160 struct enum_table
*table
;
162 table
= g_new(struct enum_table
, 1);
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
,
166 NULL
, enum_val_free
);
169 void enum_destroy(struct enum_table
*table
)
171 g_hash_table_destroy(table
->value_to_quark
);
172 g_hash_table_destroy(table
->quark_to_value
);
This page took 0.031715 seconds and 3 git commands to generate.