Page 1 of 1

[SOLVED] Possible to use cpp to create a new module?

Posted: Mon May 13, 2019 2:58 pm
by shazz
Hi,

On top of the low level module Framebuf, I'd like to add software based 3D engine module (which will use Framebuf for rendering).
Some years ago I use GNUGL but this is a portable cpp library.

Concerning the memory allocation on micropython, is it possible, not discouraged, to use some cpp code or I would need to port in to C ?
The GNUGL lib: https://github.com/shazz/e60/tree/master/gnugl

Thanks !

shazz

Re: Possible to use cpp to create a new module?

Posted: Mon May 13, 2019 11:59 pm
by jimmo
I imagine that it's not likely that you'd be able to upstream it, but for your own fork doesn't seem like a big problem.

You'll have to carefully figure out where your C / C++ boundary is so that you can make a bunch of "extern C" accessors, then ensure that if any of the C++ code uses new/delete that they are forwarded to the mpy heap allocation methods (and everything is discoverable from the root pointers, which should be fine). Also you'll probably want to disable exceptions in the C++ code.

FYI kind of the reverse situation but the micro:bit port github.com/bbcmicrobit/micropython uses c++.

Re: Possible to use cpp to create a new module?

Posted: Tue May 14, 2019 9:59 am
by stijn
jimmo wrote:
Mon May 13, 2019 11:59 pm
You'll have to carefully figure out where your C / C++ boundary is so that you can make a bunch of "extern C" accessors
In practice I think the only place this is needed is where code compiled as C++ includes the micropython headers.
then ensure that if any of the C++ code uses new/delete that they are forwarded to the mpy heap allocation methods
Not necessarily (plus that might be a lof of work): you can store arbitrary content in your MicroPython type, so if that happens to be something allocated by new() and you call delete() again in __exit__ or __del__ things are good.
Also you'll probably want to disable exceptions in the C++ code.
Alternatively wrap all calls to C++ land in try/catch and then you can raise a MicroPython exception in the catch block (or deal with the exception in another way).

Re: Possible to use cpp to create a new module?

Posted: Tue May 14, 2019 11:03 am
by jimmo
stijn wrote:
Tue May 14, 2019 9:59 am
Not necessarily (plus that might be a lof of work): you can store arbitrary content in your MicroPython type, so if that happens to be something allocated by new() and you call delete() again in __exit__ or __del__ things are good.
There's no heap for new/delete (aka system malloc/free) to allocate on. In the same way you can't use regular `malloc`.

And it's not much work -- I'm basically proposing you just overload global operator new/delete to point to m_malloc / m_free.

(This may be a moot point, I haven't checked if the library in question uses new/delete :) )

Re: Possible to use cpp to create a new module?

Posted: Tue May 14, 2019 11:12 am
by stijn
jimmo wrote:
Tue May 14, 2019 11:03 am
There's no heap for new/delete (aka system malloc/free) to allocate on. In the same way you can't use regular `malloc`.
Yeah but that's mostly the case if MicroPython runs on bare metal (couldn't derive from OP's post whether that is the case or not).

Re: Possible to use cpp to create a new module?

Posted: Wed May 15, 2019 5:21 pm
by shazz
Ok.

I did not expect to code a module in CPP but the module would use the CPP library probably wrapped with a C interface.
So I guess in this case, I would be outside the MicroPython headers but the memory management needs to be overloaded as you suggested.

I'll try that... or use a C library :)

Thanks !