poke-devel
[Top][All Lists]
Advanced

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

[PATCH 3/7] ios: Prove we don't need seek.


From: Eric Blake
Subject: [PATCH 3/7] ios: Prove we don't need seek.
Date: Sat, 29 Feb 2020 05:12:21 -0600

As part of refactoring toward the use of pread rather than fgetc, I
want to validate that the offset passed to every IOS read/write
interaction is the same as the offset previously set with ios->seek.
Since the testsuite still passes, we can then follow this up with a
patch that removes the seek, and rely instead on the offset passed to
the function.

* src/ios-dev.h (struct ios_dev_if): Add offset parameter to get_c
and put_c.
* src/ios.c (IOS_GET_C_ERR_CHCK, IOS_PUT_C_ERR_CHCK)
(ios_read_string, ios_write_string): Pass offset through.
* src/ios-dev-file.c (ios_dev_file_getc, ios_dev_file_putc):
Update signature and add assertion.
* src/ios-dev-mem.c (ios_dev_mem_getc, ios_dev_mem_putc):
Likewise.
---
 ChangeLog          | 12 ++++++++++++
 src/ios-dev.h      |  4 ++--
 src/ios-dev-file.c | 14 ++++++++++----
 src/ios-dev-mem.c  |  8 ++++++--
 src/ios.c          | 31 ++++++++++++++++---------------
 5 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 80428f04..7dd8d55f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2020-02-28  Eric Blake  <address@hidden>
+
+       ios: Prove we don't need seek.
+       * src/ios-dev.h (struct ios_dev_if): Add offset parameter to get_c
+       and put_c.
+       * src/ios.c (IOS_GET_C_ERR_CHCK, IOS_PUT_C_ERR_CHCK)
+       (ios_read_string, ios_write_string): Pass offset through.
+       * src/ios-dev-file.c (ios_dev_file_getc, ios_dev_file_putc):
+       Update signature and add assertion.
+       * src/ios-dev-mem.c (ios_dev_mem_getc, ios_dev_mem_putc):
+       Likewise.
+
 2020-02-27  Eric Blake  <address@hidden>

        ios: Pass offset to low-level macros.
diff --git a/src/ios-dev.h b/src/ios-dev.h
index 1b01302f..252ae8ab 100644
--- a/src/ios-dev.h
+++ b/src/ios-dev.h
@@ -81,12 +81,12 @@ struct ios_dev_if
   /* Read a byte from the given device at the current position.
      Return the byte in an int, or IOD_EOF on error.  */

-  int (*get_c) (void *dev);
+  int (*get_c) (void *dev, ios_dev_off offset);

   /* Write a byte to the given device at the current position.  Return
      the character written as an int, or IOD_EOF on error.  */

-  int (*put_c) (void *dev, int c);
+  int (*put_c) (void *dev, int c, ios_dev_off offset);

   /* Return the flags of the device, as it was opened.  */

diff --git a/src/ios-dev-file.c b/src/ios-dev-file.c
index 31e833a5..03f695f2 100644
--- a/src/ios-dev-file.c
+++ b/src/ios-dev-file.c
@@ -143,19 +143,25 @@ ios_dev_file_get_flags (void *iod)


 static int
-ios_dev_file_getc (void *iod)
+ios_dev_file_getc (void *iod, ios_dev_off offset)
 {
   struct ios_dev_file *fio = iod;
-  int ret = fgetc (fio->file);
+  int ret;
+
+  assert (ftello (fio->file) == offset);
+  ret = fgetc (fio->file);

   return ret == EOF ? IOD_EOF : ret;
 }

 static int
-ios_dev_file_putc (void *iod, int c)
+ios_dev_file_putc (void *iod, int c, ios_dev_off offset)
 {
   struct ios_dev_file *fio = iod;
-  int ret = putc (c, fio->file);
+  int ret;
+
+  assert (ftello (fio->file) == offset);
+  ret = putc (c, fio->file);
   //printf ("%d -> 0x%lu\n", ret, ftello (fio->file));
   return ret == EOF ? IOD_EOF : ret;
 }
diff --git a/src/ios-dev-mem.c b/src/ios-dev-mem.c
index 72b5e9a1..5fb6bfa0 100644
--- a/src/ios-dev-mem.c
+++ b/src/ios-dev-mem.c
@@ -17,6 +17,8 @@
  */

 #include <config.h>
+
+#include <assert.h>
 #include <string.h>
 #include <stdlib.h>
 #include <xalloc.h>
@@ -78,10 +80,11 @@ ios_dev_mem_get_flags  (void *iod)
 }

 static int
-ios_dev_mem_getc (void *iod)
+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)
     return IOD_EOF;

@@ -89,10 +92,11 @@ ios_dev_mem_getc (void *iod)
 }

 static int
-ios_dev_mem_putc (void *iod, int c)
+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)
     mio->pointer = xrealloc (mio->pointer,
                              mio->size + MEM_STEP);
diff --git a/src/ios.c b/src/ios.c
index 7b64d12b..9cd6d51a 100644
--- a/src/ios.c
+++ b/src/ios.c
@@ -32,20 +32,20 @@

 #define STREQ(a, b) (strcmp (a, b) == 0)

-#define IOS_GET_C_ERR_CHCK(c, io, off)         \
-  {                                             \
-  int ret = io->dev_if->get_c ((io)->dev);     \
-  if (ret == IOD_EOF)                           \
-    return IOS_EIOFF;                           \
-  (c) = ret;                                    \
-}
+#define IOS_GET_C_ERR_CHCK(c, io, off)                 \
+  {                                                    \
+    int ret = io->dev_if->get_c ((io)->dev, off);      \
+    if (ret == IOD_EOF)                                        \
+      return IOS_EIOFF;                                        \
+    (c) = ret;                                         \
+  }

-#define IOS_PUT_C_ERR_CHCK(c, io, off)         \
-{                                              \
-  if (io->dev_if->put_c ((io)->dev, (char)(c)) \
-      == IOD_EOF)                              \
-    return IOS_EIOBJ;                          \
-}
+#define IOS_PUT_C_ERR_CHCK(c, io, off)                 \
+  {                                                    \
+    if (io->dev_if->put_c ((io)->dev, (char)(c), off)  \
+       == IOD_EOF)                                     \
+      return IOS_EIOBJ;                                        \
+  }

 #define IOS_READ_INTO_CHARRAY_1BYTE(charray, off)      \
 {                                                      \
@@ -938,7 +938,7 @@ ios_read_string (ios io, ios_off offset, int flags, char 
**value)
           if (i % 128 == 0)
             str = xrealloc (str, i + 128 * sizeof (char));

-          c = io->dev_if->get_c (io->dev);
+          c = io->dev_if->get_c (io->dev, offset / 8 + i);
           if (c == IOD_EOF)
             return IOS_EIOFF;
           else
@@ -1576,7 +1576,8 @@ ios_write_string (ios io, ios_off offset, int flags,
       p = value;
       do
         {
-          if (io->dev_if->put_c (io->dev, *p) == IOD_EOF)
+          if (io->dev_if->put_c (io->dev, *p,
+                                offset / 8 + p - value) == IOD_EOF)
             return IOS_EIOFF;
         }
       while (*(p++) != '\0');
-- 
2.25.1




reply via email to

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