From 299ed08a68d4f603bb72b7635bfa5c6f95776b22 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Wed, 31 Mar 2010 16:13:38 -0700 Subject: [PATCH] Fix big dumbness in glsl_type::get_array_instance hash_table_insert needs to keep the key so that it compare keys on a following hash_table_find call. Since key was allocated on the stack, it disappeared out from under the hash table. --- glsl_types.cpp | 29 ++++++++++++++++++++--------- glsl_types.h | 6 ------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/glsl_types.cpp b/glsl_types.cpp index eb9ab820b16..7b8b3e90142 100644 --- a/glsl_types.cpp +++ b/glsl_types.cpp @@ -576,29 +576,40 @@ glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns) int glsl_type::array_key_compare(const void *a, const void *b) { - const array_hash_key *const key1 = (array_hash_key *) a; - const array_hash_key *const key2 = (array_hash_key *) b; + const glsl_type *const key1 = (glsl_type *) a; + const glsl_type *const key2 = (glsl_type *) b; - return ((key1->type == key2->type) && (key1->size == key2->size)) ? 0 : 1; + /* Return zero is the types match (there is zero difference) or non-zero + * otherwise. + */ + return ((key1->fields.array == key2->fields.array) + && (key1->length == key2->length)) ? 0 : 1; } unsigned glsl_type::array_key_hash(const void *a) { - char buf[sizeof(array_hash_key) + 1]; + const glsl_type *const key = (glsl_type *) a; - memcpy(buf, a, sizeof(array_hash_key)); - buf[sizeof(array_hash_key)] = '\0'; + const struct { + const glsl_type *t; + unsigned l; + char nul; + } hash_key = { + key->fields.array, + key->length, + '\0' + }; - return hash_table_string_hash(buf); + return hash_table_string_hash(& hash_key); } const glsl_type * glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) { - array_hash_key key = { base, array_size }; + const glsl_type key(base, array_size); if (array_types == NULL) { array_types = hash_table_ctor(64, array_key_hash, array_key_compare); @@ -608,7 +619,7 @@ glsl_type::get_array_instance(const glsl_type *base, unsigned array_size) if (t == NULL) { t = new glsl_type(base, array_size); - hash_table_insert(array_types, (void *) t, & key); + hash_table_insert(array_types, (void *) t, t); } assert(t->base_type == GLSL_TYPE_ARRAY); diff --git a/glsl_types.h b/glsl_types.h index 23a04fbe9d0..32035f5a443 100644 --- a/glsl_types.h +++ b/glsl_types.h @@ -330,12 +330,6 @@ private: /** Hash table containing the known array types. */ static struct hash_table *array_types; - /** Structure defining the key type used for array_types hash table. */ - struct array_hash_key { - const glsl_type *type; - unsigned size; - }; - static int array_key_compare(const void *a, const void *b); static unsigned array_key_hash(const void *key); };