[libvirt] [PATCH v2] util: Simplify hash implementation

Eric Blake eblake at redhat.com
Fri Apr 29 18:43:50 UTC 2011


On 04/12/2011 11:25 AM, Jiri Denemark wrote:
> So far first entries for each hash key are stored directly in the hash
> table while other entries mapped to the same key are linked through
> pointers. As a result of that, the code is cluttered with special
> handling for the first items.
> 
> This patch makes all entries (even the first ones) linked through
> pointers, which significantly simplifies the code and makes it more
> maintainable.
> @@ -632,44 +539,27 @@ int virHashRemoveSet(virHashTablePtr table, virHashSearcher iter, const void *da
>      table->iterating = true;
>      table->current = NULL;
>      for (i = 0 ; i < table->size ; i++) {
> -        virHashEntryPtr prev = NULL;
> -        virHashEntryPtr entry = &(table->table[i]);
> +        virHashEntryPtr *nextptr = table->table + i;
>  
> -        while (entry && entry->valid) {
> -            if (iter(entry->payload, entry->name, data)) {
> +        while (*nextptr) {
> +            virHashEntryPtr entry = *nextptr;
> +            if (!iter(entry->payload, entry->name, data)) {
> +                *nextptr = entry->next;
> +            } else {
>                  count++;
>                  if (table->dataFree)
>                      table->dataFree(entry->payload, entry->name);
>                  if (table->keyFree)
>                      table->keyFree(entry->name);
> +                *nextptr = entry->next;

Oops.  We should first be updating *nextptr->next before advancing
nextptr.  Without that, we're corrupting the table.  I caught this by
running valgrind on hashtest.c; I'm working on a patch now.

Also, virHashFree is leaking table->table.

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20110429/b992ba64/attachment-0001.sig>


More information about the libvir-list mailing list