4 * BabelTrace - Enumeration Type
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/types.h>
25 GHashTable
*value_to_quark
; /* Tuples (value, GQuark) */
26 GHashTable
*quark_to_value
; /* Tuples (GQuark, value) */
29 #if (__WORDSIZE == 32)
30 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
32 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
34 return (GQuark
) (unsigned long) q
;
37 GQuark
enum_int_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
39 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
41 return (GQuark
) (unsigned long) q
;
44 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
45 size_t len
, int byte_order
, GQuark q
)
47 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
49 return *(const uint64_t *) v
;
52 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
53 size_t len
, int byte_order
, GQuark q
)
55 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
57 return *(const int64_t *) v
;
60 guint
enum_val_hash(gconstpointer key
)
62 int64_t ukey
= *(const int64_t *)key
;
64 return (guint
)ukey
^ (guint
)(ukey
>> 32);
67 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
69 int64_t ua
= *(const int64_t *)a
;
70 int64_t ub
= *(const int64_t *)b
;
75 void enum_val_free(void *ptr
)
80 void enum_signed_insert(struct type_class_enum
*enum_class
, int64_t v
, GQuark q
)
82 int64_t *valuep
= g_new(int64_t, 1);
84 g_hash_table_insert(enum_class
->table
>value_to_quark
, valuep
,
85 (gpointer
) (unsigned long) q
);
86 g_hash_table_insert(enum_class
->table
.quark_to_value
,
87 (gpointer
) (unsigned long) q
,
91 void enum_unsigned_insert(struct type_class_enum
*enum_class
, uint64_t v
, GQuark q
)
93 uint64_t *valuep
= g_new(uint64_t, 1);
95 g_hash_table_insert(enum_class
->table
.value_to_quark
, valuep
,
96 (gpointer
) (unsigned long) q
);
97 g_hash_table_insert(enum_class
->table
.quark_to_value
,
98 (gpointer
) (unsigned long) q
,
101 #else /* __WORDSIZE != 32 */
102 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
104 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
106 return (GQuark
) (unsigned long) q
;
109 GQuark
enum_int_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
111 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
113 return (GQuark
) (unsigned long) q
;
116 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
117 size_t len
, int byte_order
, GQuark q
)
119 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
120 (gconstpointer
) (unsigned long) q
);
121 return *(const uint64_t *) v
;
124 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
125 size_t len
, int byte_order
, GQuark q
)
127 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
128 (gconstpointer
) (unsigned long) q
);
129 return *(const int64_t *) v
;
132 guint
enum_val_hash(gconstpointer key
)
134 return g_direct_hash(key
);
137 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
139 return g_direct_equal(a
, b
);
142 void enum_val_free(void *ptr
)
146 void enum_signed_insert(struct type_class_enum
*enum_class
,
149 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
150 (gpointer
) (unsigned long) q
);
151 g_hash_table_insert(enum_class
->table
.quark_to_value
,
152 (gpointer
) (unsigned long) q
,
156 void enum_unsigned_insert(struct type_class_enum
*enum_class
,
157 uint64_t v
, GQuark q
)
159 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
160 (gpointer
) (unsigned long) q
);
161 g_hash_table_insert(enum_class
->table
.quark_to_value
,
162 (gpointer
) (unsigned long) q
,
165 #endif /* __WORDSIZE != 32 */
167 size_t enum_copy(unsigned char *dest
, const struct format
*fdest
,
168 const unsigned char *src
, const struct format
*fsrc
,
169 const struct type_class
*type_class
)
171 struct type_class_enum
*enum_class
=
172 container_of(type_class
, struct type_class_enum
, p
);
173 struct type_class_integer
*int_class
= &enum_class
->p
;
176 v
= fsrc
->enum_read(src
, enum_class
)
177 return fdest
->enum_write(dest
, enum_class
, v
);
180 void enum_type_free(struct type_class_enum
*enum_class
)
182 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
183 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
188 void _enum_type_free(struct type_class
*type_class
)
190 struct type_class_enum
*enum_class
=
191 container_of(type_class
, struct type_class_enum
, p
);
192 enum_type_free(enum_class
);
195 struct type_class_enum
*enum_type_new(const char *name
,
196 size_t len
, int byte_order
,
200 struct type_class_integer
*int_class
;
203 enum_class
= g_new(struct type_class_enum
, 1);
204 enum_class
->table
.value_to_quark
= g_hash_table_new(enum_val_hash
,
206 enum_class
->table
.quark_to_value
= g_hash_table_new_full(g_direct_hash
,
208 NULL
, enum_val_free
);
209 int_class
= &enum_class
->p
;
210 int_class
->p
.name
= g_quark_from_string(name
);
211 int_class
->p
.alignment
= alignment
;
212 int_class
->p
.copy
= enum_copy
;
213 int_class
->p
.free
= _enum_type_free
;
214 int_class
->len
= len
;
215 int_class
->byte_order
= byte_order
;
216 int_class
->signedness
= signedness
;
217 if (int_class
->p
.name
) {
218 ret
= register_type(&int_class
->p
);
220 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
221 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
This page took 0.034933 seconds and 5 git commands to generate.