Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded Devices / Nim Memory Management #96

Open
davidvfx07 opened this issue Mar 22, 2024 · 3 comments
Open

Embedded Devices / Nim Memory Management #96

davidvfx07 opened this issue Mar 22, 2024 · 3 comments

Comments

@davidvfx07
Copy link

I am working on a project to get Nim with Arduino libs running on an ESP32 and have had a few minor successes, though successes nonetheless. I am looking to further the project with Arduino/C/C++ libraries such as LVGL and TFT_ESPI because of the support and density of the Arduino catalogue. The ability to auto-generate wrappers for these libraries and add destructors for Nim-managed memory really enticed me toward Futhark, as I, coming from years of Python, have never managed memory in my life, and would like to keep it that way. So my questions are:

A) Is Futhark on embedded possible? I get nasty errors like "Error: OS module not ported to your operating system!" or "Error: Port memory manager to your platform."
B) If so, how would one go about incorporating destructors with generated wrappers to keep memory management in the hands of Nim?

Thank you PMunch for this project and your endless contributions to Nim. I can't google a query about Nim without your name showing up lol.

@PMunch
Copy link
Owner

PMunch commented Mar 22, 2024

A) Futhark on embedded is definitely possible. I'm using it for developing embedded software at work. The errors you face is Nim not quite understanding that the host OS and target OS (or lack thereof) are not the same. So a lot of the features Futhark needs which is available on the host OS are disabled when compiling for an embedded target. This of course isn't actually an issue since Futhark runs completely on compile-time, but Nim still fights you. The trick is to create a small dummy file in Nim, with all the needed flags and options to the Clang compiler you would use to compile your libraries. Then compile this dummy file and copy the Futhark cache from the cache folder, or use the outputPath option for Futhark to place it locally. Then when you have your complete Futharked wrapper you can import that into your embedded project. I'm currently working on a "project mode" which is what I use at work which takes a folder and wraps each file in that folder with links between the modules etc. This means you wrap once and then import like you would in C instead of handling imports through Futhark. It's a bit more ergonomic to work with on embedded.

B) There is a section in the README about adding destructors. I also added them in my MAPM wrapper (linked in the README) if you want a practical example.

Thank you for the kind words, it means a lot to me that people find my work useful!

@davidvfx07
Copy link
Author

Ha! Yes, all I needed to do was run Futhark on host completely dethatched from the embedded project. Thank you! On destructors, since I have no background in memory management, I don't even know where to start with when/why/how to add destructors. Are there any resources that would help someone like me with no experience?

PS. Your welcome! You truly deserve it!

@PMunch
Copy link
Owner

PMunch commented Mar 22, 2024

Hmm, it's quite a complicated topic. I'm not aware of any simple resources for learning this stuff. But generally speaking most libraries tend to have create/free function pairs for each of its types. So it's mostly a matter of adding destructors to call the free functions, and of course wrapping the create functions in something more ergonomic. Of course it can be hard to get right, so learning how to use a memory tracking tool like Valgrind can be of great help. If you do come across some easy resources I'd be interested in looking at them out of curiosity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants