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

Use custom version of atoi() to eliminate dependency on newlib's locale and save RAM #323

Closed
dpgeorge opened this issue Nov 15, 2017 · 3 comments

Comments

@dpgeorge
Copy link
Contributor

As discussed over at the MicroPython port to the micro:bit bbcmicrobit/micropython#363, the latest version of newlib (2.5.0) introduced some new locale handling code and has a large (~350 bytes) __global_locale data structure that is not constant and lives in RAM. On the micro:bit 350 bytes of RAM is a lot and it would be good to reclaim this.

Upstream at newlib they say that the only way around this is to add an option to newlib to completely disable the locale stuff, see https://sourceware.org/ml/newlib/2017/msg01041.html

Since that option might not be feasible for us (it requires a custom newlib build) I found another way: the only place in the DAL that uses the locale-dependent code (at least in the MicroPython port) is in MicroBitImage::MicroBitImage(const char *s) when it calls atoi(). By simply replacing atoi() with a call to a custom version of this function (eg my_atoi()) that has a very simple implementation, one can reclaim 364 bytes of RAM and around 600 bytes of flash/ROM.

Is this a modification (changing atoi to my_atoi) that would be accepted in the DAL?

@finneyj
Copy link
Contributor

finneyj commented Nov 15, 2017

Hi @dpgeorge

Yes, of course. No problem at all - a natural home would be here:
https://github.com/lancaster-university/microbit-dal/blob/master/source/core/MicroBitCompat.cpp

Do you have a simple implementation to hand? If not, I have a few hundred minions learning C at the moment that could sharpen their teeth on this... They'll do anything for a mars bar.

@dpgeorge
Copy link
Contributor Author

Please see above PR for an idea to remove atoi(). Since the code calling atoi() was almost parsing the number already, I just adjusted it so it did construct the integer value.

@finneyj
Copy link
Contributor

finneyj commented Dec 19, 2017

yup - now merged, so closing this off. Thanks @dpgeorge.

@finneyj finneyj closed this as completed Dec 19, 2017
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