jingfei195887 opened a new pull request, #22545:
URL: https://github.com/apache/nuttx/pull/22545

   ## Summary
   
   Add `CONFIG_FS_HEAPLARGE_ONLY` to reserve the existing filesystem heap for
   TMPFS file data and directory entry arrays, pseudo-file contents, and 
flat-build
   SHM buffers. Ordinary filesystem allocations then use the kernel heap.
   The option defaults to **n**, preserving the current allocation policy.
   
   Selection follows the purpose of an allocation, not a byte-size threshold.
   The private `fs_large_*` family keeps data allocation, reallocation, size 
queries
   and freeing on the same heap. File-pool membership checks follow the selected
   ordinary allocator. Protected SHM still uses user memory; kernel SHM still 
uses
   the page allocator.
   
   ## Impact
   
   This adds an optional allocation policy and filesystem documentation. 
Existing
   configurations retain their behavior. Public APIs, file contents, mapping
   permissions, board support and build commands are unchanged. Internal 
allocator
   names are documented in `Documentation/os/filesystem/heap.rst`.
   
   Moving metadata can leave more of the fixed FS heap available to data 
buffers.
   It does not reduce the heap reservation or promise lower total RAM use.
   Both heaps must be sized for the workload; creating a TMPFS file can still 
need
   a data-heap directory-array expansion.
   
   ## Testing
   
   Linux x86-64; local simulator Make/CMake builds and OSTest. The same final 
fixture
   on baseline `f9d3eb13c3f412426a2658227f2b332e21e3a526` and head 
`ef351e4072fcae74fb1b9ef2a65ebab1babe23ad` performs 1,535 checks:
   33 baseline assertions report the missing split-allocation policy, and all 
pass
   on the branch. Configurations differ only in the new option.
   
   Six flat configurations cover split on/off, no independent heap, TLSF, 32-bit
   large files, and the file pool with four static objects and expansion by 
four.
   They pass 9,177 checks in total. The CMake image passes another
   1,517 checks. The fixture covers allocation residency, read/write,
   nonzero shrink/regrowth, mapped data and TMPFS mmap growth, duplicates, 
directory
   array growth, rename replacement, unlink while open, repeated cleanup, 
nonempty
   unmount and data-heap exhaustion/recovery. TMPFS and SHM also cover 
zero-length
   truncation. Pseudo-file truncation stays nonzero; mappings are removed before
   unlink and close.
   
   With a 256 KiB FS heap, opening 64 descriptors adds 5,408 bytes there before 
the
   change and zero with the option enabled. After exhausting the data heap, 
opening
   `/dev/null` succeeds with the split policy while data growth fails without
   changing the existing file content or length.
   
   Optimized small `sim:nsh` images exclude the fixture and coverage 
instrumentation.
   For text/data/bss, the default-off difference is
   0 / 0 / 0 bytes;
   enabling the new option differs from baseline by
   -128 / 0 / 0 bytes.
   
   Protected `qemu-armv7r:pnsh` and kernel
   `qemu-armv8a:knsh` with file pools enabled build complete images, including 
all
   three affected filesystems. Protected uses the default heap-pool threshold of
   -1. Setting the threshold to zero enables the heap-pool code (without 
automatic
   pooling); that protected configuration fails on both revisions with the same 
five unresolved references to
   `nxsem_tickwait_uninterruptible`, `procfs_snprintf` and `procfs_memcpy` in 
the
   user image. Those builds are not counted as passing.
   
   Functional simulator logs retain an existing NSH startup `/tmp` mount error. 
The
   fixture uses `/heaplarge`, whose mount and unmount checks pass. An early 
fixture
   mistakenly tried to remove the already removed mountpoint; another used the
   kernel-heap statistics name in a shared-heap flat build. Both failed attempts
   are retained locally; the final fixture uses the existing flat 
