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/format.h>
24 #if (__WORDSIZE == 32)
25 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
27 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
29 return (GQuark
) (unsigned long) q
;
32 GQuark
enum_int_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 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
42 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
44 return *(const uint64_t *) v
;
47 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
50 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
52 return *(const int64_t *) v
;
55 guint
enum_val_hash(gconstpointer key
)
57 int64_t ukey
= *(const int64_t *)key
;
59 return (guint
)ukey
^ (guint
)(ukey
>> 32);
62 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
64 int64_t ua
= *(const int64_t *)a
;
65 int64_t ub
= *(const int64_t *)b
;
70 void enum_val_free(void *ptr
)
75 void enum_signed_insert(struct type_class_enum
*enum_class
, int64_t v
, GQuark q
)
77 int64_t *valuep
= g_new(int64_t, 1);
79 g_hash_table_insert(enum_class
->table
.value_to_quark
, valuep
,
80 (gpointer
) (unsigned long) q
);
81 g_hash_table_insert(enum_class
->table
.quark_to_value
,
82 (gpointer
) (unsigned long) q
,
86 void enum_unsigned_insert(struct type_class_enum
*enum_class
, uint64_t v
, GQuark q
)
88 uint64_t *valuep
= g_new(uint64_t, 1);
90 g_hash_table_insert(enum_class
->table
.value_to_quark
, valuep
,
91 (gpointer
) (unsigned long) q
);
92 g_hash_table_insert(enum_class
->table
.quark_to_value
,
93 (gpointer
) (unsigned long) q
,
96 #else /* __WORDSIZE != 32 */
97 GQuark
enum_uint_to_quark(const struct type_class_enum
*enum_class
, uint64_t v
)
99 gconstpointer q
= g_hash_table_lookup(enum_class
->table
.value_to_quark
,
101 return (GQuark
) (unsigned long) q
;
104 GQuark
enum_int_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 uint64_t enum_quark_to_uint(const struct type_class_enum
*enum_class
,
114 gconstpointer v
= g_hash_table_lookup(enum_class
->table
.quark_to_value
,
115 (gconstpointer
) (unsigned long) q
);
116 return *(const uint64_t *) v
;
119 int64_t enum_quark_to_int(const struct type_class_enum
*enum_class
,
122 gconstpointer v
= g_hash_table_lookup(enum_class
->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 type_class_enum
*enum_class
,
144 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
145 (gpointer
) (unsigned long) q
);
146 g_hash_table_insert(enum_class
->table
.quark_to_value
,
147 (gpointer
) (unsigned long) q
,
151 void enum_unsigned_insert(struct type_class_enum
*enum_class
,
152 uint64_t v
, GQuark q
)
154 g_hash_table_insert(enum_class
->table
.value_to_quark
, (gpointer
) v
,
155 (gpointer
) (unsigned long) q
);
156 g_hash_table_insert(enum_class
->table
.quark_to_value
,
157 (gpointer
) (unsigned long) q
,
160 #endif /* __WORDSIZE != 32 */
162 void enum_copy(struct stream_pos
*dest
, const struct format
*fdest
,
163 struct stream_pos
*src
, const struct format
*fsrc
,
164 const struct type_class
*type_class
)
166 struct type_class_enum
*enum_class
=
167 container_of(type_class
, struct type_class_enum
, p
.p
);
168 struct type_class_integer
*int_class
= &enum_class
->p
;
171 v
= fsrc
->enum_read(src
, enum_class
);
172 return fdest
->enum_write(dest
, enum_class
, v
);
175 void enum_type_free(struct type_class_enum
*enum_class
)
177 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
178 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
183 void _enum_type_free(struct type_class
*type_class
)
185 struct type_class_enum
*enum_class
=
186 container_of(type_class
, struct type_class_enum
, p
.p
);
187 enum_type_free(enum_class
);
190 struct type_class_enum
*enum_type_new(const char *name
,
191 size_t len
, int byte_order
,
195 struct type_class_enum
*enum_class
;
196 struct type_class_integer
*int_class
;
199 enum_class
= g_new(struct type_class_enum
, 1);
200 enum_class
->table
.value_to_quark
= g_hash_table_new(enum_val_hash
,
202 enum_class
->table
.quark_to_value
= g_hash_table_new_full(g_direct_hash
,
204 NULL
, enum_val_free
);
205 int_class
= &enum_class
->p
;
206 int_class
->p
.name
= g_quark_from_string(name
);
207 int_class
->p
.alignment
= alignment
;
208 int_class
->p
.copy
= enum_copy
;
209 int_class
->p
.free
= _enum_type_free
;
210 int_class
->p
.ref
= 1;
211 int_class
->len
= len
;
212 int_class
->byte_order
= byte_order
;
213 int_class
->signedness
= signedness
;
214 if (int_class
->p
.name
) {
215 ret
= register_type(&int_class
->p
);
217 g_hash_table_destroy(enum_class
->table
.value_to_quark
);
218 g_hash_table_destroy(enum_class
->table
.quark_to_value
);
This page took 0.034337 seconds and 4 git commands to generate.