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


The following commit(s) were added to refs/heads/master by this push:
     new 82a1289a9e6 fs: add mempool for file objects
82a1289a9e6 is described below

commit 82a1289a9e6aed62c72d8b55b98a99ec95c0238d
Author: ganjing <[email protected]>
AuthorDate: Thu Nov 26 11:43:07 2026 +0800

    fs: add mempool for file objects
    
    Add dedicated mempool for file objects to provide memory isolation from
    shared heap. Dynamic expansion uses fs_heap via allocator callbacks.
    Preallocated objects avoid individual heap allocations. Return the final
    file reference through file_deallocate so it uses the same allocator.
    
    Signed-off-by: Gan Jing <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/file_descriptors.rst | 25 ++++++++
 fs/Kconfig                                       | 22 +++++++
 fs/fs_heap.c                                     |  5 ++
 fs/fs_heap.h                                     |  3 +
 fs/inode/fs_files.c                              | 78 +++++++++++++++++++++++-
 5 files changed, 132 insertions(+), 1 deletion(-)

diff --git a/Documentation/os/filesystem/file_descriptors.rst 
b/Documentation/os/filesystem/file_descriptors.rst
index a54ffae2f04..dd55094bb26 100644
--- a/Documentation/os/filesystem/file_descriptors.rst
+++ b/Documentation/os/filesystem/file_descriptors.rst
@@ -4,6 +4,31 @@
 File Descriptors
 ================
 
+File Object Allocation
+======================
+
+``CONFIG_FS_PREALLOC_FILES`` reserves a static pool of ``struct file``
+objects. ``CONFIG_FS_ALLOC_FILES`` sets the number of objects added to the
+pool from ``fs_heap`` when the pool is empty. Both settings default to zero,
+which preserves direct allocation from ``fs_heap``.
+
+Set ``CONFIG_FS_PREALLOC_FILES`` to a positive value and leave
+``CONFIG_FS_ALLOC_FILES`` at zero for a fixed pool. Allocation fails
+immediately when all objects are in use. Include files opened during system
+startup when choosing its capacity. Set ``CONFIG_FS_ALLOC_FILES`` to a
+positive value to allow growth, with or without statically reserved objects.
+Expanded storage remains allocated for reuse, so memory use follows the
+peak number of file objects. Dynamic chunks use the independent FS heap
+when ``CONFIG_FS_HEAPSIZE`` is enabled, and the kernel heap otherwise.
+
+The pool serves dynamically allocated open file objects across all tasks.
+``dup()`` and inherited descriptors share references to an open file object;
+the object returns to the pool when its last reference is released. File
+descriptor tables, socket private data and filesystem private data have
+their own allocation policies. Caller-owned objects passed to
+``file_open()`` are also outside this pool. Enabling this pool alone does
+not make ``open()`` free of heap allocations.
+
 Drivers within Drivers
 ======================
 
diff --git a/fs/Kconfig b/fs/Kconfig
index 321303c736c..c1c8c29710e 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -248,6 +248,28 @@ config SENDFILE_BUFSIZE
        ---help---
                Size of the I/O buffer to allocate in sendfile().  Default: 512b
 
+config FS_PREALLOC_FILES
+       int "Preallocated file objects"
+       default 0
+       range 0 65535
+       ---help---
+               Number of statically reserved struct file objects. If this or
+               FS_ALLOC_FILES is nonzero, file objects use a dedicated memory 
pool.
+               Otherwise they are allocated directly from fs_heap as before.
+
+               With FS_ALLOC_FILES set to zero, the pool has a fixed capacity 
and
+               allocation fails immediately when it is empty. Reserve enough 
objects
+               for system startup as well as all concurrently open files.
+
+config FS_ALLOC_FILES
+       int "File object pool expansion batch size"
+       default 0
+       range 0 65535
+       ---help---
+               Number of file objects allocated from fs_heap when the file 
pool is
+               empty. Set to zero to disable expansion. Expanded blocks are 
retained
+               for reuse, so heap use follows the peak number of open file 
objects.
+
 config FS_HEAPSIZE
        int "Independent heap bytes"
        default 0
diff --git a/fs/fs_heap.c b/fs/fs_heap.c
index b46aa95ec03..dc5df4fa693 100644
--- a/fs/fs_heap.c
+++ b/fs/fs_heap.c
@@ -88,6 +88,11 @@ void fs_heap_free(FAR void *mem)
   mm_free(g_fs_heap, mem);
 }
 
