[ovs-dev] [PATCH] poll-loop: Drop malloc() from every poll_block().

Russell Bryant rbryant at redhat.com
Tue Apr 14 13:34:00 UTC 2015


On 04/13/2015 04:16 PM, Jarno Rajahalme wrote:
> I’ll let Ben have his say on this, but see some comments below:
> 
>   Jarno
> 
>> On Apr 11, 2015, at 6:59 PM, Russell Bryant <rbryant at redhat.com> wrote:

>> @@ -329,18 +337,34 @@ poll_block(void)
>>     }
>>
>>     timewarp_run();
>> -    pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
>> +    if (loop->n_pollfds < hmap_count(&loop->poll_nodes)) {
>> +        size_t bytes = hmap_count(&loop->poll_nodes) * sizeof *loop->pollfds;
>> +        if (loop->pollfds) {
>> +            loop->pollfds = xrealloc(loop->pollfds, bytes);
>> +        } else {
>> +            loop->pollfds = xmalloc(bytes);
>> +        }
>> +        loop->n_pollfds = hmap_count(&loop->poll_nodes);
>> +    }
>>
> 
> The separate malloc is not necessary, as realloc does allocate the space when the pointer is NULL, so this could become:
> 
>     if (loop->n_pollfds < hmap_count(&loop->poll_nodes)) {
>         loop->n_pollfds = hmap_count(&loop->poll_nodes);
>         loop->pollfds = xrealloc(loop->pollfds, loop->n_pollfds * sizeof *loop->pollfds);
>     }

Thanks for pointing this out!  I'll incorporate this and we'll see what
Ben thinks.  :-)

-- 
Russell Bryant



More information about the dev mailing list