From e8bfd88f26b2db57ba6769810ec49069bd9d35b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Thu, 11 Jun 2009 13:16:15 +0100 Subject: [PATCH] gstvalue: don't put GTypes into int variables GTypes are not ints and as such are not guaranteed to fit into an int (with the exception of fundamental types), so we really shouldn't put them into int variables. Even if a rather unlikely obscure corner case, this has actually been a problem at some point in the past, see commit 99f16655f4cfbc8e06b5972417ba11279083a64e. --- gst/gstvalue.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/gst/gstvalue.c b/gst/gstvalue.c index e7a3c564d6..5d8cabce20 100644 --- a/gst/gstvalue.c +++ b/gst/gstvalue.c @@ -85,6 +85,18 @@ static gchar *gst_string_wrap (const gchar * s); static gchar *gst_string_take_and_wrap (gchar * s); static gchar *gst_string_unwrap (const gchar * s); +static inline GstValueTable * +gst_value_hash_lookup_type (GType type) +{ + return g_hash_table_lookup (gst_value_hash, (gpointer) type); +} + +static void +gst_value_hash_add_type (GType type, const GstValueTable * table) +{ + g_hash_table_insert (gst_value_hash, (gpointer) type, (gpointer) table); +} + /******** * list * ********/ @@ -2807,12 +2819,11 @@ gst_value_get_compare_func (const GValue * value1) GstValueTable *table, *best = NULL; guint i; GType type1; - gint key; - key = type1 = G_VALUE_TYPE (value1); + type1 = G_VALUE_TYPE (value1); /* this is a fast check */ - best = g_hash_table_lookup (gst_value_hash, GINT_TO_POINTER (key)); + best = gst_value_hash_lookup_type (type1); /* slower checks */ if (G_UNLIKELY (!best || !best->compare)) { @@ -3259,17 +3270,16 @@ void gst_value_register (const GstValueTable * table) { GstValueTable *found; - gint key; g_array_append_val (gst_value_table, *table); - key = table->type; - - found = g_hash_table_lookup (gst_value_hash, GINT_TO_POINTER (key)); + found = gst_value_hash_lookup_type (table->type); if (found) - g_warning ("adding type %d multiple times", key); + g_warning ("adding type %s multiple times", g_type_name (table->type)); - g_hash_table_insert (gst_value_hash, GINT_TO_POINTER (key), (gpointer) table); + /* FIXME: we're not really doing the const justice, we assume the table is + * static */ + gst_value_hash_add_type (table->type, table); } /** @@ -3304,13 +3314,12 @@ gst_value_serialize (const GValue * value) GstValueTable *table, *best; char *s; GType type; - gint key; g_return_val_if_fail (G_IS_VALUE (value), NULL); - key = type = G_VALUE_TYPE (value); + type = G_VALUE_TYPE (value); - best = g_hash_table_lookup (gst_value_hash, GINT_TO_POINTER (key)); + best = gst_value_hash_lookup_type (type); if (G_UNLIKELY (!best || !best->serialize)) { best = NULL; @@ -3352,15 +3361,13 @@ gst_value_deserialize (GValue * dest, const gchar * src) GstValueTable *table, *best; guint i; GType type; - gint key; g_return_val_if_fail (src != NULL, FALSE); g_return_val_if_fail (G_IS_VALUE (dest), FALSE); - key = type = G_VALUE_TYPE (dest); - - best = g_hash_table_lookup (gst_value_hash, GINT_TO_POINTER (key)); + type = G_VALUE_TYPE (dest); + best = gst_value_hash_lookup_type (type); if (G_UNLIKELY (!best || !best->deserialize)) { best = NULL; for (i = 0; i < gst_value_table->len; i++) {