Re: [Libguestfs] [PATCH 5/9] connections: Allow multiple handles to be stored in the connection object.

Eric Blake
Wed, 17 Jan 2018 21:44:04 -0000

On 01/17/2018 02:53 PM, Richard W.M. Jones wrote:
> Previously only one handle could be stored, but we will need to store
> multiple handles when we have filters.
>
> The plugin handle is defined as index 0.  Filters will use indices > 0.
> ---
>  src/connections.c | 37 ++++++++++++++++++++++++++++++-------
>  src/internal.h    |  4 ++--
>  src/plugins.c     | 53
+++++++++++++++++++++++++++--------------------------
>  3 files changed, 59 insertions(+), 35 deletions(-)
>

> +int
> +connection_set_handle (struct connection *conn, size_t i, void *handle)
>  {
> -  conn->handle = handle;
> +  size_t j;
> +
> +  if (i < conn->nr_handles)
> +    conn->handles[i] = handle;
> +  else {
> +    j = conn->nr_handles;
> +    conn->nr_handles = i+1;
> +    conn->handles = realloc (conn->handles,
> +                             conn->nr_handles * sizeof (void *));

Growing linearly (adding 1 per realloc) is a quadratic O(n^2) growth
algorithm; it won't matter if we don't expect a large number of filters
(say anything less than 100), but can get noticeable if we want to allow
extremes.  A less-intensive algorithm is to grow geometrically (a fixed
growth factor, say x1.5 or x2, gives you amortized O(n) - although you
have to track the array size independently from the number of slots in
the array).  At this point, I'm fine keeping the simpler linear realloc,
on the grounds that a large number of filters on the command line is
unlikely.

ACK.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org