4 * BabelTrace - Enumeration Type
6 * Copyright 2010, 2011 - 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>
25 struct definition
*_enum_definition_new(struct declaration
*declaration
,
26 struct definition_scope
*parent_scope
,
27 GQuark field_name
, int index
,
28 const char *root_name
);
30 void _enum_definition_free(struct definition
*definition
);
33 void enum_range_set_free(void *ptr
)
39 * Returns a GArray or NULL.
40 * Caller must release the GArray with g_array_unref().
42 GArray
*enum_uint_to_quark_set(const struct declaration_enum
*enum_declaration
,
45 struct enum_range_to_quark
*iter
;
46 GArray
*qs
, *ranges
= NULL
;
48 /* Single values lookup */
49 qs
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
, &v
);
52 cds_list_for_each_entry(iter
, &enum_declaration
->table
.range_to_quark
, node
) {
53 if (iter
->range
.start
._unsigned
> v
|| iter
->range
.end
._unsigned
< v
)
60 ranges
= g_array_sized_new(FALSE
, TRUE
,
61 sizeof(struct enum_range
),
63 g_array_set_size(ranges
, qs_len
+ 1);
65 memcpy(ranges
->data
, qs
->data
,
66 sizeof(struct enum_range
) * qs_len
);
67 g_array_index(ranges
, struct enum_range
, qs_len
) = iter
->range
;
69 g_array_set_size(ranges
, ranges
->len
+ 1);
70 g_array_index(ranges
, struct enum_range
, ranges
->len
) = iter
->range
;
81 * Returns a GArray or NULL.
82 * Caller must release the GArray with g_array_unref().
84 GArray
*enum_int_to_quark_set(const struct declaration_enum
*enum_declaration
,
87 struct enum_range_to_quark
*iter
;
88 GArray
*qs
, *ranges
= NULL
;
90 /* Single values lookup */
91 qs
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
, &v
);
94 cds_list_for_each_entry(iter
, &enum_declaration
->table
.range_to_quark
, node
) {
95 if (iter
->range
.start
._signed
> v
|| iter
->range
.end
._signed
< v
)
102 ranges
= g_array_sized_new(FALSE
, TRUE
,
103 sizeof(struct enum_range
),
105 g_array_set_size(ranges
, qs_len
+ 1);
107 memcpy(ranges
->data
, qs
->data
,
108 sizeof(struct enum_range
) * qs_len
);
109 g_array_index(ranges
, struct enum_range
, qs_len
) = iter
->range
;
111 g_array_set_size(ranges
, ranges
->len
+ 1);
112 g_array_index(ranges
, struct enum_range
, ranges
->len
) = iter
->range
;
122 #if (__WORDSIZE == 32)
124 guint
enum_val_hash(gconstpointer key
)
126 int64_t ukey
= *(const int64_t *)key
;
128 return (guint
)ukey
^ (guint
)(ukey
>> 32);
132 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
134 int64_t ua
= *(const int64_t *)a
;
135 int64_t ub
= *(const int64_t *)b
;
141 void enum_val_free(void *ptr
)
147 void enum_signed_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
153 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
, &v
);
155 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
156 g_array_set_size(array
, 1);
157 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
158 valuep
= g_new(int64_t, 1);
160 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
, valuep
, array
);
162 g_array_set_size(array
, array
->len
+ 1);
163 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
168 void enum_unsigned_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
169 uint64_t v
, GQuark q
)
174 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
, &v
);
176 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
177 g_array_set_size(array
, 1);
178 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
179 valuep
= g_new(uint64_t, 1);
181 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
, valuep
, array
);
183 g_array_set_size(array
, array
->len
+ 1);
184 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
187 #else /* __WORDSIZE != 32 */
189 guint
enum_val_hash(gconstpointer key
)
191 return g_direct_hash(key
);
195 gboolean
enum_val_equal(gconstpointer a
, gconstpointer b
)
197 return g_direct_equal(a
, b
);
201 void enum_val_free(void *ptr
)
206 void enum_signed_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
211 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
214 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
215 g_array_set_size(array
, 1);
216 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
217 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
,
218 (gpointer
) v
, array
);
220 g_array_set_size(array
, array
->len
+ 1);
221 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
226 void enum_unsigned_insert_value_to_quark_set(struct declaration_enum
*enum_declaration
,
227 uint64_t v
, GQuark q
)
231 array
= g_hash_table_lookup(enum_declaration
->table
.value_to_quark_set
,
234 array
= g_array_sized_new(FALSE
, TRUE
, sizeof(GQuark
), 1);
235 g_array_set_size(array
, 1);
236 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
237 g_hash_table_insert(enum_declaration
->table
.value_to_quark_set
,
238 (gpointer
) v
, array
);
240 g_array_set_size(array
, array
->len
+ 1);
241 g_array_index(array
, GQuark
, array
->len
- 1) = q
;
244 #endif /* __WORDSIZE != 32 */
246 GArray
*enum_quark_to_range_set(const struct declaration_enum
*enum_declaration
,
249 return g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
250 (gconstpointer
) (unsigned long) q
);
254 void enum_signed_insert_range_to_quark(struct declaration_enum
*enum_declaration
,
255 int64_t start
, int64_t end
, GQuark q
)
257 struct enum_range_to_quark
*rtoq
;
259 rtoq
= g_new(struct enum_range_to_quark
, 1);
260 cds_list_add(&rtoq
->node
, &enum_declaration
->table
.range_to_quark
);
261 rtoq
->range
.start
._signed
= start
;
262 rtoq
->range
.end
._signed
= end
;
267 void enum_unsigned_insert_range_to_quark(struct declaration_enum
*enum_declaration
,
268 uint64_t start
, uint64_t end
, GQuark q
)
270 struct enum_range_to_quark
*rtoq
;
272 rtoq
= g_new(struct enum_range_to_quark
, 1);
273 cds_list_add(&rtoq
->node
, &enum_declaration
->table
.range_to_quark
);
274 rtoq
->range
.start
._unsigned
= start
;
275 rtoq
->range
.end
._unsigned
= end
;
279 void enum_signed_insert(struct declaration_enum
*enum_declaration
,
280 int64_t start
, int64_t end
, GQuark q
)
283 struct enum_range
*range
;
286 enum_signed_insert_value_to_quark_set(enum_declaration
, start
, q
);
295 enum_signed_insert_range_to_quark(enum_declaration
, start
, end
, q
);
298 array
= g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
299 (gconstpointer
) (unsigned long) q
);
301 array
= g_array_sized_new(FALSE
, TRUE
,
302 sizeof(struct enum_range
), 1);
303 g_hash_table_insert(enum_declaration
->table
.quark_to_range_set
,
304 (gpointer
) (unsigned long) q
,
307 g_array_set_size(array
, array
->len
+ 1);
308 range
= &g_array_index(array
, struct enum_range
, array
->len
- 1);
309 range
->start
._signed
= start
;
310 range
->end
._signed
= end
;
313 void enum_unsigned_insert(struct declaration_enum
*enum_declaration
,
314 uint64_t start
, uint64_t end
, GQuark q
)
317 struct enum_range
*range
;
321 enum_unsigned_insert_value_to_quark_set(enum_declaration
, start
, q
);
330 enum_unsigned_insert_range_to_quark(enum_declaration
, start
, end
, q
);
333 array
= g_hash_table_lookup(enum_declaration
->table
.quark_to_range_set
,
334 (gconstpointer
) (unsigned long) q
);
336 array
= g_array_sized_new(FALSE
, TRUE
,
337 sizeof(struct enum_range
), 1);
338 g_hash_table_insert(enum_declaration
->table
.quark_to_range_set
,
339 (gpointer
) (unsigned long) q
,
342 g_array_set_size(array
, array
->len
+ 1);
343 range
= &g_array_index(array
, struct enum_range
, array
->len
- 1);
344 range
->start
._unsigned
= start
;
345 range
->end
._unsigned
= end
;
348 size_t enum_get_nr_enumerators(struct declaration_enum
*enum_declaration
)
350 return g_hash_table_size(enum_declaration
->table
.quark_to_range_set
);
354 void _enum_declaration_free(struct declaration
*declaration
)
356 struct declaration_enum
*enum_declaration
=
357 container_of(declaration
, struct declaration_enum
, p
);
358 struct enum_range_to_quark
*iter
, *tmp
;
360 g_hash_table_destroy(enum_declaration
->table
.value_to_quark_set
);
361 cds_list_for_each_entry_safe(iter
, tmp
, &enum_declaration
->table
.range_to_quark
, node
) {
362 cds_list_del(&iter
->node
);
365 g_hash_table_destroy(enum_declaration
->table
.quark_to_range_set
);
366 declaration_unref(&enum_declaration
->integer_declaration
->p
);
367 g_free(enum_declaration
);
370 struct declaration_enum
*
371 enum_declaration_new(struct declaration_integer
*integer_declaration
)
373 struct declaration_enum
*enum_declaration
;
375 enum_declaration
= g_new(struct declaration_enum
, 1);
377 enum_declaration
->table
.value_to_quark_set
= g_hash_table_new_full(enum_val_hash
,
380 enum_range_set_free
);
381 CDS_INIT_LIST_HEAD(&enum_declaration
->table
.range_to_quark
);
382 enum_declaration
->table
.quark_to_range_set
= g_hash_table_new_full(g_direct_hash
,
384 NULL
, enum_range_set_free
);
385 declaration_ref(&integer_declaration
->p
);
386 enum_declaration
->integer_declaration
= integer_declaration
;
387 enum_declaration
->p
.id
= CTF_TYPE_ENUM
;
388 enum_declaration
->p
.alignment
= 1;
389 enum_declaration
->p
.declaration_free
= _enum_declaration_free
;
390 enum_declaration
->p
.definition_new
= _enum_definition_new
;
391 enum_declaration
->p
.definition_free
= _enum_definition_free
;
392 enum_declaration
->p
.ref
= 1;
393 return enum_declaration
;
398 _enum_definition_new(struct declaration
*declaration
,
399 struct definition_scope
*parent_scope
,
400 GQuark field_name
, int index
,
401 const char *root_name
)
403 struct declaration_enum
*enum_declaration
=
404 container_of(declaration
, struct declaration_enum
, p
);
405 struct definition_enum
*_enum
;
406 struct definition
*definition_integer_parent
;
409 _enum
= g_new(struct definition_enum
, 1);
410 declaration_ref(&enum_declaration
->p
);
411 _enum
->p
.declaration
= declaration
;
412 _enum
->declaration
= enum_declaration
;
415 * Use INT_MAX order to ensure that all fields of the parent
416 * scope are seen as being prior to this scope.
418 _enum
->p
.index
= root_name
? INT_MAX
: index
;
419 _enum
->p
.name
= field_name
;
420 _enum
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
421 _enum
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
423 ret
= register_field_definition(field_name
, &_enum
->p
,
426 definition_integer_parent
=
427 enum_declaration
->integer_declaration
->p
.definition_new(&enum_declaration
->integer_declaration
->p
,
429 g_quark_from_static_string("container"), 0, NULL
);
430 _enum
->integer
= container_of(definition_integer_parent
,
431 struct definition_integer
, p
);
436 void _enum_definition_free(struct definition
*definition
)
438 struct definition_enum
*_enum
=
439 container_of(definition
, struct definition_enum
, p
);
441 definition_unref(&_enum
->integer
->p
);
442 free_definition_scope(_enum
->p
.scope
);
443 declaration_unref(_enum
->p
.declaration
);
445 g_array_unref(_enum
->value
);