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
[SOLVED] Possible to use cpp to create a new module?
[SOLVED] Possible to use cpp to create a new module?
Last edited by shazz on Wed May 15, 2019 5:21 pm, edited 1 time in total.
8bits should be enough...
Re: Possible to use cpp to create a new module?
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++.
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?
In practice I think the only place this is needed is where code compiled as C++ includes the micropython headers.
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.then ensure that if any of the C++ code uses new/delete that they are forwarded to the mpy heap allocation methods
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).Also you'll probably want to disable exceptions in the C++ code.
Re: Possible to use cpp to create a new module?
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?
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 !
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...