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

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

commit a8973437341d4ce476dcac8c13dabdaeca16aeb6
Author: fangxinyong <[email protected]>
AuthorDate: Thu Nov 19 00:15:04 2026 +0800

    fs/shm: separate backing store from shmfs_object_s
    
    Split the physical memory backing store into an independently allocated
    buffer so it can be freed and reallocated without destroying the object
    shell.  In KERNEL build the page-pointer array is no longer a flexible
    array embedded in the struct; it is a separately malloc'd array.
    
    Add shmfs_alloc_buffer / shmfs_free_buffer that handle the per-mode
    allocation details (FLAT: fs heap, PROTECTED: kumm, KERNEL: page
    array).  shmfs_alloc_object calls shmfs_alloc_buffer; shmfs_free_object
    calls shmfs_free_buffer.  All three build modes use the same
    object/buffer split.
    
    This is a prerequisite for a per-object lock: the object shell stays
    valid while the buffer is replaced.
    
    Signed-off-by: fangxinyong <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/shm/shmfs.c       |   4 +-
 fs/shm/shmfs.h       |  15 ++--
 fs/shm/shmfs_alloc.c | 201 +++++++++++++++++++++++++--------------------------
 3 files changed, 107 insertions(+), 113 deletions(-)

diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c
index b736ee7f776..34818fb957b 100644
--- a/fs/shm/shmfs.c
+++ b/fs/shm/shmfs.c
@@ -106,7 +106,7 @@ static void shmfs_copy(FAR struct shmfs_object_s *sho, 
off_t pos,
                        FAR char *buffer, size_t len, bool write)
 {
 #ifdef CONFIG_BUILD_KERNEL
-  FAR uintptr_t *pages = (FAR uintptr_t *)&sho->paddr;
+  FAR uintptr_t *pages = (FAR uintptr_t *)sho->paddr;
   FAR char *vaddr;
   size_t offset;
   size_t n;
@@ -343,7 +343,7 @@ static int shmfs_map_object(FAR struct shmfs_object_s 
*object,
 
   FAR struct tcb_s *tcb = this_task();
   FAR struct task_group_s *group = tcb->group;
-  FAR uintptr_t *pages = (FAR uintptr_t *)&object->paddr;
+  FAR uintptr_t *pages = (FAR uintptr_t *)object->paddr;
   uintptr_t mapaddr;
   unsigned int npages;
 
diff --git a/fs/shm/shmfs.h b/fs/shm/shmfs.h
index b13b3fa85ff..31b2da7ee63 100644
--- a/fs/shm/shmfs.h
+++ b/fs/shm/shmfs.h
@@ -45,20 +45,15 @@ struct shmfs_object_s
 
   size_t length;
 
-  /* Vector of allocations from physical memory.
+  /* The backing store, NULL when none is allocated.
    *
-   * - In flat and protected builds this is a pointer to the
-   *   allocated memory.
+   * - In flat and protected builds this points to the allocated memory.
    *
-   * - In kernel build this is start of a malloc'd vector of void pointers
-   *   and the length of the vector is MM_NPAGES(length).
+   * - In kernel build this points to a separately allocated vector of
+   *   MM_NPAGES(length) page pointers.
    */
 
   FAR void *paddr;
-
-  /* The struct continues here as malloc'd array of void pointers
-   * if CONFIG_BUILD_KERNEL
-   */
 };
 
 /****************************************************************************
@@ -66,8 +61,10 @@ struct shmfs_object_s
  ****************************************************************************/
 
 FAR struct shmfs_object_s *shmfs_alloc_object(size_t length);
+FAR void *shmfs_alloc_buffer(size_t length);
 
 void shmfs_free_object(FAR struct shmfs_object_s *object);
+void shmfs_free_buffer(FAR struct shmfs_object_s *object);
 
 /****************************************************************************
  * Name: shmfs_initialize
diff --git a/fs/shm/shmfs_alloc.c b/fs/shm/shmfs_alloc.c
index 83f72d808e9..40d5ef750b6 100644
--- a/fs/shm/shmfs_alloc.c
+++ b/fs/shm/shmfs_alloc.c
@@ -27,6 +27,7 @@
 #include <nuttx/config.h>
 
 #include <stdbool.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <nuttx/arch.h>
@@ -45,153 +46,149 @@
 FAR struct shmfs_object_s *shmfs_alloc_object(size_t length)
 {
   FAR struct shmfs_object_s *object;
-  bool allocated = false;
 
-#if defined(CONFIG_BUILD_FLAT)
-  /* in FLAT build, allocate the object metadata and the data in the same
-   * chunk in kernel heap
-   */
+  object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
+  if (object == NULL)
+    {
+      return NULL;
+    }
+
+  if (length > 0)
+    {
+      object->paddr = shmfs_alloc_buffer(length);
+      if (object->paddr == NULL)
+        {
+          fs_heap_free(object);
+          return NULL;
+        }
+    }
+
+  object->length = length;
+  return object;
+}
 
