Allocate the emergency exception buffer more than once

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Allocate the emergency exception buffer more than once

Post by fstengel » Thu Apr 16, 2020 2:46 pm

Just an odd question. What happens if one allocates the emergency exception buffer (via alloc_emergency_exception_buf) more than once?
Is the memory used for the first allocation lost, reallocated or anything else?

I ask this since, as a sloppy programmer, I sometimes use alloc_emergency_exception_buf more than once in various modules, rather than once in, say, boot.py

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Allocate the emergency exception buffer more than once

Post by jimmo » Thu Apr 16, 2020 11:26 pm

fstengel wrote:
Thu Apr 16, 2020 2:46 pm
Is the memory used for the first allocation lost, reallocated or anything else?
alloc_emergency_exception_buf allocated the emergency exception buffer in the GC heap. So yeah it just grabs a new buffer, then for good measure it also frees the old one (which means it's available for reuse immediately without a collection taking place).

https://github.com/micropython/micropyt ... cept.c#L89

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: Allocate the emergency exception buffer more than once

Post by fstengel » Fri Apr 17, 2020 7:37 am

Thanks.The source code is rather clear. The only thing is that I would not have found it alone: sometimes one can be overwhelmed by the amount of files to search into.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Allocate the emergency exception buffer more than once

Post by jimmo » Fri Apr 17, 2020 9:24 am

jimmo wrote:
Thu Apr 16, 2020 11:26 pm
The only thing is that I would not have found it alone
If it helps, two approaches to do this quickly:

- Almost all modules "foo" are modfoo.c. My editor has fast goto-file, so modmicropython.c
- From there search for "alloc_em" which tells me the name of the method.
- In this case it's in a different file (objexcept.c) but a global search founs that quickly.

Or:
- Anything that ends up being a string "foo" in the firmware (i.e. the name of the method) must be MP_QSTR_foo.
- Global search for MP_QSTR_alloc_em (this gets you to modmicropython.c).

I use my editors jump-to-file, and either "git grep" or "ag" (silver searcher) for global search.

Post Reply