Skip to content

pkg/baselibc: [WIP] a light-weight C library#10292

Closed
jcarrano wants to merge 2 commits intoRIOT-OS:masterfrom
jcarrano:baselibc-initial
Closed

pkg/baselibc: [WIP] a light-weight C library#10292
jcarrano wants to merge 2 commits intoRIOT-OS:masterfrom
jcarrano:baselibc-initial

Conversation

@jcarrano
Copy link
Contributor

@jcarrano jcarrano commented Oct 29, 2018

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.

printf is 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-uart would 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:

  1. Compile hello world (BOARD=samr21-xpro WERROR=0 make
  2. Get the binary (arm-none-eabi-objcopy -O binary bin/samr21-xpro/hello-world.elf new.bin)
  3. In the makefile, comment the line that says USEPKG+=baselibc.
  4. Repeat (1) and (2)- use old.bin or another filename.
  5. Compare sizes:
$ 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.bin

Related PRs

To integrate this properly, #9258 would be really helpful.

@jcarrano jcarrano added State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT Area: pkg Area: External package ports labels Oct 29, 2018
@kaspar030
Copy link
Contributor

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.

@jcarrano
Copy link
Contributor Author

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.

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.
@stale
Copy link

stale bot commented Aug 10, 2019

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.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@stale stale bot closed this Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: pkg Area: External package ports State: stale State: The issue / PR has no activity for >185 days State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants