From a0a49e530b269679695daa6014bfade0b36970e1 Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Wed, 4 Dec 2013 17:14:49 +0100 Subject: libfshelp: use a hash table in get-identity.c Currently fshelp_get_identity uses ports_class_iterate to de-duplicate the identity ports. Use a hash table instead. * libfshelp/get-identity.c (struct idspec): Remove field fileno. (struct idspec): Add field id_hashloc. (id_clean): New function. (id_initialize): Use id_clean as cleanup function for idclass. (fshelp_get_identity): Use a hash table to de-duplicate the identity ports. --- libfshelp/get-identity.c | 66 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/libfshelp/get-identity.c b/libfshelp/get-identity.c index 946b7e8e..2dbd254c 100644 --- a/libfshelp/get-identity.c +++ b/libfshelp/get-identity.c @@ -21,6 +21,8 @@ #include #include +#include +#include #include static struct port_class *idclass = 0; @@ -29,14 +31,26 @@ static pthread_mutex_t idlock = PTHREAD_MUTEX_INITIALIZER; struct idspec { struct port_info pi; - ino_t fileno; + hurd_ihash_locp_t id_hashloc; }; +static struct hurd_ihash idhash + = HURD_IHASH_INITIALIZER (offsetof (struct idspec, id_hashloc)); + +static void +id_clean (void *cookie) +{ + struct idspec *i = cookie; + pthread_mutex_lock (&idlock); + hurd_ihash_locp_remove (&idhash, i->id_hashloc); + pthread_mutex_unlock (&idlock); +} + static void id_initialize () { assert (!idclass); - idclass = ports_create_class (0, 0); + idclass = ports_create_class (id_clean, NULL); } error_t @@ -47,42 +61,32 @@ fshelp_get_identity (struct port_bucket *bucket, struct idspec *i; error_t err = 0; - error_t check_port (void *arg) - { - struct idspec *i = arg; - if (i->fileno == fileno) - { - *pt = ports_get_right (i); - return 1; - } - else - return 0; - } - pthread_mutex_lock (&idlock); if (!idclass) id_initialize (); - *pt = MACH_PORT_NULL; - - ports_class_iterate (idclass, check_port); - - if (*pt != MACH_PORT_NULL) + i = hurd_ihash_find (&idhash, (hurd_ihash_key_t) fileno); + if (i == NULL) { - pthread_mutex_unlock (&idlock); - return 0; + err = ports_create_port (idclass, bucket, sizeof (struct idspec), &i); + if (err) + goto lose; + err = hurd_ihash_add (&idhash, (hurd_ihash_key_t) fileno, i); + if (err) + goto lose_port; + + *pt = ports_get_right (i); + ports_port_deref (i); } + else + *pt = ports_get_right (i); - err = ports_create_port (idclass, bucket, sizeof (struct idspec), &i); - if (err) - { - pthread_mutex_unlock (&idlock); - return err; - } - i->fileno = fileno; + /* Success! */ + goto lose; - *pt = ports_get_right (i); - ports_port_deref (i); + lose_port: + ports_destroy_right (i); + lose: pthread_mutex_unlock (&idlock); - return 0; + return err; } -- cgit v1.2.3