xiaoxiang781216 commented on code in PR #14898:
URL: https://github.com/apache/nuttx/pull/14898#discussion_r1855086624


##########
include/nuttx/fs/uio.h:
##########
@@ -45,6 +45,7 @@ struct uio
 {
   FAR const struct iovec *uio_iov;
   int uio_iovcnt;
+  size_t uio_offset_in_iov; /* offset in uio_iov[0].iov_base */

Review Comment:
   uio_offset



##########
fs/vfs/fs_uio.c:
##########
@@ -116,3 +116,97 @@ void uio_init(FAR struct uio *uio)
 {
   memset(uio, 0, sizeof(*uio));
 }
+
+/****************************************************************************
+ * Name: uio_copyfrom
+ *
+ * Description:
+ *   Copy data from the linear buffer to uio.
+ *
+ ****************************************************************************/
+
+void uio_copyfrom(FAR struct uio *uio, size_t offset, FAR const void *buf,
+                  size_t len)
+{
+  FAR const struct iovec *iov = uio->uio_iov;
+
+  DEBUGASSERT(uio_resid(uio) >= 0);
+  DEBUGASSERT(len <= uio_resid(uio));
+  DEBUGASSERT(offset <= uio_resid(uio) - len);
+  DEBUGASSERT(SSIZE_MAX - offset >= uio->uio_offset_in_iov);
+
+  offset += uio->uio_offset_in_iov;
+  while (offset > iov->iov_len)
+    {
+      offset -= iov->iov_len;
+      iov++;
+    }
+
+  while (len > 0)
+    {
+      size_t blen = len;
+      if (blen > iov->iov_len - offset)
+        {
+          blen = iov->iov_len - offset;
+        }
+
+      memcpy((FAR uint8_t *)iov->iov_base + offset, buf, blen);
+
+      len -= blen;
+      if (len == 0)
+        {
+          break;
+        }
+
+      buf = (const uint8_t *)buf + blen;
+      iov++;
+      offset = 0;
+    }
+}
+
+/****************************************************************************
+ * Name: uio_copyto
+ *
+ * Description:
+ *   Copy data to the linear buffer from uio.
+ *
+ ****************************************************************************/
+
+void uio_copyto(FAR struct uio *uio, size_t offset, FAR void *buf,

Review Comment:
   how to support call uio_copyto/from mulitple time with the same uio and the 
partial buffer 



##########
fs/vfs/fs_uio.c:
##########
@@ -51,18 +51,68 @@ ssize_t uio_total_len(FAR const struct uio *uio)
 {
   const struct iovec *iov = uio->uio_iov;
   int iovcnt = uio->uio_iovcnt;
+  size_t offset_in_iov = uio->uio_offset_in_iov;
   size_t len = 0;
   int i;
 
   for (i = 0; i < iovcnt; i++)
     {
-      if (SSIZE_MAX - len < iov[i].iov_len)
+      DEBUGASSERT(offset_in_iov <= iov[i].iov_len);
+      if (SSIZE_MAX - len < iov[i].iov_len - offset_in_iov)
         {
           return -EOVERFLOW;
         }
 
-      len += iov[i].iov_len;
+      len += iov[i].iov_len - offset_in_iov;
+      offset_in_iov = 0;
     }
 
   return len;
 }
+
+/****************************************************************************
+ * Name: uio_advance
+ *
+ * Description:
+ *   Advance the pointer/offset in uio by the specified amount.
+ *
+ ****************************************************************************/
+
+void uio_advance(FAR struct uio *uio, size_t sz)
+{
+  FAR const struct iovec *iov = uio->uio_iov;
+  int iovcnt = uio->uio_iovcnt;
+  size_t offset_in_iov = uio->uio_offset_in_iov;
+
+  while (iovcnt > 0)
+    {
+      DEBUGASSERT(offset_in_iov <= iov->iov_len);
+      if (sz < iov->iov_len - offset_in_iov)
+        {
+          offset_in_iov += sz;
+          break;
+        }
+
+      sz -= iov->iov_len;
+      iov++;

Review Comment:
   should we set iov->iov_offset_in_iov to iov->iov_len



##########
fs/vfs/fs_read.c:
##########
@@ -102,6 +101,11 @@ static ssize_t file_readv_compat(FAR struct file *filep,
       remaining -= nread;
     }
 
+  if (ntotal >= 0)
+    {
+      uio_advance(uio, ntotal);

Review Comment:
   what's benefit to update the offset in one of uio entry



##########
fs/vfs/fs_uio.c:
##########
@@ -39,15 +39,15 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: uio_total_len
+ * Name: uio_resid
  *
  * Description:
- *   Return the total length of data in bytes.
+ *   Return the remaining length of data in bytes.
  *   Or -EOVERFLOW.
  *
  ****************************************************************************/
 
-ssize_t uio_total_len(FAR const struct uio *uio)
+ssize_t uio_resid(FAR const struct uio *uio)

Review Comment:
   uio_remain?



##########
fs/vfs/fs_uio.c:
##########
@@ -84,6 +84,7 @@ void uio_advance(FAR struct uio *uio, size_t sz)
   int iovcnt = uio->uio_iovcnt;
   size_t offset_in_iov = uio->uio_offset_in_iov;
 
+  DEBUGASSERT(sz <= uio_resid(uio));

Review Comment:
   merge tl the patch which add uio_advance



##########
fs/vfs/fs_uio.c:
##########
@@ -51,18 +51,68 @@ ssize_t uio_total_len(FAR const struct uio *uio)
 {
   const struct iovec *iov = uio->uio_iov;
   int iovcnt = uio->uio_iovcnt;
+  size_t offset_in_iov = uio->uio_offset_in_iov;
   size_t len = 0;
   int i;
 
   for (i = 0; i < iovcnt; i++)
     {
-      if (SSIZE_MAX - len < iov[i].iov_len)
+      DEBUGASSERT(offset_in_iov <= iov[i].iov_len);
+      if (SSIZE_MAX - len < iov[i].iov_len - offset_in_iov)
         {
           return -EOVERFLOW;
         }
 
-      len += iov[i].iov_len;
+      len += iov[i].iov_len - offset_in_iov;
+      offset_in_iov = 0;

Review Comment:
   should we set ti iov[i].iov_offset_in_iov



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to