`mallinfo_task`
   API when no independent heap is configured.
   
   No physical hardware, DMA/cache-line target, SMP, or protected/kernel runtime
   test was performed. The fixture uses private filesystem structures to check
   allocator residency, so its complete source is attached here rather than
   adding an application that depends on those private headers.
   
   <details>
   <summary>Local validation</summary>
   
   ```text
   target   : nuttx
   branch   : fs/heap-large-only
   head     : ef351e4072fcae74fb1b9ef2a65ebab1babe23ad
   subject  : fs: add CONFIG_FS_HEAPLARGE_ONLY support
   commits  : 1
   base     : f9d3eb13c3 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : ef351e4072
   apps     : 0a37af9866
   options  : CONFIG_FS_TMPFS CONFIG_FS_SHMFS CONFIG_PSEUDOFS_FILE 
CONFIG_FS_HEAPSIZE=262144 CONFIG_FS_HEAPLARGE_ONLY 
CONFIG_EXAMPLES_HEAPLARGECHECK CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=0 
CONFIG_MM_BACKTRACE=0 CONFIG_FS_PREALLOC_FILES=4 CONFIG_FS_ALLOC_FILES=4 
CONFIG_DEBUG_ASSERTIONS
   host     : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
   checkpatch: rc=0
   signer   : jingfei <[email protected]>
   commits missing your Signed-off-by: 0
   make build: rc=0
   warnings in changed files: 0
   ostest: Exiting with status 0
   cmake reconfigure: rc=0
   cmake build: rc=0
   worktree tracked changes after: 0
   RESULT: PASS
   ```
   </details>
   
   <details>
   <summary>Flat fixture configuration options</summary>
   
   ```json
   [
     "CONFIG_EXAMPLES_HEAPLARGECHECK",
     "CONFIG_FS_TMPFS",
     "CONFIG_FS_SHMFS",
     "CONFIG_PSEUDOFS_FILE",
     "CONFIG_PSEUDOFS_FILE_READONLY=n",
     "CONFIG_FS_HEAPSIZE=262144",
     "CONFIG_MM_BACKTRACE=0",
     "CONFIG_MM_BACKTRACE_SEQNO=n",
     "CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=0",
     "CONFIG_SIM_M32=n",
     "CONFIG_DEBUG_ASSERTIONS",
     "CONFIG_DEFAULT_SMALL",
     "CONFIG_COVERAGE_NONE",
     "CONFIG_DEBUG_NOOPT=n",
     "CONFIG_DEBUG_FULLOPT",
     "CONFIG_FS_PREALLOC_FILES=0",
     "CONFIG_FS_ALLOC_FILES=0",
     "CONFIG_MM_DEFAULT_MANAGER"
   ]
   ```
   </details>
   
   <details>
   <summary>Optimized image sizes</summary>
   
   ```json
   {
     "base": {
       "text": 576917,
       "data": 2800,
       "bss": 13056
     },
     "off": {
       "text": 576917,
       "data": 2800,
       "bss": 13056
     },
     "on": {
       "text": 576789,
       "data": 2800,
       "bss": 13056
     },
     "delta-off": {
       "text": 0,
       "data": 0,
       "bss": 0
     },
     "delta-on": {
       "text": -128,
       "data": 0,
       "bss": 0
     }
   }
   ```
   </details>
   
   <details>
   <summary>baseline64-exact build and runtime logs</summary>
   
   ```text
   rev f9d3eb13c3f arch/arm/pp502x: Finish start-up in the common no-MMU entry. 
build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/baseline64-exact/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-pseudo
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-shm
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   ROUND 0 data_heap_bytes=4800
   FILE /heaplarge/data
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-pseudo
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-shm
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   ROUND 1 data_heap_bytes=4800
   FILE /heaplarge/data
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-pseudo
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FILE /heaplarge-shm
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   FAIL metadata heap selection errno=0
   ROUND 2 data_heap_bytes=4800
   METADATA data_heap_bytes before=4800 during=10208 delta=5408
   FAIL descriptors preserve data heap capacity errno=0
   FAIL metadata works when data heap is full errno=12
   EXHAUST blocks=90 metadata_open=-1 resize=-1
   FAIL unmount releases all data allocations errno=2
   HEAPLARGE RESULT checks=1535 failures=33 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>enabled64-exact build and runtime logs</summary>
   
   ```text
   rev ef351e4072f fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/enabled64-exact/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=144
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=144
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=144
   METADATA data_heap_bytes before=144 during=144 delta=0
   EXHAUST blocks=70 metadata_open=4 resize=-1
   HEAPLARGE RESULT checks=1535 failures=0 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>disabled64 build and runtime logs</summary>
   
   ```text
   rev 7c83333cff4 fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/disabled64/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 0
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=4800
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=4800
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=4800
   METADATA data_heap_bytes before=4800 during=10208 delta=5408
   EXHAUST blocks=90 metadata_open=-1 resize=-1
   HEAPLARGE RESULT checks=1532 failures=0 expected_split=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>shared64-v2 build and runtime logs</summary>
   
   ```text
   rev ef351e4072f fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/shared64-v2/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 0
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=1417168
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=1417168
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=1417168
   METADATA data_heap_bytes before=1417168 during=1422576 delta=5408
   HEAPLARGE RESULT checks=1523 failures=0 expected_split=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>tlsf64 build and runtime logs</summary>
   
   ```text
   rev ef351e4072f fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/tlsf64/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=96
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=96
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=96
   METADATA data_heap_bytes before=96 during=96 delta=0
   EXHAUST blocks=70 metadata_open=4 resize=-1
   HEAPLARGE RESULT checks=1535 failures=0 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>enabled32 build and runtime logs</summary>
   
   ```text
   rev ef351e4072f fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/enabled32/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=104
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=104
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=104
   METADATA data_heap_bytes before=104 during=104 delta=0
   EXHAUST blocks=70 metadata_open=4 resize=-1
   HEAPLARGE RESULT checks=1535 failures=0 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>filepool64 build and runtime logs</summary>
   
   ```text
   rev ef351e4072f fs: add CONFIG_FS_HEAPLARGE_ONLY support build rc=0
   run rc=0 
log=/home/mi/test/nuttx_apache/fs-upstream/pr-fs-heap-large/filepool64/run.log
   
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=144
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=144
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=144
   METADATA data_heap_bytes before=144 during=144 delta=0
   EXHAUST blocks=70 metadata_open=4 resize=-1
   HEAPLARGE RESULT checks=1517 failures=0 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>CMake runtime</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> heaplargecheck 1
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 0 data_heap_bytes=176
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 1 data_heap_bytes=176
   FILE /heaplarge/data
   FILE /heaplarge-pseudo
   FILE /heaplarge-shm
   ROUND 2 data_heap_bytes=176
   METADATA data_heap_bytes before=176 during=176 delta=0
   EXHAUST blocks=67 metadata_open=4 resize=-1
   HEAPLARGE RESULT checks=1517 failures=0 expected_split=1
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>Protected and kernel builds</summary>
   
   ```text
   qemu-armv7r:pnsh whole-image build rc=0
   
   qemu-armv8a:knsh whole-image build rc=0
   
   Same configuration except new FS option. Baseline and branch have identical 
