You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2023/01/12 17:12:05 UTC

[nuttx] 01/03: fs/mmap: Remove the duplication rammap handling

This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit ab67d6a75bdeb6b219c064a15188b188e84a9e6a
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Wed Jan 11 00:48:45 2023 +0800

    fs/mmap: Remove the duplication rammap handling
    
    and minor ifx for style and format
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 fs/mmap/fs_mmap.c   | 37 ++++++-------------------------------
 fs/mmap/fs_rammap.c | 19 +++++++++----------
 fs/mmap/fs_rammap.h |  4 +++-
 3 files changed, 18 insertions(+), 42 deletions(-)

diff --git a/fs/mmap/fs_mmap.c b/fs/mmap/fs_mmap.c
index b979805b6d..a533602f76 100644
--- a/fs/mmap/fs_mmap.c
+++ b/fs/mmap/fs_mmap.c
@@ -85,15 +85,6 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
       return -ENOSYS;
     }
 
-#ifndef CONFIG_FS_RAMMAP
-  if ((flags & MAP_PRIVATE) != 0)
-    {
-      ferr("ERROR: MAP_PRIVATE is not supported without file mapping"
-           "emulation\n");
-      return -ENOSYS;
-    }
-#endif /* CONFIG_FS_RAMMAP */
-
   /* A length of 0 is invalid. */
 
   if (length == 0)
@@ -103,8 +94,7 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
     }
 #endif /* CONFIG_DEBUG_FEATURES */
 
-  if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE &&
-      (flags & MAP_SHARED) != 0)
+  if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE)
     {
       ferr("ERROR: Unsupported options for read-only file descriptor,"
            "prot=%x flags=%04x\n", prot, flags);
@@ -128,45 +118,30 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
       return map_anonymous(&entry, kernel);
     }
 
-  if ((flags & MAP_PRIVATE) != 0)
-    {
-#ifdef CONFIG_FS_RAMMAP
-      /* Allocate memory and copy the file into memory.  We would, of course,
-       * do much better in the KERNEL build using the MMU.
-       */
-
-      return rammap(filep, &entry, kernel);
-#endif
-    }
-
   /* Call driver's mmap to get the base address of the file in 'mapped'
    * in memory.
    */
 
-  if (filep->f_inode && filep->f_inode->u.i_ops->mmap != NULL)
+  if ((flags & MAP_PRIVATE) == 0 && filep->f_inode &&
+      filep->f_inode->u.i_ops->mmap != NULL)
     {
       ret = filep->f_inode->u.i_ops->mmap(filep, &entry);
       if (ret == OK)
         {
-          *mapped = (void *)entry.vaddr;
+          *mapped = entry.vaddr;
         }
     }
   else
     {
-      /* Not directly mappable, probably because the underlying media does
-       * not support random access.
+      /* Caller request the private mapping. Or not directly mappable,
+       * probably because the underlying media doesn't support random access.
        */
 
-#ifdef CONFIG_FS_RAMMAP
       /* Allocate memory and copy the file into memory.  We would, of course,
        * do much better in the KERNEL build using the MMU.
        */
 
       return rammap(filep, &entry, kernel);
-#else
-      ferr("ERROR: mmap not supported \n");
-      return -ENOSYS;
-#endif
     }
 
   /* Return */
diff --git a/fs/mmap/fs_rammap.c b/fs/mmap/fs_rammap.c
index 5e346b8a5e..16ea937659 100644
--- a/fs/mmap/fs_rammap.c
+++ b/fs/mmap/fs_rammap.c
@@ -51,9 +51,9 @@ static int unmap_rammap(FAR struct task_group_s *group,
                         size_t length)
 {
   FAR void *newaddr;
-  unsigned int offset;
-  bool kernel = entry->priv.i != 0 ? true : false;
-  int ret;
+  off_t offset;
+  bool kernel = entry->priv.i;
+  int ret = OK;
 
   /* Get the offset from the beginning of the region and the actual number
    * of bytes to "unmap".  All mappings must extend to the end of the region.
@@ -111,9 +111,8 @@ static int unmap_rammap(FAR struct task_group_s *group,
         }
 
       DEBUGASSERT(newaddr == entry->vaddr);
-      UNUSED(newaddr); /* May not be used */
+      entry->vaddr = newaddr;
       entry->length = length;
-      ret = OK;
     }
 
   return ret;
@@ -176,7 +175,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
   rdbuffer = kernel ? kmm_malloc(length) : kumm_malloc(length);
   if (!rdbuffer)
     {
-      ferr("ERROR: Region allocation failed, length: %d\n", (int)length);
+      ferr("ERROR: Region allocation failed, length: %zu\n", length);
       return -ENOMEM;
     }
 
@@ -189,7 +188,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
        * the correct response.
        */
 
-      ferr("ERROR: Seek to position %d failed\n", (int)entry->offset);
+      ferr("ERROR: Seek to position %zu failed\n", (size_t)entry->offset);
       ret = fpos;
       goto errout_with_region;
     }
@@ -209,8 +208,8 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
             {
               /* All other read errors are bad. */
 
-              ferr("ERROR: Read failed: offset=%d ret=%d\n",
-                   (int)entry->offset, (int)nread);
+              ferr("ERROR: Read failed: offset=%zu ret=%zd\n",
+                   (size_t)entry->offset, nread);
 
               ret = nread;
               goto errout_with_region;
@@ -237,7 +236,7 @@ int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
   /* Add the buffer to the list of regions */
 
   entry->vaddr = rdbuffer;
-  entry->priv.i = kernel ? 1 : 0;
+  entry->priv.i = kernel;
   entry->munmap = unmap_rammap;
 
   ret = mm_map_add(entry);
diff --git a/fs/mmap/fs_rammap.h b/fs/mmap/fs_rammap.h
index a3c0a49fa1..32fc0c132b 100644
--- a/fs/mmap/fs_rammap.h
+++ b/fs/mmap/fs_rammap.h
@@ -89,6 +89,8 @@
 
 int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry,
            bool kernel);
-
+#else
+#  define rammap(file, entry, kernel) (-ENOSYS)
 #endif /* CONFIG_FS_RAMMAP */
+
 #endif /* __FS_MMAP_FS_RAMMAP_H */