poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/7] ios: Drop unused seek/tell callbacks.


From: Eric Blake
Subject: [PATCH 4/7] ios: Drop unused seek/tell callbacks.
Date: Sat, 29 Feb 2020 05:12:22 -0600

Now that the previous patch proved we were always passing a valid
offset, it's time to shift the burden on who does seeking.  Instead of
the ios layer making two calls into the driver (first to seek, then to
do I/O), the seek is now local to the driver.  For memory (and the
upcoming NBD driver), this is actually simpler (no need to track an
artificial current location).  For file, we temporarily have more
calls to fseeko than previously (8 times instead of 1 when reading an
aligned uint64_t, for example); but that will be cleaned up in
upcoming patches when we switch to a pread-style interface.  Besides,
it would be a lame libc that didn't optimize an fseeko() that ends up
in the same location that it is already at, so we aren't really
resulting in more lseek() syscalls.

And with the shift on who does seeking, we can simplify the ios-dev
interface itself, getting rid of two now-unused callbacks.

* src/ios.c (ios_read_int, ios_read_uint, ios_read_string)
(ios_write_int_common, ios_write_int, ios_write_uint)
(ios_write_string): Drop redundant seek calls.
* src/ios-dev.h (struct ios_dev_if): Drop unused seek and tell.
* src/ios-dev-file.c (ios_dev_file_getc, ios_dev_file_putc): Set
rather than assert offset.
(ios_dev_file_tell, ios_dev_file_seek): Delete.
* src/ios-dev-mem.c (ios_dev_mem_getc, ios_dev_mem_putc): Simplify.
(ios_dev_mem_tell, ios_dev_mem_seek): Delete.
---
 ChangeLog          | 13 ++++++++++
 src/ios-dev.h      | 15 -----------
 src/ios-dev-file.c | 33 +++--------------------
 src/ios-dev-mem.c  | 44 +++++--------------------------
 src/ios.c          | 65 ----------------------------------------------
 5 files changed, 23 insertions(+), 147 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7dd8d55f..c554c5ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2020-02-28  Eric Blake  <address@hidden>
+
+       ios: Drop unused seek/tell callbacks.
+       * src/ios.c (ios_read_int, ios_read_uint, ios_read_string)
+       (ios_write_int_common, ios_write_int, ios_write_uint)
+       (ios_write_string): Drop redundant seek calls.
+       * src/ios-dev.h (struct ios_dev_if): Drop unused seek and tell.
+       * src/ios-dev-file.c (ios_dev_file_getc, ios_dev_file_putc): Set
+       rather than assert offset.
+       (ios_dev_file_tell, ios_dev_file_seek): Delete.
+       * src/ios-dev-mem.c (ios_dev_mem_getc, ios_dev_mem_putc): Simplify.
+       (ios_dev_mem_tell, ios_dev_mem_seek): Delete.
+
 2020-02-28  Eric Blake  <address@hidden>

        ios: Prove we don't need seek.
diff --git a/src/ios-dev.h b/src/ios-dev.h
index 252ae8ab..5cc786cb 100644
--- a/src/ios-dev.h
+++ b/src/ios-dev.h
@@ -32,9 +32,6 @@ typedef uint64_t ios_dev_off;
 /* The following macros are part of the device interface.  */

 #define IOD_EOF -1
-#define IOD_SEEK_SET 0
-#define IOD_SEEK_CUR 1
-#define IOD_SEEK_END 2

 /* Error codes to be used in the inteface below.  */

@@ -66,18 +63,6 @@ struct ios_dev_if

   int (*close) (void *dev);

-  /* Return the current position in the given device.  Return -1 on
-     error.  */
-
-  ios_dev_off (*tell) (void *dev);
-
-  /* Change the current position in the given device according to
-     OFFSET and WHENCE.  WHENCE can be one of IOD_SEEK_SET,
-     IOD_SEEK_CUR and IOD_SEEK_END.  Return 0 on successful
-     completion, and -1 on error.  */
-
-  int (*seek) (void *dev, ios_dev_off offset, int whence);
-
   /* Read a byte from the given device at the current position.
      Return the byte in an int, or IOD_EOF on error.  */

diff --git a/src/ios-dev-file.c b/src/ios-dev-file.c
index 03f695f2..3174e0b8 100644
--- a/src/ios-dev-file.c
+++ b/src/ios-dev-file.c
@@ -148,7 +148,8 @@ ios_dev_file_getc (void *iod, ios_dev_off offset)
   struct ios_dev_file *fio = iod;
   int ret;

