[Libguestfs] [nbdkit PATCH] plugins: Use static buffer for plugin_zeroes

Richard W.M. Jones rjones at redhat.com
Mon May 13 07:43:37 UTC 2019


On Thu, May 09, 2019 at 11:02:54AM -0500, Eric Blake wrote:
> No need to calloc/free a buffer every time NBD_CMD_WRITE_ZEROES has to
> fall back to a .pread call. Just reserve the maximum buffer up front
> in our bss.
> 
> Signed-off-by: Eric Blake <eblake at redhat.com>
> ---
> 
> I noticed that buf was a candidate for CLEANUP_FREE, but then further
> noticed that we can avoid the calloc/free altogether if we don't mind
> the bss being 64M larger.
> 
>  server/plugins.c | 10 ++--------
>  1 file changed, 2 insertions(+), 8 deletions(-)
> 
> diff --git a/server/plugins.c b/server/plugins.c
> index 947bb6d..acdfa1f 100644
> --- a/server/plugins.c
> +++ b/server/plugins.c
> @@ -594,7 +594,7 @@ plugin_zero (struct backend *b, struct connection *conn,
>               uint32_t count, uint64_t offset, uint32_t flags, int *err)
>  {
>    struct backend_plugin *p = container_of (b, struct backend_plugin, backend);
> -  char *buf;
> +  static const char buf[MAX_REQUEST_SIZE];
>    uint32_t limit;
>    int r = -1;
>    bool may_trim = flags & NBDKIT_FLAG_MAY_TRIM;
> @@ -639,12 +639,7 @@ plugin_zero (struct backend *b, struct connection *conn,
>    assert (p->plugin.pwrite || p->plugin._pwrite_old);
>    flags &= ~NBDKIT_FLAG_MAY_TRIM;
>    threadlocal_set_error (0);
> -  limit = count < MAX_REQUEST_SIZE ? count : MAX_REQUEST_SIZE;
> -  buf = calloc (limit, 1);
> -  if (!buf) {
> -    *err = ENOMEM;
> -    return -1;
> -  }
> +  limit = count < sizeof (buf) ? count : sizeof (buf);
> 
>    while (count) {
>      r = plugin_pwrite (b, conn, buf, limit, offset, flags, err);
> @@ -656,7 +651,6 @@ plugin_zero (struct backend *b, struct connection *conn,
>    }
> 
>    *err = errno;
> -  free (buf);
>    errno = *err;
> 
>   done:
> -- 

This is much simpler than it sounded when you described it
to me on IRC, ACK :-)

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/




More information about the Libguestfs mailing list