Page 1 of 1

Allocate the emergency exception buffer more than once

Posted: Thu Apr 16, 2020 2:46 pm
by fstengel
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

Re: Allocate the emergency exception buffer more than once

Posted: Thu Apr 16, 2020 11:26 pm
by jimmo
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

Re: Allocate the emergency exception buffer more than once

Posted: Fri Apr 17, 2020 7:37 am
by fstengel
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.

Re: Allocate the emergency exception buffer more than once

Posted: Fri Apr 17, 2020 9:24 am
by jimmo
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.