Repository navigation
Consider memory alloc API with explicit size param for m_free() #2
Copy link
Copy link
Closed
Description
Activity
Good idea.
In the current garbage collector (that runs properly only on the MCU), the object size is implicitly known. There is a bitmap at the very start of the heap which indicates which blocks (32 bytes each) are allocated. So an object size can be computed by working out how many blocks are contiguously allocated. There is also a bit to indicate "start of run of blocks".
Okay, I've implemented it as you suggested, with an explicit size parameter for m_free and m_realloc. The macros you should use are m_del, m_del_obj and m_renew.
- added a commit that references this issue
on Aug 28, 2016 - added a commit that references this issue
on Oct 16, 2016 - added a commit that references this issue
on Oct 6, 2017 - added a commit that references this issue
on Jan 11, 2018 - added a commit that references this issue
on Aug 16, 2018 - added a commit that references this issue
on Jan 15, 2019 - added a commit that references this issue
on Nov 25, 2019 - added a commit that references this issue
on Mar 24, 2020 5 remaining items
- added a commit that references this issue
on May 19, 2020 - added 4 commits that reference this issue
on Nov 22, 2020 - added a commit that references this issue
on May 30, 2021 - added a commit that references this issue
on Jul 16, 2021 - added a commit that references this issue
on Aug 26, 2021 - added a commit that references this issue
on Apr 16, 2023 - added a commit that references this issue
on Feb 12, 2024 - added a commit that references this issue
on Nov 21, 2025 - added a commit that references this issue
on May 23, 2026 - added a commit that references this issue
on Aug 29, 2026
Metadata
Metadata
Assignees
Labels
No labels
When dealing with interpreters, object size size is either implicitly known (for example, basic representation of object has fixed size, like 8 or 32 bytes), or size is stored explicitly on interpreter's level (for example, array size needs to be stored in object header anyway). This means that it may be possible to optimize low-level memory allocation system by not storing memory chunk size (thus saving memory), instead relying that higher levels will pass size explicitly.
Quick look at current sources doesn't show that MicroPython is ideally suited for such optimization, but that's why I write - to propose to add that as a (non-immediate) design goal.
First steps towards that can be simple: make m_free() have signature m_free(ptr, size), and m_realloc(ptr, old_size, new_size), then while going over code to adjust call sites, see if what to pass as "size" param is obvious. In case it's not, well, pass 0, and leave larger refactors to someone who really will implement alternative allocators. Ultimately, there're good reasons for having explicit size field for all variable-length objects, IMHO.