X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=types%2Fenum.c;h=f096d5b6dc4081626087094598bc1f59f2cbb1d9;hp=b60c9c56ce239a08dcaa23ffbbf4fb4ca70ee1af;hb=64fa3fec6c28f1d077812b4bfa06ae73b0f5999d;hpb=98df1c9fb24d5e7e10bf628692011e130e0d8339 diff --git a/types/enum.c b/types/enum.c index b60c9c56..f096d5b6 100644 --- a/types/enum.c +++ b/types/enum.c @@ -3,7 +3,9 @@ * * BabelTrace - Enumeration Type * - * Copyright 2010, 2011 - Mathieu Desnoyers + * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation + * + * Author: Mathieu Desnoyers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -35,6 +37,72 @@ void enum_range_set_free(void *ptr) g_array_unref(ptr); } +#if (__WORDSIZE == 32) +static inline +gpointer get_uint_v(uint64_t *v) +{ + return v; +} + +static inline +gpointer get_int_v(int64_t *v) +{ + return v; +} + +static +guint enum_val_hash(gconstpointer key) +{ + int64_t ukey = *(const int64_t *)key; + + return (guint)ukey ^ (guint)(ukey >> 32); +} + +static +gboolean enum_val_equal(gconstpointer a, gconstpointer b) +{ + int64_t ua = *(const int64_t *)a; + int64_t ub = *(const int64_t *)b; + + return ua == ub; +} + +static +void enum_val_free(void *ptr) +{ + g_free(ptr); +} +#else /* __WORDSIZE != 32 */ +static inline +gpointer get_uint_v(uint64_t *v) +{ + return (gpointer) *v; +} + +static inline +gpointer get_int_v(int64_t *v) +{ + return (gpointer) *v; +} + +static +guint enum_val_hash(gconstpointer key) +{ + return g_direct_hash(key); +} + +static +gboolean enum_val_equal(gconstpointer a, gconstpointer b) +{ + return g_direct_equal(a, b); +} + +static +void enum_val_free(void *ptr) +{ +} +#endif /* __WORDSIZE != 32 */ + /* * Returns a GArray or NULL. * Caller must release the GArray with g_array_unref(). @@ -46,7 +114,8 @@ GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration, GArray *qs, *ranges = NULL; /* Single values lookup */ - qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v); + qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, + get_uint_v(&v)); /* Range lookup */ cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) { @@ -58,19 +127,23 @@ GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration, if (qs) qs_len = qs->len; ranges = g_array_sized_new(FALSE, TRUE, - sizeof(struct enum_range), + sizeof(GQuark), qs_len + 1); g_array_set_size(ranges, qs_len + 1); if (qs) memcpy(ranges->data, qs->data, - sizeof(struct enum_range) * qs_len); - g_array_index(ranges, struct enum_range, qs_len) = iter->range; + sizeof(GQuark) * qs_len); + g_array_index(ranges, GQuark, qs_len) = iter->quark; } else { - g_array_set_size(ranges, ranges->len + 1); - g_array_index(ranges, struct enum_range, ranges->len) = iter->range; + size_t qs_len = ranges->len; + + g_array_set_size(ranges, qs_len + 1); + g_array_index(ranges, GQuark, qs_len) = iter->quark; } } if (!ranges) { + if (!qs) + return NULL; ranges = qs; g_array_ref(ranges); } @@ -82,13 +155,14 @@ GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration, * Caller must release the GArray with g_array_unref(). */ GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration, - uint64_t v) + int64_t v) { struct enum_range_to_quark *iter; GArray *qs, *ranges = NULL; /* Single values lookup */ - qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v); + qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, + get_int_v(&v)); /* Range lookup */ cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) { @@ -100,70 +174,29 @@ GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration, if (qs) qs_len = qs->len; ranges = g_array_sized_new(FALSE, TRUE, - sizeof(struct enum_range), + sizeof(GQuark), qs_len + 1); g_array_set_size(ranges, qs_len + 1); if (qs) memcpy(ranges->data, qs->data, - sizeof(struct enum_range) * qs_len); - g_array_index(ranges, struct enum_range, qs_len) = iter->range; + sizeof(GQuark) * qs_len); + g_array_index(ranges, GQuark, qs_len) = iter->quark; } else { - g_array_set_size(ranges, ranges->len + 1); - g_array_index(ranges, struct enum_range, ranges->len) = iter->range; + size_t qs_len = ranges->len; + + g_array_set_size(ranges, qs_len + 1); + g_array_index(ranges, GQuark, qs_len) = iter->quark; } } if (!ranges) { + if (!qs) + return NULL; ranges = qs; g_array_ref(ranges); } return ranges; } -#if (__WORDSIZE == 32) -static -guint enum_val_hash(gconstpointer key) -{ - int64_t ukey = *(const int64_t *)key; - - return (guint)ukey ^ (guint)(ukey >> 32); -} - -static -gboolean enum_val_equal(gconstpointer a, gconstpointer b) -{ - int64_t ua = *(const int64_t *)a; - int64_t ub = *(const int64_t *)b; - - return ua == ub; -} - -static -void enum_val_free(void *ptr) -{ - g_free(ptr); -} - -static -void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration, - int64_t v, GQuark q) -{ - int64_t *valuep; - GArray *array; - - array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v); - if (!array) { - array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1); - g_array_set_size(array, 1); - g_array_index(array, GQuark, array->len - 1) = q; - valuep = g_new(int64_t, 1); - *valuep = v; - g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array); - } else { - g_array_set_size(array, array->len + 1); - g_array_index(array, GQuark, array->len - 1) = q; - } -} - static void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration, uint64_t v, GQuark q) @@ -171,77 +204,50 @@ void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_decla uint64_t *valuep; GArray *array; - array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v); + array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, + get_uint_v(&v)); if (!array) { array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1); g_array_set_size(array, 1); g_array_index(array, GQuark, array->len - 1) = q; +#if (__WORDSIZE == 32) valuep = g_new(uint64_t, 1); *valuep = v; +#else /* __WORDSIZE != 32 */ + valuep = get_uint_v(&v); +#endif /* __WORDSIZE != 32 */ g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array); } else { g_array_set_size(array, array->len + 1); g_array_index(array, GQuark, array->len - 1) = q; } } -#else /* __WORDSIZE != 32 */ -static -guint enum_val_hash(gconstpointer key) -{ - return g_direct_hash(key); -} - -static -gboolean enum_val_equal(gconstpointer a, gconstpointer b) -{ - return g_direct_equal(a, b); -} - -static -void enum_val_free(void *ptr) -{ -} static void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration, int64_t v, GQuark q) { + int64_t *valuep; GArray *array; array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, - (gconstpointer) v); - if (!array) { - array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1); - g_array_set_size(array, 1); - g_array_index(array, GQuark, array->len - 1) = q; - g_hash_table_insert(enum_declaration->table.value_to_quark_set, - (gpointer) v, array); - } else { - g_array_set_size(array, array->len + 1); - g_array_index(array, GQuark, array->len - 1) = q; - } -} - -static -void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration, - uint64_t v, GQuark q) -{ - GArray *array; - - array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, - (gconstpointer) v); + get_int_v(&v)); if (!array) { array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1); g_array_set_size(array, 1); g_array_index(array, GQuark, array->len - 1) = q; - g_hash_table_insert(enum_declaration->table.value_to_quark_set, - (gpointer) v, array); +#if (__WORDSIZE == 32) + valuep = g_new(int64_t, 1); + *valuep = v; +#else /* __WORDSIZE != 32 */ + valuep = get_int_v(&v); +#endif /* __WORDSIZE != 32 */ + g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array); } else { g_array_set_size(array, array->len + 1); g_array_index(array, GQuark, array->len - 1) = q; } } -#endif /* __WORDSIZE != 32 */ GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration, GQuark q) @@ -418,14 +424,14 @@ struct definition * _enum->p.index = root_name ? INT_MAX : index; _enum->p.name = field_name; _enum->p.path = new_definition_path(parent_scope, field_name, root_name); - _enum->scope = new_definition_scope(parent_scope, field_name, root_name); + _enum->p.scope = new_definition_scope(parent_scope, field_name, root_name); _enum->value = NULL; ret = register_field_definition(field_name, &_enum->p, parent_scope); assert(!ret); definition_integer_parent = enum_declaration->integer_declaration->p.definition_new(&enum_declaration->integer_declaration->p, - _enum->scope, + _enum->p.scope, g_quark_from_static_string("container"), 0, NULL); _enum->integer = container_of(definition_integer_parent, struct definition_integer, p); @@ -439,6 +445,7 @@ void _enum_definition_free(struct definition *definition) container_of(definition, struct definition_enum, p); definition_unref(&_enum->integer->p); + free_definition_scope(_enum->p.scope); declaration_unref(_enum->p.declaration); if (_enum->value) g_array_unref(_enum->value);