five unresolved user-link references; pooled protected configuration is not 
counted as passing.
   ```
   </details>
   
   <details>
   <summary>protected-pool-baseline existing link errors</summary>
   
   ```text
   
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool.c:530:(.text.mempool_allocate+0x15c):
 undefined reference to `nxsem_tickwait_uninterruptible'
   
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:144:(.text.mempool_read+0x70):
 undefined reference to `procfs_snprintf'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:149:(.text.mempool_read+0x8c):
 undefined reference to `procfs_memcpy'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:165:(.text.mempool_read+0x120):
 undefined reference to `procfs_snprintf'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:171:(.text.mempool_read+0x138):
 undefined reference to `procfs_memcpy'
   ```
   </details>
   
   <details>
   <summary>protected existing link errors</summary>
   
   ```text
   
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool.c:530:(.text.mempool_allocate+0x15c):
 undefined reference to `nxsem_tickwait_uninterruptible'
   
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:144:(.text.mempool_read+0x70):
 undefined reference to `procfs_snprintf'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:149:(.text.mempool_read+0x8c):
 undefined reference to `procfs_memcpy'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:165:(.text.mempool_read+0x120):
 undefined reference to `procfs_snprintf'
   arm-none-eabi-ld: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/mm/mempool/mempool_procfs.c:171:(.text.mempool_read+0x138):
 undefined reference to `procfs_memcpy'
   ```
   </details>
   
   <details>
   <summary>Fixture: CMakeLists.txt</summary>
   
   ```text
   if(CONFIG_EXAMPLES_HEAPLARGECHECK)
     nuttx_add_application(
       NAME heaplargecheck
       SRCS heaplargecheck_main.c
       STACKSIZE 16384
       MODULE ${CONFIG_EXAMPLES_HEAPLARGECHECK}
       INCLUDE_DIRECTORIES ${NUTTX_DIR}/fs)
   endif()
   ```
   </details>
   
   <details>
   <summary>Fixture: Kconfig</summary>
   
   ```text
   config EXAMPLES_HEAPLARGECHECK
        tristate "Filesystem data heap allocation check"
        default n
        depends on BUILD_FLAT && FS_TMPFS && FS_SHMFS && PSEUDOFS_FILE
   ```
   </details>
   
   <details>
   <summary>Fixture: Make.defs</summary>
   
   ```text
   ifneq ($(CONFIG_EXAMPLES_HEAPLARGECHECK),)
   CONFIGURED_APPS += $(APPDIR)/examples/heaplargecheck
   endif
   ```
   </details>
   
   <details>
   <summary>Fixture: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   PROGNAME = heaplargecheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 16384
   MODULE = $(CONFIG_EXAMPLES_HEAPLARGECHECK)
   MAINSRC = heaplargecheck_main.c
   CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)/fs
   include $(APPDIR)/Application.mk
   ```
   </details>
   
   <details>
   <summary>Fixture: heaplargecheck_main.c</summary>
   
   ```c
   /****************************************************************************
    * apps/examples/heaplargecheck/heaplargecheck_main.c
    *
    * SPDX-License-Identifier: Apache-2.0
    *
    * 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 <sys/mman.h>
   #include <sys/mount.h>
   #include <sys/stat.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <limits.h>
   #include <malloc.h>
   #include <stdbool.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <unistd.h>
   
   #include <nuttx/fs/fs.h>
   #include <nuttx/mm/mm.h>
   
   #include "fs_heap.h"
   #include "tmpfs/fs_tmpfs.h"
   
   /****************************************************************************
    * Pre-processor Definitions
    
****************************************************************************/
   
   #define TEST_MOUNT "/heaplarge"
   #define TEST_SHM   "/heaplarge-shm"
   #define TEST_PSEUDO "/heaplarge-pseudo"
   
   /****************************************************************************
    * Private Data
    
****************************************************************************/
   
   static unsigned char g_buffer[8192];
   #if CONFIG_FS_HEAPSIZE > 0
   static FAR void *g_blocks[1024];
   #endif
   static int g_checks;
   static int g_failures;
   static bool g_split;
   
   /****************************************************************************
    * Private Functions
    
****************************************************************************/
   
   static void check(bool pass, FAR const char *what)
   {
     g_checks++;
     if (!pass)
       {
         g_failures++;
         printf("FAIL %s errno=%d\n", what, errno);
       }
   }
   
   static bool data_member(FAR void *ptr)
   {
   #if CONFIG_FS_HEAPSIZE > 0
   #  ifdef CONFIG_FS_HEAPLARGE_ONLY
     return fs_large_member(ptr);
   #  else
     return fs_heap_member(ptr);
   #  endif
   #else
     UNUSED(ptr);
     return false;
   #endif
   }
   
   static void check_metadata(FAR void *ptr)
   {
     check(ptr != NULL, "metadata exists");
     if (ptr != NULL)
       {
         check(data_member(ptr) == (CONFIG_FS_HEAPSIZE > 0 && !g_split),
               "metadata heap selection");
       }
   }
   
   static size_t data_used(void)
   {
     struct malltask task;
     struct mallinfo_task info;
   
     memset(&task, 0, sizeof(task));
     task.pid = PID_MM_ALLOC;
   #ifdef CONFIG_MM_BACKTRACE_SEQNO
     task.seqmax = ULONG_MAX;
   #endif
   #if CONFIG_FS_HEAPSIZE > 0
     info = fs_heap_mallinfo_task(&task);
   #else
     info = mallinfo_task(&task);
   #endif
     return info.uordblks;
   }
   
   static void check_file_metadata(int fd, bool tmpfs)
   {
     FAR struct file *filep;
     int ret;
   
     ret = file_get(fd, &filep);
     check(ret == 0, "file reference");
     if (ret == 0)
       {
   #if CONFIG_FS_PREALLOC_FILES == 0 && CONFIG_FS_ALLOC_FILES == 0
         check_metadata(filep);
   #endif
         check_metadata(filep->f_inode);
         check_metadata(tmpfs ? filep->f_priv : filep->f_inode->i_private);
         if (tmpfs)
           {
             FAR struct tmpfs_file_s *tfo = filep->f_priv;
             FAR struct tmpfs_directory_s *parent = tfo->tfo_parent;
   
             check_metadata(parent);
             check(data_member(parent->tdo_entry) == (CONFIG_FS_HEAPSIZE > 0),
                   "TMPFS directory array heap selection");
             check(data_member(tfo->tfo_data) == (CONFIG_FS_HEAPSIZE > 0),
                   "TMPFS data heap selection");
           }
   
         file_put(filep);
       }
   }
   
   static void check_content(int fd, size_t length, unsigned char first)
   {
     unsigned char buf[128];
     ssize_t ret;
     size_t offset;
     size_t i;
   
     for (offset = 0; offset < length; offset += sizeof(buf))
       {
         size_t count = length - offset;
   
         if (count > sizeof(buf))
           {
             count = sizeof(buf);
           }
   
         ret = pread(fd, buf, count, offset);
         check(ret == count, "read length");
         if (ret != count)
           {
             return;
           }
   
         for (i = 0; i < count; i++)
           {
             if (buf[i] != (offset + i == 0 ? first : 0x5a))
               {
                 check(false, "preserved data");
                 return;
               }
           }
       }
   
     check(true, "preserved data");
   }
   
   static void exercise_file(FAR const char *path, bool shm, bool tmpfs)
   {
     FAR unsigned char *mapped;
     struct stat st;
     unsigned char value;
     int fd;
     int copy;
     int ret;
   
     printf("FILE %s\n", path);
     fd = shm ? shm_open(path, O_CREAT | O_RDWR, 0600) :
                open(path, O_CREAT | O_RDWR | O_TRUNC, 0600);
     check(fd >= 0, "open test file");
     if (fd < 0)
       {
         return;
       }
   
     check(ftruncate(fd, sizeof(g_buffer)) == 0, "initial resize");
     memset(g_buffer, 0x5a, sizeof(g_buffer));
     check(pwrite(fd, g_buffer, sizeof(g_buffer), 0) == sizeof(g_buffer),
           "write full payload");
     check_file_metadata(fd, tmpfs);
     check_content(fd, sizeof(g_buffer), 0x5a);
     mapped = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     check(mapped != MAP_FAILED, "map payload");
     if (mapped != MAP_FAILED)
       {
         check(data_member(mapped) == (CONFIG_FS_HEAPSIZE > 0),
               "mapped data heap selection");
         mapped[0] = 0xa5;
         check(pread(fd, &value, 1, 0) == 1 && value == 0xa5,
               "mapped data is shared");
         check(munmap(mapped, 4096) == 0, "unmap payload");
       }
   
     if (tmpfs)
       {
         mapped = mmap(NULL, 16384, PROT_READ, MAP_SHARED, fd, 0);
         check(mapped != MAP_FAILED, "grow buffer for mapping");
         if (mapped != MAP_FAILED)
           {
             check(data_member(mapped) == (CONFIG_FS_HEAPSIZE > 0),
                   "expanded mapping heap selection");
             check(mapped[8192] == 0 && mapped[16383] == 0,
                   "mapping extension zeroed");
             check(munmap(mapped, 16384) == 0, "unmap expanded buffer");
           }
       }
   
     copy = dup(fd);
     check(copy >= 0, "duplicate descriptor");
     if (copy >= 0)
       {
         check_content(copy, 1024, 0xa5);
         check(close(copy) == 0, "close duplicate");
       }
   
     check(ftruncate(fd, 1024) == 0, "shrink payload");
     check(ftruncate(fd, 32768) == 0, "regrow payload");
     check_content(fd, 1024, 0xa5);
     check(pread(fd, &value, 1, 32000) == 1 && value == 0,
           "regrowth zeroed");
     check(fstat(fd, &st) == 0 && st.st_size == 32768, "grown length");
     if (tmpfs || shm)
       {
         check(ftruncate(fd, 0) == 0, "release all payload");
         check(ftruncate(fd, 4096) == 0, "allocate after zero length");
       }
   
     ret = shm ? shm_unlink(path) : unlink(path);
     check(ret == 0, "unlink while open");
     check(fstat(fd, &st) == 0, "open object survives unlink");
     check(close(fd) == 0, "release unlinked object");
   }
   
   static void exercise_directories(void)
   {
     char path[64];
     int i;
     int fd;
   
     check(mkdir(TEST_MOUNT "/a", 0700) == 0, "create first directory");
     check(mkdir(TEST_MOUNT "/b", 0700) == 0, "create second directory");
     for (i = 0; i < 32; i++)
       {
         snprintf(path, sizeof(path), TEST_MOUNT "/a/f%d", i);
         fd = open(path, O_CREAT | O_RDWR, 0600);
         check(fd >= 0, "grow directory array");
         if (fd >= 0)
           {
             check(write(fd, "data", 4) == 4, "directory member payload");
             check(close(fd) == 0, "close directory member");
           }
       }
   
     check(rename(TEST_MOUNT "/a/f0", TEST_MOUNT "/a/f1") == 0,
           "replace closed file");
     check(rename(TEST_MOUNT "/a", TEST_MOUNT "/b") == 0,
           "replace empty directory");
     for (i = 1; i < 32; i++)
       {
         snprintf(path, sizeof(path), TEST_MOUNT "/b/f%d", i);
         check(unlink(path) == 0, "remove directory member");
       }
   
     check(rmdir(TEST_MOUNT "/b") == 0, "release directory array");
   }
   
   static void exercise_descriptors(void)
   {
     int fds[64];
     size_t before;
     size_t during;
     int i;
   
     before = data_used();
     for (i = 0; i < 64; i++)
       {
         fds[i] = open("/dev/null", O_RDWR);
         check(fds[i] >= 0, "allocate descriptor metadata");
       }
   
     during = data_used();
     printf("METADATA data_heap_bytes before=%zu during=%zu delta=%zd\n",
            before, during, (ssize_t)(during - before));
   #if CONFIG_FS_HEAPSIZE > 0
     if (g_split)
       {
         check(during == before, "descriptors preserve data heap capacity");
       }
   #endif
   
     for (i = 0; i < 64; i++)
       {
         if (fds[i] >= 0)
           {
             check(close(fds[i]) == 0, "release descriptor metadata");
           }
       }
   }
   
   static void exercise_exhaustion(void)
   {
   #if CONFIG_FS_HEAPSIZE > 0
     static const size_t sizes[] =
     {
       4096, 512, 64, 1
     };
   
     struct stat st;
     unsigned char value;
     size_t i;
     int count = 0;
     int fd;
     int extra;
     int resize;
   
     fd = open(TEST_MOUNT "/full", O_CREAT | O_RDWR, 0600);
     check(fd >= 0, "create exhaustion test file");
     if (fd < 0)
       {
         return;
       }
   
     check(write(fd, "x", 1) == 1, "seed exhaustion test data");
     for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
       {
         while (count < 1024)
           {
   #ifdef CONFIG_FS_HEAPLARGE_ONLY
             g_blocks[count] = fs_large_malloc(sizes[i]);
   #else
             g_blocks[count] = fs_heap_malloc(sizes[i]);
   #endif
             if (g_blocks[count] == NULL)
               {
                 break;
               }
   
             count++;
           }
       }
   
     extra = open("/dev/null", O_RDWR);
     resize = ftruncate(fd, CONFIG_FS_HEAPSIZE * 2);
     check(count < 1024, "exhausted data heap before fixture capacity");
     if (g_split)
       {
         check(extra >= 0, "metadata works when data heap is full");
       }
   
     check(resize < 0, "data growth fails when data heap is full");
     check(fstat(fd, &st) == 0 && st.st_size == 1,
           "failed growth preserves length");
     check(pread(fd, &value, 1, 0) == 1 && value == 'x',
           "failed growth preserves content");
     printf("EXHAUST blocks=%d metadata_open=%d resize=%d\n",
            count, extra, resize);
     if (extra >= 0)
       {
         close(extra);
       }
   
     while (count > 0)
       {
   #ifdef CONFIG_FS_HEAPLARGE_ONLY
         fs_large_free(g_blocks[--count]);
   #else
         fs_heap_free(g_blocks[--count]);
   #endif
       }
   
     check(ftruncate(fd, 8192) == 0, "data heap recovers after exhaustion");
     check(close(fd) == 0, "close exhaustion file");
     check(unlink(TEST_MOUNT "/full") == 0, "remove exhaustion file");
   #endif
   }
   
   static void populate_unmount(void)
   {
     int fd;
   
     check(mkdir(TEST_MOUNT "/remaining", 0700) == 0,
           "create directory for unmount");
     fd = open(TEST_MOUNT "/remaining/data", O_CREAT | O_RDWR, 0600);
     check(fd >= 0, "create file for unmount");
     if (fd >= 0)
       {
         check(ftruncate(fd, 8192) == 0, "allocate data for unmount");
         check(close(fd) == 0, "leave closed file for unmount");
       }
   }
   
   /****************************************************************************
    * Public Functions
    
****************************************************************************/
   
   int main(int argc, FAR char *argv[])
   {
     struct stat st;
     size_t initial;
     size_t previous = 0;
     size_t current;
     int round;
   
     g_checks = 0;
     g_failures = 0;
     g_split = argc > 1 && atoi(argv[1]) != 0;
     initial = data_used();
     check(mkdir(TEST_MOUNT, 0700) == 0, "create mountpoint");
     if (mount(NULL, TEST_MOUNT, "tmpfs", 0, NULL) < 0)
       {
         check(false, "mount TMPFS");
         return 1;
       }
   
     for (round = 0; round < 3; round++)
       {
         exercise_file(TEST_MOUNT "/data", false, true);
         exercise_file(TEST_PSEUDO, false, false);
         exercise_file(TEST_SHM, true, false);
         exercise_directories();
         current = data_used();
         printf("ROUND %d data_heap_bytes=%zu\n", round, current);
         if (round > 0)
           {
             check(current == previous, "repeated lifecycle releases memory");
           }
   
         previous = current;
       }
   
     exercise_descriptors();
     exercise_exhaustion();
     populate_unmount();
     check(umount(TEST_MOUNT) == 0, "unmount TMPFS");
     check(stat(TEST_MOUNT, &st) < 0 && errno == ENOENT,
           "unmount removes mountpoint");
     if (g_split)
       {
         check(data_used() == initial, "unmount releases all data allocations");
       }
   
     printf("HEAPLARGE RESULT checks=%d failures=%d expected_split=%d\n",
            g_checks, g_failures, g_split);
     return g_failures ? 1 : 0;
   }
   ```
   </details>
   
   The fixture lives in `apps/examples/heaplargecheck`; the attached Make.defs,
   Makefile, CMakeLists.txt and Kconfig register the `heaplargecheck` command.
   Use `heaplargecheck 1` to check the split policy, or `heaplargecheck 0` for
   normal/shared heap selection. Configuration and compiler details are recorded
   in the local validation log above.
   
   ## PR verification Self-Check
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] My PR adheres to Contributing Guidelines and Documentation.
   * [ ] My PR is still work in progress (not ready for review).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to