2 * BabelTrace - Enumeration Type
4 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/types.h>
27 GHashTable
*value_to_quark
; /* Tuples (value, GQuark) */
28 GHashTable
*quark_to_value
; /* Tuples (GQuark, value) */
31 #if (__WORDSIZE == 32)
32 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
34 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
36 return (GQuark
) (unsigned long) q
;
39 GQuark
enum_int_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
41 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
43 return (GQuark
) (unsigned long) q
;
46 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
47 size_t len
, int byte_order
, GQuark q
)
49 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
51 return *(const uint64_t *) v
;
54 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
55 size_t len
, int byte_order
, GQuark q
)
57 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
59 return *(const int64_t *) v
;
62 guint
enum_val_hash(gconstpointer key
)
64 int64_t ukey
= *(const int64_t *)key
;
66 return (guint
)ukey
^ (guint
)(ukey
>> 32);
69 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
71 int64_t ua
= *(const int64_t *)a
;
72 int64_t ub
= *(const int64_t *)b
;
77 void enum_val_free(void *ptr
)
82 void enum_signed_insert(struct type_class_enum
*enum_class
, int64_t v
, GQuark q
)
84 int64_t *valuep
= g_new(int64_t, 1);
86 g_hash_table_insert(enum_class
->table
>value_to_quark
, valuep
,
87 (gpointer
) (unsigned long) q
);
88 g_hash_table_insert(enum_class
->table
.quark_to_value
,
89 (gpointer
) (unsigned long) q
,
93 void enum_unsigned_insert(struct type_class_enum
*enum_class
, uint64_t v
, GQuark q
)
95 uint64_t *valuep
= g_new(uint64_t, 1);
97 g_hash_table_insert(enum_class
->table
.value_to_quark
, valuep
,
98 (gpointer
) (unsigned long) q
);
99 g_hash_table_insert(enum_class
->table
.quark_to_value
,
100 (gpointer
) (unsigned long) q
,
103 #else /* __WORDSIZE != 32 */
104 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
106 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
108 return (GQuark
) (unsigned long) q
;
111 GQuark
enum_int_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
113 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
115 return (GQuark
) (unsigned long) q
;
118 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
119 size_t len
, int byte_order
, GQuark q
)
121 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
122 (gconstpointer
) (unsigned long) q
);
123 return *(const uint64_t *) v
;
126 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
127 size_t len
, int byte_order
, GQuark q
)
129 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
130 (gconstpointer
) (unsigned long) q
);
131 return *(const int64_t *) v
;
134 guint
enum_val_hash(gconstpointer key
)
136 return g_direct_hash(key
);
139 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
141 return g_direct_equal(a
, b
);
144 void enum_val_free(void *ptr
)
148 void enum_signed_insert(struct type_class_enum
*enum_class
,
151 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
152 (gpointer
) (unsigned long) q
);
153 g_hash_table_insert(enum_class
->table
.quark_to_value
,
154 (gpointer
) (unsigned long) q
,
158 void enum_unsigned_insert(struct type_class_enum
*enum_class
,
159 uint64_t v
, GQuark q
)
161 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
162 (gpointer
) (unsigned long) q
);
163 g_hash_table_insert(enum_class
->table
.quark_to_value
,
164 (gpointer
) (unsigned long) q
,
167 #endif /* __WORDSIZE != 32 */
169 size_t enum_copy(unsigned char *dest
, const struct format
*fdest
,
170 const unsigned char *src
, const struct format
*fsrc
,
171 const struct type_class
*type_class
)
173 struct type_class_enum
*enum_class
=
174 container_of(type_class
, struct type_class_enum
, p
);
175 struct type_class_bitfield
*bitfield_class
= &enum_class
->p
;
176 struct type_class_integer
*int_class
= &bitfield_class
->p
;
179 v
= fsrc
->enum_read(src
, enum_class
)
180 return fdest
->enum_write(dest
, enum_class
, v
);
183 void enum_type_free(struct type_class_enum
*enum_class
)
185 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
186 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
191 void _enum_type_free(struct type_class
*type_class
)
193 struct type_class_enum
*enum_class
=
194 container_of(type_class
, struct type_class_enum
, p
);
195 enum_type_free(enum_class
);
198 struct type_class_enum
*enum_type_new(const char *name
,
199 size_t len
, int byte_order
,
203 struct type_class_bitfield
*bitfield_class
;
204 struct type_class_integer
*int_class
;
207 enum_class
= g_new(struct type_class_bitfield
, 1);
208 enum_class
->table
.value_to_quark
= g_hash_table(enum_val_hash
,
210 enum_class
->table
.quark_to_value
= g_hash_table_new_full(g_direct_hash
,
212 NULL
, enum_val_free
);
213 bitfield_class
= &enum_class
->p
;
214 int_class
= &bitfield_class
->p
;
215 int_class
->p
.name
= g_quark_from_string(name
);
216 int_class
->p
.alignment
= alignment
;
217 int_class
->p
.copy
= enum_copy
;
218 int_class
->p
.free
= _enum_type_free
;
219 int_class
->len
= len
;
220 int_class
->byte_order
= byte_order
;
221 int_class
->signedness
= signedness
;
222 if (int_class
->p
.name
) {
223 ret
= register_type(&int_class
->p
);
225 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
226 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
This page took 0.03637 seconds and 5 git commands to generate.