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

[nuttx] 05/06: Move anonymous mapping emulation into own module under fs/mmap

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

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

commit a5ed8014e876242cb9bea12499fef74ac0bfa76b
Author: Jukka Laitinen <ju...@ssrc.tii.ae>
AuthorDate: Mon Jan 9 12:02:24 2023 +0400

    Move anonymous mapping emulation into own module under fs/mmap
    
    Signed-off-by: Jukka Laitinen <ju...@ssrc.tii.ae>
---
 fs/mmap/Kconfig      |   7 +++-
 fs/mmap/Make.defs    |   4 ++
 fs/mmap/fs_anonmap.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/mmap/fs_anonmap.h |  61 +++++++++++++++++++++++++++
 fs/mmap/fs_mmap.c    |  61 +--------------------------
 5 files changed, 186 insertions(+), 61 deletions(-)

diff --git a/fs/mmap/Kconfig b/fs/mmap/Kconfig
index b4551376e4..6732b8e89d 100644
--- a/fs/mmap/Kconfig
+++ b/fs/mmap/Kconfig
@@ -23,5 +23,8 @@ config FS_RAMMAP
 
 		See nuttx/fs/mmap/README.txt for additional information.
 
-if FS_RAMMAP
-endif
+config FS_ANONMAP
+	bool "Anonymous mapping emulation"
+	default y
+	---help---
+		Simulate private anonymous mappings by plain malloc
diff --git a/fs/mmap/Make.defs b/fs/mmap/Make.defs
index ab8cbd05e4..dff6e732b8 100644
--- a/fs/mmap/Make.defs
+++ b/fs/mmap/Make.defs
@@ -24,6 +24,10 @@ ifeq ($(CONFIG_FS_RAMMAP),y)
 CSRCS += fs_rammap.c
 endif
 
+ifeq ($(CONFIG_FS_ANONMAP),y)
+CSRCS += fs_anonmap.c
+endif
+
 # Include MMAP build support
 
 DEPPATH += --dep-path mmap
