[Mesa-dev] [PATCH 1/2] util: Implement a hash table cloning function

Emil Velikov emil.l.velikov at gmail.com
Wed Mar 14 11:43:21 UTC 2018


On 13 March 2018 at 20:04, Thomas Helland <thomashelland90 at gmail.com> wrote:
> 2018-03-12 19:48 GMT+01:00 Emil Velikov <emil.l.velikov at gmail.com>:
>> Hi Thomas,
>>
>> On 12 March 2018 at 17:55, Thomas Helland <thomashelland90 at gmail.com> wrote:
>>> V2: Don't rzalloc; we are about to rewrite the whole thing (Vladislav)
>>> ---
>>>  src/util/hash_table.c | 22 ++++++++++++++++++++++
>>>  src/util/hash_table.h |  2 ++
>>>  2 files changed, 24 insertions(+)
>>>
>>> diff --git a/src/util/hash_table.c b/src/util/hash_table.c
>>> index b7421a0144..f8d5d0f88a 100644
>>> --- a/src/util/hash_table.c
>>> +++ b/src/util/hash_table.c
>>> @@ -141,6 +141,28 @@ _mesa_hash_table_create(void *mem_ctx,
>>>     return ht;
>>>  }
>>>
>>> +struct hash_table *
>>> +_mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx)
>>> +{
>>> +   struct hash_table *ht;
>>> +
>>> +   ht = ralloc(dst_mem_ctx, struct hash_table);
>>> +   if (ht == NULL)
>>> +      return NULL;
>>> +
>>> +   memcpy(ht, src, sizeof(struct hash_table));
>>> +
>>> +   ht->table = ralloc_array(ht, struct hash_entry, ht->size);
>>> +   if (ht->table == NULL) {
>>> +      ralloc_free(ht);
>>> +      return NULL;
>>> +   }
>>> +
>>> +   memcpy(ht->table, src->table, ht->size * sizeof(struct hash_entry));
>>> +
>> Thinking out loud:
>>
>> I'm wondering if it won't make sense to reuse _mesa_hash_table_create,
>> instead of open-coding it?
>>
>> -Emil
>
> That wont work like you might expect. The hash table will then be initialized
> to the wrong size. We want an exact copy, so we have to make sure we copy
> also things like size, deleted_entries, etc. If we don't we can not memcpy
> the array of entries, defeating the main purpose which is to avoid all the
> insertions into the hash table,
>
Yes, invoking it directly won't work. The various macro tricks used in
the kernel came to mind.
It does seem like an overkill, though.

v2 of the series looks good
Reviewed-by: Emil Velikov <emil.velikov at collabora.com>

-Emil


More information about the mesa-dev mailing list