From 8667b2f7756b42a570ef1328d3f4658da7dffb3f Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 3 Nov 2011 14:32:31 +0100 Subject: gck: Add gck_attribute_hash and cleanup for use in hash table * Add gck_attribute_hash() so GckAttribute can be used as a hash table key. * Change gck_attribute_free() and gck_attribute_equal() to use pointers so they can be used as GEqualFunc and GDestroyNotify without casting --- docs/reference/gck/gck-sections.txt | 1 + gck/gck-attributes.c | 47 ++++++++++++++++++++++++++++--------- gck/gck.h | 8 ++++--- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/docs/reference/gck/gck-sections.txt b/docs/reference/gck/gck-sections.txt index da8cab5..266d286 100644 --- a/docs/reference/gck/gck-sections.txt +++ b/docs/reference/gck/gck-sections.txt @@ -26,6 +26,7 @@ gck_attribute_clear gck_attribute_free gck_attribute_dump gck_attribute_equal +gck_attribute_hash GCK_TYPE_ATTRIBUTE gck_attribute_get_type diff --git a/gck/gck-attributes.c b/gck/gck-attributes.c index cb48ca1..6f3b2ec 100644 --- a/gck/gck-attributes.c +++ b/gck/gck-attributes.c @@ -641,39 +641,41 @@ gck_attribute_clear (GckAttribute *attr) /** * gck_attribute_free: - * @attr: Attribute to free. + * @attr: (type Gck.Attribute): attribute to free * * Free an attribute and its allocated memory. These is usually * used with attributes that are allocated by gck_attribute_new() * or a similar function. **/ void -gck_attribute_free (GckAttribute *attr) +gck_attribute_free (gpointer attr) { + GckAttribute *a = attr; if (attr) { - attribute_clear (attr, g_realloc); - g_slice_free (GckAttribute, attr); + attribute_clear (a, g_realloc); + g_slice_free (GckAttribute, a); } } /** * gck_attribute_equal: - * @a: (type Gck.Attribute): first attribute to compare - * @b: (type Gck.Attribute): second attribute to compare + * @attr1: (type Gck.Attribute): first attribute to compare + * @attr2: (type Gck.Attribute): second attribute to compare * * Compare two attributes. Useful with GHashTable. * * Returns: %TRUE if the attributes are equal. */ gboolean -gck_attribute_equal (gconstpointer a, gconstpointer b) +gck_attribute_equal (gconstpointer attr1, + gconstpointer attr2) { - const GckAttribute *aa = a; - const GckAttribute *ab = b; + const GckAttribute *aa = attr1; + const GckAttribute *ab = attr2; - if (!a && !b) + if (!aa && !ab) return TRUE; - if (!a || !b) + if (!aa || !ab) return FALSE; if (aa->type != ab->type) @@ -687,6 +689,29 @@ gck_attribute_equal (gconstpointer a, gconstpointer b) return memcmp (aa->value, ab->value, aa->length) == 0; } +/** + * gck_attribute_hash: + * @attr: (type Gck.Attribute): attribute to hash + * + * Hash an attribute for use in GHashTable keys. + * + * Returns: the hash code + */ +guint +gck_attribute_hash (gconstpointer attr) +{ + const GckAttribute *a = attr; + const signed char *p, *e; + guint32 h = 5381; + + h ^= _gck_ulong_hash (&a->type); + + for (p = (signed char *)a->value, e = p + a->length; p != e; p++) + h = (h << 5) + h + *p; + + return h; +} + /** * SECTION:gck-attributes * @title: GckAttributes diff --git a/gck/gck.h b/gck/gck.h index 42b16e2..e9dbf9b 100644 --- a/gck/gck.h +++ b/gck/gck.h @@ -174,14 +174,16 @@ gchar* gck_attribute_get_string (GckAttribute *attr) void gck_attribute_get_date (GckAttribute *attr, GDate* value); -gboolean gck_attribute_equal (gconstpointer a, - gconstpointer b); +gboolean gck_attribute_equal (gconstpointer attr1, + gconstpointer attr2); + +guint gck_attribute_hash (gconstpointer attr); GckAttribute* gck_attribute_dup (GckAttribute *attr); void gck_attribute_clear (GckAttribute *attr); -void gck_attribute_free (GckAttribute *attr); +void gck_attribute_free (gpointer attr); void gck_attribute_dump (GckAttribute *attr); -- cgit v1.2.1