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

Eric Blake eblake at redhat.com
Thu May 9 16:02:54 UTC 2019


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:
-- 
2.20.1




More information about the Libguestfs mailing list