-  size_t hdr_size = sizeof(struct shmfs_object_s);
+FAR void *shmfs_alloc_buffer(size_t length)
+{
+  FAR void *paddr = NULL;
 
-  /* mmap() hands out this buffer directly: round it up to a page so that
-   * a page-sized mapping of a smaller object stays inside it.
+#if defined(CONFIG_BUILD_KERNEL)
+  /* In KERNEL build, allocate the shared memory from the page pool and
+   * keep the page addresses in a separate vector.
    */
 
-  size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
-#ifdef CONFIG_FS_SHMFS_NO_ALIGN
-  size_t cachesize = 0;
-#else
-  size_t cachesize = up_get_dcache_linesize();
-#endif
+  size_t n_pages = MM_NPAGES(length);
+  FAR void **pages;
+  size_t i;
 
-  if (cachesize > 0)
-    {
-      hdr_size = ALIGN_UP(hdr_size, cachesize);
-      alloc_size = ALIGN_UP(alloc_size, cachesize);
-      object = fs_heap_memalign(cachesize, hdr_size + alloc_size);
-    }
-  else
+  pages = fs_heap_zalloc(n_pages * sizeof(FAR void *));
+  if (pages == NULL)
     {
-      object = fs_heap_malloc(hdr_size + alloc_size);
+      return NULL;
     }
 
-  if (object)
+  for (i = 0; i < n_pages; i++)
     {
-      memset(object, 0, hdr_size + alloc_size);
-      object->paddr = (void *)((uintptr_t)object + hdr_size);
-      allocated = true;
-    }
+      pages[i] = (FAR void *)mm_pgalloc(1);
+      if (pages[i] == NULL)
+        {
+          break;
+        }
 
-#elif defined(CONFIG_BUILD_PROTECTED)
-  /* in PROTECTED build, allocate the shm object in kernel heap, and shared
-   * memory in user heap
-   */
+      /* Clear the page memory (requirement for truncate) */
 
-  size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
+      up_addrenv_page_wipe((uintptr_t)pages[i]);
+    }
 
-  object = fs_heap_zalloc(sizeof(struct shmfs_object_s));
-  if (object)
+  if (i < n_pages)
     {
-#ifdef CONFIG_FS_SHMFS_NO_ALIGN
-      size_t cachesize = 0;
-#else
-      size_t cachesize = up_get_dcache_linesize();
-#endif
+      /* Delete any partial allocation */
 
-      if (cachesize > 0)
+      while (i-- > 0)
         {
-          alloc_size = ALIGN_UP(alloc_size, cachesize);
-          object->paddr = kumm_memalign(cachesize, alloc_size);
-        }
-      else
-        {
-          object->paddr = kumm_malloc(alloc_size);
+          mm_pgfree((uintptr_t)pages[i], 1);
         }
 
-      if (object->paddr)
-        {
-          memset(object->paddr, 0, alloc_size);
-          allocated = true;
-        }
+      fs_heap_free(pages);
+      return NULL;
     }
 