diff --git a/fs/mmap/fs_anonmap.c b/fs/mmap/fs_anonmap.c
new file mode 100644
index 0000000000..534806e9af
--- /dev/null
+++ b/fs/mmap/fs_anonmap.c
@@ -0,0 +1,114 @@
+/****************************************************************************
+ * fs/mmap/fs_anonmap.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/kmalloc.h>
+#include <debug.h>
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: unmap_anonymous
+ ****************************************************************************/
+
+static int unmap_anonymous(FAR struct task_group_s *group,
+                           FAR struct mm_map_entry_s *entry,
+                           FAR void *start,
+                           size_t length)
+{
+  int ret;
+
+  /* De-allocate memory.
+   * NB: This is incomplete anounymous mapping implementation
+   * see file_mmap_ below
+   */
+
+  if (start == entry->vaddr && length == entry->length)
+    {
+      /* entry->priv marks allocation from kernel heap */
+
+      if (entry->priv.i)
+        {
+          kmm_free(start);
+        }
+      else
+        {
+          kumm_free(start);
+        }
+
+      ret = mm_map_remove(get_group_mm(group), entry);
+    }
+  else
+    {
+      ret = -EINVAL;
+      ferr("ERROR: Unknown map type\n");
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int map_anonymous(FAR struct mm_map_entry_s *entry, bool kernel)
+{
+  int ret;
+
+  /* REVISIT:  Should reside outside of the heap.  That is really the
+   * only purpose of MAP_ANONYMOUS:  To get non-heap memory.  In KERNEL
+   * build, this could be accomplished using pgalloc(), provided that
+   * you had logic in place to assign a virtual address to the mapping.
+   */
+
+  entry->vaddr = kernel ?
+    kmm_zalloc(entry->length) : kumm_zalloc(entry->length);
+  if (entry->vaddr == NULL)
+    {
+      ferr("ERROR: kumm_alloc() failed, enable DEBUG_MM for info!\n");
+      return -ENOMEM;
+    }
+
+  entry->munmap = unmap_anonymous;
+  entry->priv.i = kernel;
+
+  ret = mm_map_add(entry);
+  if (ret < 0)
+    {
+      if (kernel)
+        {
+          kmm_free(entry->vaddr);
+        }
+      else
+        {
+          kumm_free(entry->vaddr);
+        }
+
+      entry->vaddr = NULL;
+    }
+
+  return ret;
+}
diff --git a/fs/mmap/fs_anonmap.h b/fs/mmap/fs_anonmap.h
new file mode 100644
index 0000000000..70725c5623
--- /dev/null
+++ b/fs/mmap/fs_anonmap.h
@@ -0,0 +1,61 @@
+/****************************************************************************
+ * fs/mmap/fs_anonmap.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __FS_MMAP_FS_ANONMAP_H
+#define __FS_MMAP_FS_ANONMAP_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <nuttx/mm/map.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: map_anonymous
+ *
+ * Description:
+ *   Support simulation of private anonymous mapping by allocating memory
+ *   from heap
+ *
+ * Input Parameters:
+ *   map     Input struct containing user request
+ *   kernel  kmm_zalloc or kumm_zalloc
+ *
+ * Returned Value:
+ *   On success returns 0. Otherwise negated errno is returned appropriately.
+ *
+ *     ENOMEM
+ *       Insufficient memory is available to simulate mapping
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_ANONMAP
+int map_anonymous(FAR struct mm_map_entry_s *entry, bool kernel);
+#else
+#define map_anonymous(entry,kernel) (-ENOSYS)
+#endif /* CONFIG_FS_ANONMAP */
+
+#endif /* __FS_MMAP_FS_ANONMAP_H */
diff --git a/fs/mmap/fs_mmap.c b/fs/mmap/fs_mmap.c
index 4f541fffe9..e0f81f53a9 100644
--- a/fs/mmap/fs_mmap.c
+++ b/fs/mmap/fs_mmap.c
@@ -36,51 +36,12 @@
 
 #include "inode/inode.h"
 #include "fs_rammap.h"
+#include "fs_anonmap.h"
 
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: unmap_anonymous
- ****************************************************************************/
-
-static int unmap_anonymous(FAR struct task_group_s *group,
-                           FAR struct mm_map_entry_s *entry,
-                           FAR void *start,
-                           size_t length)
-{
-  int ret;
-
-  /* De-allocate memory.
-   * NB: This is incomplete anounymous mapping implementation
-   * see file_mmap_ below
-   */
-
-  if (start == entry->vaddr && length == entry->length)
-    {
-      /* entry->priv marks allocation from kernel heap */
-
-      if (entry->priv.i)
-        {
-          kmm_free(start);
-        }
-      else
-        {
-          kumm_free(start);
-        }
-
-      ret = mm_map_remove(group, &entry);
-    }
-  else
-    {
-      ret = -EINVAL;
-      ferr("ERROR: Unknown map type\n");
-    }
-
-  return ret;
-}
-
 /****************************************************************************
  * Name: file_mmap_
  ****************************************************************************/
@@ -164,25 +125,7 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
 
   if ((flags & MAP_ANONYMOUS) != 0)
     {
-      /* REVISIT:  Should reside outside of the heap.  That is really the
-       * only purpose of MAP_ANONYMOUS:  To get non-heap memory.  In KERNEL
-       * build, this could be accomplished using pgalloc(), provided that
-       * you had logic in place to assign a virtual address to the mapping.
-       */
-
-      *mapped = kernel ? kmm_zalloc(length) : kumm_zalloc(length);
-      if (*mapped == NULL)
-        {
-          ferr("ERROR: kumm_alloc() failed, enable DEBUG_MM for info!\n");
-          return -ENOMEM;
-        }
-
-      entry.vaddr = *mapped;
-      entry.munmap = unmap_anonymous;
-      entry.priv.i = kernel;
-      mm_map_add(&entry);
-
-      return OK;
+      return map_anonymous(&entry, kernel);
     }
 
   if ((flags & MAP_PRIVATE) != 0)