-  assert (ftello (fio->file) == offset);
+  if (fseeko (fio->file, offset, SEEK_SET) == -1)
+    return IOD_EOF;
   ret = fgetc (fio->file);

   return ret == EOF ? IOD_EOF : ret;
@@ -160,37 +161,13 @@ ios_dev_file_putc (void *iod, int c, ios_dev_off offset)
   struct ios_dev_file *fio = iod;
   int ret;

-  assert (ftello (fio->file) == offset);
+  if (fseeko (fio->file, offset, SEEK_SET) == -1)
+    return IOD_EOF;
   ret = putc (c, fio->file);
   //printf ("%d -> 0x%lu\n", ret, ftello (fio->file));
   return ret == EOF ? IOD_EOF : ret;
 }

-static ios_dev_off
-ios_dev_file_tell (void *iod)
-{
-  struct ios_dev_file *fio = iod;
-  return ftello (fio->file);
-}
-
-static int
-ios_dev_file_seek (void *iod, ios_dev_off offset, int whence)
-{
-  struct ios_dev_file *fio = iod;
-  int fwhence;
-
-  switch (whence)
-    {
-    case IOD_SEEK_SET: fwhence = SEEK_SET; break;
-    case IOD_SEEK_CUR: fwhence = SEEK_CUR; break;
-    case IOD_SEEK_END: fwhence = SEEK_END; break;
-    default:
-      assert (0);
-    }
-
-  return fseeko (fio->file, offset, fwhence);
-}
-
 static ios_dev_off
 ios_dev_file_size (void *iod)
 {
@@ -206,8 +183,6 @@ struct ios_dev_if ios_dev_file =
    .handler_normalize = ios_dev_file_handler_normalize,
    .open = ios_dev_file_open,
    .close = ios_dev_file_close,
-   .tell = ios_dev_file_tell,
-   .seek = ios_dev_file_seek,
    .get_c = ios_dev_file_getc,
    .put_c = ios_dev_file_putc,
    .get_flags = ios_dev_file_get_flags,
diff --git a/src/ios-dev-mem.c b/src/ios-dev-mem.c
index 5fb6bfa0..22938909 100644
--- a/src/ios-dev-mem.c
+++ b/src/ios-dev-mem.c
@@ -32,7 +32,6 @@ struct ios_dev_mem
 {
   char *pointer;
   size_t size;
-  size_t cur;
   uint64_t flags;
 };

@@ -54,7 +53,6 @@ ios_dev_mem_open (const char *handler, uint64_t flags, int 
*error)
   mio = xmalloc (sizeof (struct ios_dev_mem));
   mio->pointer = xmalloc (MEM_STEP);
   mio->size = MEM_STEP;
-  mio->cur = 0;
   mio->flags = flags;

   return mio;
@@ -84,11 +82,10 @@ ios_dev_mem_getc (void *iod, ios_dev_off offset)
 {
   struct ios_dev_mem *mio = iod;

-  assert (mio->cur == offset);
-  if (mio->cur >= mio->size)
+  if (offset >= mio->size)
     return IOD_EOF;

-  return mio->pointer[mio->cur++];
+  return mio->pointer[offset];
 }

 static int
@@ -96,42 +93,15 @@ ios_dev_mem_putc (void *iod, int c, ios_dev_off offset)
 {
   struct ios_dev_mem *mio = iod;

-  assert (mio->cur == offset);
-  if (mio->cur >= mio->size)
+  if (offset >= mio->size) {
+    assert (MEM_STEP > offset - mio->size);
     mio->pointer = xrealloc (mio->pointer,
                              mio->size + MEM_STEP);
-  mio->pointer[mio->cur++] = c;
+  }
+  mio->pointer[offset] = c;
   return c;
 }

-static ios_dev_off
-ios_dev_mem_tell (void *iod)
-{
-  struct ios_dev_mem *mio = iod;
-  return mio->cur;
-}
-
-static int
-ios_dev_mem_seek (void *iod, ios_dev_off offset, int whence)
-{
-  struct ios_dev_mem *mio = iod;
-
-  switch (whence)
-    {
-    case IOD_SEEK_SET:
-      mio->cur = offset;
-      break;
-    case IOD_SEEK_CUR:
-      mio->cur += offset;
-      break;
-    case IOD_SEEK_END:
-      mio->cur = mio->size - offset;
-      break;
-    }
-
-  return mio->cur;
-}
-
 static ios_dev_off
 ios_dev_mem_size (void *iod)
 {
@@ -144,8 +114,6 @@ struct ios_dev_if ios_dev_mem =
    .handler_normalize = ios_dev_mem_handler_normalize,
    .open = ios_dev_mem_open,
    .close = ios_dev_mem_close,
-   .tell = ios_dev_mem_tell,
-   .seek = ios_dev_mem_seek,
    .get_c = ios_dev_mem_getc,
    .put_c = ios_dev_mem_putc,
    .get_flags = ios_dev_mem_get_flags,
diff --git a/src/ios.c b/src/ios.c
index 9cd6d51a..ca7b5b68 100644
--- a/src/ios.c
+++ b/src/ios.c
@@ -695,10 +695,6 @@ ios_read_int (ios io, ios_off offset, int flags,
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);

-  /* We always need to start reading from offset / 8  */
-  if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-    return IOS_EIOFF;
-
   /* Fast track for byte-aligned 8x bits  */
   if (offset % 8 == 0 && bits % 8 == 0)
     {
@@ -830,10 +826,6 @@ ios_read_uint (ios io, ios_off offset, int flags,
   /* When aligned, 1 to 64 bits can span at most 8 bytes.  */
   uint64_t c[8] = {0, 0, 0, 0, 0, 0, 0, 0};

-  /* We always need to start reading from offset / 8  */
-  if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-    return IOS_EIOFF;
-
   /* Fast track for byte-aligned 8x bits  */
   if (offset % 8 == 0 && bits % 8 == 0)
     {
@@ -929,10 +921,6 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)
          boundary.  We just read bytes from the IOD until either EOF
          or a NULL byte.  */

-      if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET)
-          == -1)
-        return IOS_EIOFF;
-
       do
         {
           if (i % 128 == 0)
@@ -1157,9 +1145,6 @@ ios_write_int_common (ios io, ios_off offset, int flags,
       tail = head;
       IOS_CHAR_GET_MSB(&head, offset % 8);
       IOS_CHAR_GET_LSB(&tail, 8 - lastbyte_bits);
-      /* We will write starting from offset / 8.  */
-      if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-       return IOS_EIOFF;

       /* Write the byte back without changing the surrounding bits.  */
       c[0] = head | tail | (value << (8 - lastbyte_bits));
@@ -1174,9 +1159,6 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     /* Correctly set the unmodified trailing bits of the last byte.  */
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + 1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB && bits > 8)
       {
@@ -1197,13 +1179,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1231,13 +1208,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1269,13 +1241,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1311,13 +1278,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1357,13 +1319,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1407,13 +1364,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1461,13 +1413,8 @@ ios_write_int_common (ios io, ios_off offset, int flags,
     IOS_GET_C_ERR_CHCK(c[0], io, offset / 8);
     IOS_CHAR_GET_MSB(&c[0], offset % 8);
     /* Correctly set the unmodified trailing bits of the last byte.  */
-    if (io->dev_if->seek (io->dev, offset / 8 + bytes_minus1, IOD_SEEK_SET) == 
-1)
-      return IOS_EIOFF;
     IOS_GET_C_ERR_CHCK(c[bytes_minus1], io, offset / 8 + bytes_minus1);
     IOS_CHAR_GET_LSB(&c[bytes_minus1], 8 - lastbyte_bits);
-    /* We will write starting from offset / 8.  */
-    if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-      return IOS_EIOFF;

     if (endian == IOS_ENDIAN_LSB)
     {
@@ -1518,10 +1465,6 @@ ios_write_int (ios io, ios_off offset, int flags,
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);

-  /* We always need to start reading or writing from offset / 8  */
-  if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-    return IOS_EIOFF;
-
   /* Fast track for byte-aligned 8x bits  */
   if (offset % 8 == 0 && bits % 8 == 0)
     return ios_write_int_fast (io, offset, flags, bits, endian, value);
@@ -1543,10 +1486,6 @@ ios_write_uint (ios io, ios_off offset, int flags,
   /* Apply the IOS bias.  */
   offset += ios_get_bias (io);

-  /* We always need to start reading or writing from offset / 8  */
-  if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET) == -1)
-    return IOS_EIOFF;
-
   /* Fast track for byte-aligned 8x bits  */
   if (offset % 8 == 0 && bits % 8 == 0)
     return ios_write_int_fast (io, offset, flags, bits, endian, value);
@@ -1569,10 +1508,6 @@ ios_write_string (ios io, ios_off offset, int flags,
       /* This is the fast case: we want to write a string at a
          byte-boundary.  Just write the bytes to the IOD.  */

-      if (io->dev_if->seek (io->dev, offset / 8, IOD_SEEK_SET)
-          == -1)
-        return IOS_EIOFF;
-
       p = value;
       do
         {
-- 
2.25.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]