-#elif defined(CONFIG_BUILD_KERNEL)
-  /* in KERNEL build, allocate the shared memory from page pool and store the
-   * physical address
+  paddr = pages;
+#else
+  /* In FLAT build the shared memory comes from the kernel heap, in
+   * PROTECTED build from the user heap.  mmap() hands out this buffer
+   * directly: round it up to a page so that a page-sized mapping of a
+   * smaller object stays inside it.
    */
 
-  size_t i = 0;
-  FAR void **pages;
-  size_t n_pages = MM_NPAGES(length);
-
-  object = fs_heap_zalloc(sizeof(struct shmfs_object_s) +
-                      (n_pages - 1) * sizeof(object->paddr));
+  size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE));
+#  ifdef CONFIG_FS_SHMFS_NO_ALIGN
+  size_t cachesize = 0;
+#  else
+  size_t cachesize = up_get_dcache_linesize();
+#  endif
 
-  if (object)
+  if (cachesize > 0)
     {
-      pages = &object->paddr;
-      for (; i < n_pages; i++)
-        {
-          pages[i] = (FAR void *)mm_pgalloc(1);
-          if (!pages[i])
-            {
-              break;
-            }
-          else
-            {
-              /* Clear the page memory (requirement for truncate) */
-
-              up_addrenv_page_wipe((uintptr_t)pages[i]);
-            }
-        }
+      alloc_size = ALIGN_UP(alloc_size, cachesize);
+#  ifdef CONFIG_BUILD_FLAT
+      paddr = fs_heap_memalign(cachesize, alloc_size);
+#  else
+      paddr = kumm_memalign(cachesize, alloc_size);
+#  endif
     }
-
-  if (i == n_pages)
+  else
     {
-      allocated = true;
+#  ifdef CONFIG_BUILD_FLAT
+      paddr = fs_heap_malloc(alloc_size);
+#  else
+      paddr = kumm_malloc(alloc_size);
+#  endif
     }
-#endif
 
-  if (allocated)
-    {
-      object->length = length;
-    }
-  else
+  if (paddr != NULL)
     {
-      /* delete any partial allocation */
-
-      shmfs_free_object(object);
-      object = NULL;
+      memset(paddr, 0, alloc_size);
     }
+#endif
 
-  return object;
+  return paddr;
 }
 
 void shmfs_free_object(FAR struct shmfs_object_s *object)
 {
-  if (object)
+  if (object != NULL)
     {
-#if defined(CONFIG_BUILD_PROTECTED)
-      kumm_free(object->paddr);
-#elif defined(CONFIG_BUILD_KERNEL)
-      size_t i;
+      shmfs_free_buffer(object);
+      fs_heap_free(object);
+    }
+}
+
+void shmfs_free_buffer(FAR struct shmfs_object_s *object)
+{
+  if (object->paddr != NULL)
+    {
+#if defined(CONFIG_BUILD_KERNEL)
+      FAR void **pages = object->paddr;
       size_t n_pages = MM_NPAGES(object->length);
-      FAR void **pages = &object->paddr;
+      size_t i;
 
       for (i = 0; i < n_pages; i++)
         {
-          if (pages[i])
+          if (pages[i] != NULL)
             {
               mm_pgfree((uintptr_t)pages[i], 1);
             }
         }
-#endif
 
-      /* Delete the object metadata
-       * (and the shared memory in case of FLAT build)
-       */
+      fs_heap_free(pages);
+#elif defined(CONFIG_BUILD_PROTECTED)
+      kumm_free(object->paddr);
+#else
+      fs_heap_free(object->paddr);
+#endif
 
-      fs_heap_free(object);
+      object->paddr = NULL;
     }
+
+  object->length = 0;
 }

Reply via email to