Skip to content

Conversation

@casaroli
Copy link
Contributor

@casaroli casaroli commented Nov 17, 2025

Description

In embedded systems with external PSRAM, we usually want to store our objects in two different heaps: one system heap for small and fast objects (like the internal SRAM); and another user heap or application heap which is used for large objects stored in external slow PSRAM.

This adds the user heap interface in ocre, and sets WAMR to use it as the application heap. We also use it to store the container parameter as well as the container image executable for running in WAMR.

This ultimately removes the need for the two build time configuration variables and make them automatically use the external heap.

In MMU systems, we usually only have one heap, so user_malloc() does the same as core_malloc().

TODO: user_realloc() must be correclty implemented for Zephyr.

Type of change

Please delete options that are not relevant.

  • Code cleanup/refactoring
  • This change requires a documentation update - maybe?
  • CI system update
  • Test Coverage update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration including the following required details.

Test Configuration (required):

  • Developer Board make & model: pico_plus2

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

the user_malloc and user_free functions will be used on platforms
with support for external PSRAM. This way we can differentiate
allocations in the internal or external RAM.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
These functions allocate memory on external PSRAM on boards that
have CONFIG_SHARED_MULTI_HEAP enabled.

Otherwise, they work just like core_malloc and core_free.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
On systems with a single heap, these functions work exactly the
same as malloc and free.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The user heap is user for large objects.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Use the user heap for wamr and related container memory.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
In favor of the user heap mechanism.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
We now use the user heap mechanism.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli casaroli changed the title Shared multi heap user heap using shared multi heap on Zephyr Nov 17, 2025
@casaroli casaroli changed the title user heap using shared multi heap on Zephyr WIP: user heap using shared multi heap on Zephyr Nov 17, 2025
memset(&init_args, 0, sizeof(RuntimeInitArgs));
init_args.mem_alloc_type = Alloc_With_Pool;
init_args.mem_alloc_option.pool.heap_buf = wamr_heap_buf;
init_args.mem_alloc_option.pool.heap_size = sizeof(wamr_heap_buf);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use system heap in systems where we don't have PSRAM for wasm, then we can't limit it anymore. I don't think it's good - we need a way to limit max amount of warm's linear memory, don't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should enable WASM_MEM_ALLOC_WITH_USAGE and limit the amount of memory per-container instead. This is just the allocation. Every use of user_malloc should properly handle the ENOMEM error and recover gracefully if possible.

@kr-t
Copy link
Collaborator

kr-t commented Nov 18, 2025

Discussion notes:

  1. implement shared_mem_realloc similar to other shared/sys wrappers:
    https://docs.zephyrproject.org/apidoc/latest/multi__heap_8h.html
    multi_heap: support for realloc zephyrproject-rtos/zephyr#77174
  2. use malloc/free instead of k_malloc/k_free for non kernel stuff

@casaroli
Copy link
Contributor Author

casaroli commented Jan 9, 2026

Closing this in favor of #116

@casaroli casaroli closed this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants