Skip to content

Conversation

@Shanks0224
Copy link

Summary

The simulation platform previously used a custom heap implementation that
lacked support for standard memory management features. This prevented
the use of shared memory mechanisms and consistent heap
debugging across platforms.

This patch migrates arch/sim to use the standard umm_heap framework while
maintaining necessary platform-specific customizations. The changes enable:

  1. Standard heap management with shared memory support
  2. Accurate memory statistics through timely delayed free processing
  3. Extensible procfs interface for memory debugging
  4. Proper accounting of heap metadata

Similar implementations exist in other platforms which use the umm_heap framework
with platform-specific hooks.

Impact

  • Users: Enables shared memory features (OpenAMP) on simulation platform
  • Build: No impact on build process; changes are conditional
  • Compatibility: Maintains backward compatibility with existing sim code
  • Security: No security impact; heap management internal to kernel

Testing

Host: Ubuntu 22.04 x86_64
Toolchain: gcc 11.4.0
Configuration: sim:nsh with MM_UMM_CUSTOMIZE_MANAGER enabled

Custom application testing

Created a test application (hello) to verify heap functionality:

Source Code

/****************************************************************************
 * apps/examples/hello/hello_main.c
 ****************************************************************************/

#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, FAR char *argv[])
{
  printf("Hello, World!!!\n");
  char *p = (char *)malloc(100);
  p[50] = 'a';
  printf("p[50] = %c\n", p[50]);
  printf("p = %p\n", p);
  free(p);
  return 0;
}

Build and Run

cmake -B build_sim -DBOARD_CONFIG=boards/sim/sim/sim/configs/nsh -GNinja
cmake --build build_sim
gdb-multiarch build_sim/nuttx

Output

nsh> hello
Hello, World!!!
p[50] = a
p = 0x401bcdf0
nsh>

Results

✅ malloc(100) returns valid pointer 0x401bcdf0
✅ Memory write p[50] = 'a' succeeds
✅ Memory read outputs correct value a
✅ free(p) executes without crash
✅ Application exits cleanly

This commit enables proper heap management on the simulation platform
by migrating from custom heap implementation to the standard umm heap
framework:

1. mm/umm: Allow customizing umm heap implementation
   - Add hooks for platform-specific heap customization
   - Enable arch/sim to use standard umm_heap while retaining flexibility

2. arch/sim: Migrate to standard umm heap
   - Replace custom mm_heap implementation with umm_heap
   - Improve heap initialization and management in simulation environment
   - Enable shared memory support (e.g., for OpenAMP scenarios)

3. mm: Ensure accurate heap statistics for sim
   - Move mm_free_delaylist to mm_mallinfo for timely statistics
   - Guarantee accurate memory information when queried

4. fs/procfs: Add extensible memory info hooks
   - Introduce mallinfo and memdump callbacks
   - Allow sim platform to customize memory reporting through procfs

5. mm/tlfs: Account initial heap structure size
   - Properly include sizeof(struct mm_heap_s) in TLFS initialization
   - Ensure accurate available memory calculation for sim

These changes unify heap management across platforms while providing
the flexibility needed for simulation environments, enabling features
like shared memory support and accurate memory debugging on sim.

Signed-off-by: ganjing <ganjing@xiaomi.com>
@github-actions github-actions bot added Arch: simulator Issues related to the SIMulator Area: File System File System issues Area: Memory Management Memory Management issues Board: simulator Size: L The size of the change in this PR is large labels Jan 21, 2026
@acassis
Copy link
Contributor

acassis commented Jan 21, 2026

@Shanks0224 it is a good practice to separate logic implementation from board support. @xiaoxiang781216 please take a look

@Shanks0224
Copy link
Author

@GUIDINGLI PTAL

@xiaoxiang781216
Copy link
Contributor

@Shanks0224 remove merge patch from pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Area: File System File System issues Area: Memory Management Memory Management issues Board: simulator Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants