pkg/baselibc: [WIP] a light-weight C library#10292
pkg/baselibc: [WIP] a light-weight C library#10292jcarrano wants to merge 2 commits intoRIOT-OS:masterfrom
Conversation
|
Yup, the libc has quite some optimization potential. Even better, it is much easier to use the same libc on all platforms, if the C library is a little more approachable. I experimented with actually replacing newlib, as most RIOT applications use only a super small subset of libc. |
|
The nice thing about this one is that you don't need to replace anything (in fact, it needs the toolchain-supplied library to provide stdint.h, stddef.h, etc). Filesystem-related stuff is missing, for example, though most apps won't even use that. I thing it gives a good "bang for the buck" in terms of benefits vs complexity. I would't expect everything to work 100% the same with this libc (especially code that uses string formatting) but enabling and disabling it is just a matter of enabling or disabling a package. I don't like the all the extra indirection in putchar, where it could consist of just sending a char to the UART: __extern_inline size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
{
if (stream->vmt->write == NULL) return 0;
return stream->vmt->write(stream, (char*)buf, size*nmemb) / size;
}
__extern_inline int fputc(int c, FILE *f)
{
unsigned char ch = c;
return fwrite(&ch, 1, 1, f) == 1 ? ch : EOF;
}
#define putchar(c) fputc((c),stdout)The next step (after properly implementing the UART callbacks) is copying MyNewt's version of this lib and diffing it to see what they changed. |
a2d6a62 to
7faad6c
Compare
9269669 to
6e6d885
Compare
baselibc is a C library derived from klibc (without the GPL code). It is lightweight, at the expense of performance.
Throwaway, quick-n-dirty commit to evalate FLASH savings from baselibc.
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
Contribution description
I stole the C library from MyNewt, see https://mynewt.apache.org/latest/os/modules/baselibc.html . This is still a WIP, I'm publishing early to see if there is interest.
33% reduction in code size for hello world
Baselibc is derived from klibc (part of the linux kernel initialization system) with the GPL stuff removed. It does not replace newlib. Rather, it replaces several of it's functions by simpler, smaller and probably less efficient versions.
printfis based on tinyprintf, so many things won't be supported. The version in mynewt is slightly different from the one used here, so I might try pulling in those changes (their version has optional float support).The interesting thing is that
stdio-uartwould need only small modifications to work both with and without this library (compare the stdio_read2, stdio_write2 functions I placed in main.c with the ones in stdio-uart).Testing procedure
I modified hello-world so that it uses this package:
BOARD=samr21-xpro WERROR=0 makearm-none-eabi-objcopy -O binary bin/samr21-xpro/hello-world.elf new.bin)USEPKG+=baselibc.old.binor another filename.$ ls -l *.bin -rwxr-xr-x 1 jcarrano jcarrano 5724 oct 29 17:19 new.bin -rwxr-xr-x 1 jcarrano jcarrano 8572 oct 29 17:18 old.binRelated PRs
To integrate this properly, #9258 would be really helpful.