4 * BabelTrace - Enumeration Type
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
27 struct definition
*_enum_definition_new(struct declaration
*declaration
,
28 struct definition_scope
*parent_scope
,
29 GQuark field_name
, int index
,
30 const char *root_name
);
32 void _enum_definition_free(struct definition
*definition
);
35 void enum_range_set_free(void *ptr
)
40 #if (__WORDSIZE == 32)
42 gpointer
get_uint_v(uint64_t *v
)
48 gpointer
get_int_v(int64_t *v
)
54 guint
enum_val_hash(gconstpointer key
)
56 int64_t ukey
= *(const int64_t *)key
;
58 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
;
71 void enum_val_free(void *ptr
)
75 #else /* __WORDSIZE != 32 */
77 gpointer
get_uint_v(uint64_t *v
)
83 gpointer
get_int_v(int64_t *v
)
89 guint
enum_val_hash(gconstpointer key
)
91 return g_direct_hash(key
);
95 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
97 return g_direct_equal(a
, b
);
101 void enum_val_free(void *ptr
)
104 #endif /* __WORDSIZE != 32 */
107 * Returns a GArray or NULL.
108 * Caller must release the GArray with g_array_unref().
110 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
113 struct enum_range_to_quark
*iter
;
114 GArray
*qs
, *ranges
= NULL
;
116 /* Single values lookup */
117 qs
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
121 cds_list_for_each_entry(iter
, &enum_declaration
->table
.range_to_quark
, node
) {
122 if (iter
->range
.start
._unsigned
> v
|| iter
->range
.end
._unsigned
< v
)
129 ranges
= g_array_sized_new(FALSE
, TRUE
,
132 g_array_set_size(ranges
, qs_len
+ 1);
134 memcpy(ranges
->data
, qs
->data
,
135 sizeof(GQuark
) * qs_len
);
136 g_array_index(ranges
, GQuark
, qs_len
) = iter
->quark
;
138 size_t qs_len
= ranges
->len
;
140 g_array_set_size(ranges
, qs_len
+ 1);
141 g_array_index(ranges
, GQuark
, qs_len
) = iter
->quark
;
154 * Returns a GArray or NULL.
155 * Caller must release the GArray with g_array_unref().
157 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
160 struct enum_range_to_quark
*iter
;
161 GArray
*qs
, *ranges
= NULL
;
163 /* Single values lookup */
164 qs
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
168 cds_list_for_each_entry(iter
, &enum_declaration
->table
.range_to_quark
, node
) {
169 if (iter
->range
.start
._signed
> v
|| iter
->range
.end
._signed
< v
)
176 ranges
= g_array_sized_new(FALSE
, TRUE
,
179 g_array_set_size(ranges
, qs_len
+ 1);
181 memcpy(ranges
->data
, qs
->data
,
182 sizeof(GQuark
) * qs_len
);
183 g_array_index(ranges
, GQuark
, qs_len
) = iter
->quark
;
185 size_t qs_len
= ranges
->len
;
187 g_array_set_size(ranges
, qs_len
+ 1);
188 g_array_index(ranges
, GQuark
, qs_len
) = iter
->quark
;
201 void enum_unsigned_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
202 uint64_t v
, GQuark q
)
207 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
210 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
211 g_array_set_size(array
, 1);
212 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
213 #if (__WORDSIZE == 32)
214 valuep
= g_new(uint64_t, 1);
216 #else /* __WORDSIZE != 32 */
217 valuep
= get_uint_v(&v
);
218 #endif /* __WORDSIZE != 32 */
219 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
, valuep
, array
);
221 g_array_set_size(array
, array
->len
+ 1);
222 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
227 void enum_signed_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
233 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
236 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
237 g_array_set_size(array
, 1);
238 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
239 #if (__WORDSIZE == 32)
240 valuep
= g_new(int64_t, 1);
242 #else /* __WORDSIZE != 32 */
243 valuep
= get_int_v(&v
);
244 #endif /* __WORDSIZE != 32 */
245 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
, valuep
, array
);
247 g_array_set_size(array
, array
->len
+ 1);
248 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
252 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
255 return g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
256 (gconstpointer
) (unsigned long) q
);
260 void enum_signed_insert_range_to_quark(struct declaration_enum
*enum_declaration
,
261 int64_t start
, int64_t end
, GQuark q
)
263 struct enum_range_to_quark
*rtoq
;
265 rtoq
= g_new(struct enum_range_to_quark
, 1);
266 cds_list_add(&rtoq
->node
, &enum_declaration
->table
.range_to_quark
);
267 rtoq
->range
.start
._signed
= start
;
268 rtoq
->range
.end
._signed
= end
;
273 void enum_unsigned_insert_range_to_quark(struct declaration_enum
*enum_declaration
,
274 uint64_t start
, uint64_t end
, GQuark q
)
276 struct enum_range_to_quark
*rtoq
;
278 rtoq
= g_new(struct enum_range_to_quark
, 1);
279 cds_list_add(&rtoq
->node
, &enum_declaration
->table
.range_to_quark
);
280 rtoq
->range
.start
._unsigned
= start
;
281 rtoq
->range
.end
._unsigned
= end
;
285 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
286 int64_t start
, int64_t end
, GQuark q
)
289 struct enum_range
*range
;
292 enum_signed_insert_value_to_quark_set(enum_declaration
, start
, q
);
301 enum_signed_insert_range_to_quark(enum_declaration
, start
, end
, q
);
304 array
= g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
305 (gconstpointer
) (unsigned long) q
);
307 array
= g_array_sized_new(FALSE
, TRUE
,
308 sizeof(struct enum_range
), 1);
309 g_hash_table_insert(enum_declaration
->table
.quark_to_range_set
,
310 (gpointer
) (unsigned long) q
,
313 g_array_set_size(array
, array
->len
+ 1);
314 range
= &g_array_index(array
, struct enum_range
, array
->len
- 1);
315 range
->start
._signed
= start
;
316 range
->end
._signed
= end
;
319 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
320 uint64_t start
, uint64_t end
, GQuark q
)
323 struct enum_range
*range
;
327 enum_unsigned_insert_value_to_quark_set(enum_declaration
, start
, q
);
336 enum_unsigned_insert_range_to_quark(enum_declaration
, start
, end
, q
);
339 array
= g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
340 (gconstpointer
) (unsigned long) q
);
342 array
= g_array_sized_new(FALSE
, TRUE
,
343 sizeof(struct enum_range
), 1);
344 g_hash_table_insert(enum_declaration
->table
.quark_to_range_set
,
345 (gpointer
) (unsigned long) q
,
348 g_array_set_size(array
, array
->len
+ 1);
349 range
= &g_array_index(array
, struct enum_range
, array
->len
- 1);
350 range
->start
._unsigned
= start
;
351 range
->end
._unsigned
= end
;
354 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
)
356 return g_hash_table_size(enum_declaration
->table
.quark_to_range_set
);
360 void _enum_declaration_free(struct declaration
*declaration
)
362 struct declaration_enum
*enum_declaration
=
363 container_of(declaration
, struct declaration_enum
, p
);
364 struct enum_range_to_quark
*iter
, *tmp
;
366 g_hash_table_destroy(enum_declaration
->table
.value_to_quark_set
);
367 cds_list_for_each_entry_safe(iter
, tmp
, &enum_declaration
->table
.range_to_quark
, node
) {
368 cds_list_del(&iter
->node
);
371 g_hash_table_destroy(enum_declaration
->table
.quark_to_range_set
);
372 declaration_unref(&enum_declaration
->integer_declaration
->p
);
373 g_free(enum_declaration
);
376 struct declaration_enum
*
377 enum_declaration_new(struct declaration_integer
*integer_declaration
)
379 struct declaration_enum
*enum_declaration
;
381 enum_declaration
= g_new(struct declaration_enum
, 1);
383 enum_declaration
->table
.value_to_quark_set
= g_hash_table_new_full(enum_val_hash
,
386 enum_range_set_free
);
387 CDS_INIT_LIST_HEAD(&enum_declaration
->table
.range_to_quark
);
388 enum_declaration
->table
.quark_to_range_set
= g_hash_table_new_full(g_direct_hash
,
390 NULL
, enum_range_set_free
);
391 declaration_ref(&integer_declaration
->p
);
392 enum_declaration
->integer_declaration
= integer_declaration
;
393 enum_declaration
->p
.id
= CTF_TYPE_ENUM
;
394 enum_declaration
->p
.alignment
= 1;
395 enum_declaration
->p
.declaration_free
= _enum_declaration_free
;
396 enum_declaration
->p
.definition_new
= _enum_definition_new
;
397 enum_declaration
->p
.definition_free
= _enum_definition_free
;
398 enum_declaration
->p
.ref
= 1;
399 return enum_declaration
;
404 _enum_definition_new(struct declaration
*declaration
,
405 struct definition_scope
*parent_scope
,
406 GQuark field_name
, int index
,
407 const char *root_name
)
409 struct declaration_enum
*enum_declaration
=
410 container_of(declaration
, struct declaration_enum
, p
);
411 struct definition_enum
*_enum
;
412 struct definition
*definition_integer_parent
;
415 _enum
= g_new(struct definition_enum
, 1);
416 declaration_ref(&enum_declaration
->p
);
417 _enum
->p
.declaration
= declaration
;
418 _enum
->declaration
= enum_declaration
;
421 * Use INT_MAX order to ensure that all fields of the parent
422 * scope are seen as being prior to this scope.
424 _enum
->p
.index
= root_name
? INT_MAX
: index
;
425 _enum
->p
.name
= field_name
;
426 _enum
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
427 _enum
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
429 ret
= register_field_definition(field_name
, &_enum
->p
,
432 definition_integer_parent
=
433 enum_declaration
->integer_declaration
->p
.definition_new(&enum_declaration
->integer_declaration
->p
,
435 g_quark_from_static_string("container"), 0, NULL
);
436 _enum
->integer
= container_of(definition_integer_parent
,
437 struct definition_integer
, p
);
442 void _enum_definition_free(struct definition
*definition
)
444 struct definition_enum
*_enum
=
445 container_of(definition
, struct definition_enum
, p
);
447 definition_unref(&_enum
->integer
->p
);
448 free_definition_scope(_enum
->p
.scope
);
449 declaration_unref(_enum
->p
.declaration
);
451 g_array_unref(_enum
->value
);