[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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 redhat com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]