+bool fs_heap_member(FAR void *mem)
+{
+  return mm_heapmember(g_fs_heap, mem);
+}
+
 FAR char *fs_heap_strdup(FAR const char *s)
 {
   size_t len = strlen(s) + 1;
diff --git a/fs/fs_heap.h b/fs/fs_heap.h
index 9ba6d8dd812..511a87fd0a9 100644
--- a/fs/fs_heap.h
+++ b/fs/fs_heap.h
@@ -32,6 +32,7 @@
 #include <nuttx/compiler.h>
 #include <nuttx/kmalloc.h>
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -47,6 +48,7 @@ size_t    fs_heap_malloc_size(FAR void *mem);
 FAR void *fs_heap_realloc(FAR void *oldmem, size_t size) realloc_like(2);
 FAR void *fs_heap_memalign(size_t alignment, size_t size) malloc_like1(2);
 void      fs_heap_free(FAR void *mem);
+bool      fs_heap_member(FAR void *mem);
 FAR char *fs_heap_strdup(FAR const char *s) malloc_like;
 FAR char *fs_heap_strndup(FAR const char *s, size_t size) malloc_like;
 int       fs_heap_asprintf(FAR char **strp, FAR const char *fmt, ...)
@@ -60,6 +62,7 @@ struct mallinfo_task fs_heap_mallinfo_task(FAR const struct 
malltask *task);
 #  define fs_heap_realloc       kmm_realloc
 #  define fs_heap_memalign      kmm_memalign
 #  define fs_heap_free          kmm_free
+#  define fs_heap_member        kmm_heapmember
 #  define fs_heap_mallinfo_task kmm_mallinfo_task
 #  define fs_heap_strdup        strdup
 #  define fs_heap_strndup       strndup
diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index 016e1045edc..8723dbba178 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -39,6 +39,7 @@
 
 #include <nuttx/fs/fs.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/cancelpt.h>
 #include <nuttx/fs/ioctl.h>
 #include <nuttx/mutex.h>
@@ -56,10 +57,77 @@
 #include "inode/inode.h"
 #include "fs_heap.h"
 
+#if CONFIG_FS_PREALLOC_FILES > 0 || CONFIG_FS_ALLOC_FILES > 0
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define FILE_POOL_BLOCKSIZE ALIGN_UP(sizeof(struct file), MM_ALIGN)
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static FAR void *file_pool_alloc(FAR struct mempool_s *pool, size_t size);
+static void file_pool_free(FAR struct mempool_s *pool, FAR void *addr);
+static void file_pool_check(FAR struct mempool_s *pool, FAR void *addr);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static char g_file_pool_buffer[CONFIG_FS_PREALLOC_FILES *
+  MEMPOOL_REALBLOCKSIZE(FILE_POOL_BLOCKSIZE) + MEMPOOL_HEADER_SIZE]
+  aligned_data(MM_ALIGN);
+
+static struct mempool_s g_file_mempool =
+{
+  .initialbase = g_file_pool_buffer,
+  .initialsize = sizeof(g_file_pool_buffer),
+  .blocksize   = FILE_POOL_BLOCKSIZE,
+  .expandsize  = CONFIG_FS_ALLOC_FILES *
+                 MEMPOOL_REALBLOCKSIZE(FILE_POOL_BLOCKSIZE) +
+                 MEMPOOL_HEADER_SIZE,
+  .name        = "file",
+  .alloc       = file_pool_alloc,
+  .free        = file_pool_free,
+  .check       = file_pool_check,
+};
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
 
+#if CONFIG_FS_PREALLOC_FILES > 0 || CONFIG_FS_ALLOC_FILES > 0
+/****************************************************************************
+ * Name: file_pool_alloc
+ ****************************************************************************/
+
+static FAR void *file_pool_alloc(FAR struct mempool_s *pool, size_t size)
+{
+  return fs_heap_malloc(size);
+}
+
+/****************************************************************************
+ * Name: file_pool_free
+ ****************************************************************************/
+
+static void file_pool_free(FAR struct mempool_s *pool, FAR void *addr)
+{
+  fs_heap_free(addr);
+}
+
+/****************************************************************************
+ * Name: file_pool_check
+ ****************************************************************************/
+
+static void file_pool_check(FAR struct mempool_s *pool, FAR void *addr)
+{
+  DEBUGASSERT(fs_heap_member(addr));
+}
+#endif
+
 /****************************************************************************
  * Name: fdlist_limit
  *
@@ -835,7 +903,11 @@ int fdlist_copy(FAR struct fdlist *plist, FAR struct 
fdlist *clist,
 
 FAR struct file *file_allocate(void)
 {
+#if CONFIG_FS_PREALLOC_FILES > 0 || CONFIG_FS_ALLOC_FILES > 0
+  return mempool_zallocate(&g_file_mempool, 0);
+#else
   return fs_heap_zalloc(sizeof(struct file));
+#endif
 }
 
 /****************************************************************************
@@ -848,7 +920,11 @@ FAR struct file *file_allocate(void)
 
 void file_deallocate(FAR struct file *filep)
 {
+#if CONFIG_FS_PREALLOC_FILES > 0 || CONFIG_FS_ALLOC_FILES > 0
+  mempool_release(&g_file_mempool, filep);
+#else
   fs_heap_free(filep);
+#endif
 }
 
 /****************************************************************************
@@ -927,7 +1003,7 @@ int file_put(FAR struct file *filep)
           ferr("ERROR: fs putfilep file_close() failed: %d\n", ret);
         }
 
-      fs_heap_free(filep);
+      file_deallocate(filep);
     }
 
   return ret;

Reply via email to