Skip to content

esp32/main: Store native code as linked list instead of list on GC heap. #15589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dpgeorge
Copy link
Member

@dpgeorge dpgeorge commented Aug 3, 2024

Summary

Finalisers that run during gc_sweep_all() may run native code, for example if an open file is closed and the underlying block device is implemented in native code, then the filesystem driver (eg FAT) may call into the native code.

Therefore, native code must be freed after the call to gc_sweep_all(). That can only be achieved if the GC heap is not used to store the list of allocated native code blocks. Instead, this commit makes the native code blocks a linked list.

Testing

Tested on an ESP32C3, running the native tests as per #15551:

./run-tests.py --target esp32 --device /dev/ttyUSB0 --via-mpy --emit native --mpy-cross-flags="-march=rv32imc" -d basics extmod

All the tests pass.

Note: requires #15573, #15575.

Trade-offs and Alternatives

This approach uses 4 bytes extra iRAM per native function, but keeps the code simple and understandable. An alternative would be to still use the GC heap to store the list of native memory blocks, and make gc_sweep_all() only call the finalisers, not actually reclaim/clear memory. That's a complex change to make though.

Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

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

This approach looks good to me!

Finalisers that run during `gc_sweep_all()` may run native code, for
example if an open file is closed and the underlying block device is
implemented in native code, then the filesystem driver (eg FAT) may call
into the native code.

Therefore, native code must be freed after the call to `gc_sweep_all()`.
That can only be achieved if the GC heap is not used to store the list of
allocated native code blocks.  Instead, this commit makes the native code
blocks a linked list.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the esp32-native-code-linked-list branch from 34355a7 to 5ff6c12 Compare August 7, 2024 04:56
@dpgeorge dpgeorge merged commit 5ff6c12 into micropython:master Aug 7, 2024
8 checks passed
@dpgeorge dpgeorge deleted the esp32-native-code-linked-list branch August 7, 2024 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants