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

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
shazz
Posts: 46
Joined: Tue Apr 30, 2019 6:35 pm
Contact:

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

Post by shazz » Mon May 13, 2019 2:58 pm

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
Last edited by shazz on Wed May 15, 2019 5:21 pm, edited 1 time in total.
8bits should be enough...

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

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

Post by jimmo » Mon May 13, 2019 11:59 pm

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++.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » Tue May 14, 2019 9:59 am

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).

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

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

Post by jimmo » Tue May 14, 2019 11:03 am

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 :) )

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

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

Post by stijn » Tue May 14, 2019 11:12 am

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).

User avatar
shazz
Posts: 46
Joined: Tue Apr 30, 2019 6:35 pm
Contact:

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

Post by shazz » Wed May 15, 2019 5:21 pm

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 !
8bits should be enough...

Post Reply