LibC - libc replacement/supplement

Working libraries, libraries being ported and related hardware
Post Reply
Posts: 111
Joined: Thu May 31, 2018 5:39 pm

LibC - libc replacement/supplement

Post by heisan » Fri Aug 31, 2018 11:13 am

I am putting this library up for testing:

Install this in your libraries folder, then simply add:

Code: Select all

to your .ino file to replace some of the more memory intensive and/or useless libc symbols.

In projects which have static objects with desctructors, this will save around 5k of flash and 2k of RAM.

It also replaces malloc/free with more compact (if slower) versions, with more aggressive defragmentation for embedded devices.

Also provides an optional pre-allocated dynamic memory space for 'what you see is what you get' memory management (what the compiler tells you is your free ram, is indeed your free ram - dynamic allocation will not eat into this.

See for details.

Any feedback appreciated.

(NOTE: Still porting my printf libraries over - will add them when done.)

Posts: 111
Joined: Thu May 31, 2018 5:39 pm

Re: LibC - libc replacement/supplement

Post by heisan » Wed Sep 05, 2018 6:46 pm

Added printf support to the library:

Two simple APIs to use:

Code: Select all

pprintf(Serial, "Use Serial explicitly as a print device...\n");
where the first parameter is anything with a Print class.


Code: Select all

printf("Use Serial by setting it as the system print device...\n");
Any Print class can be set as the default output for the printf statement, which is great for portable code. Just one call and all your output goes to the correct device.

Total memory saving over libc implementation is 19kB of flash and 1kB of RAM. Floating point support adds 900 bytes, and can be compiled out, if required.

There are a few minor deviations from the C spec, but should not affect any common use cases. From the comments, these are the deviations:

1. Output failures are not tracked/counted. Return value is the number of characters
that _should have_ been printed.

2. Limited precision - 64 bit ints are truncated to 32, doubles to floats

3. Floats are cast back to int32 for rendering - if there are two many digits, zeros
are appended (and possibly decimals are reduced) - PRECISION IS LOST ON LARGE FLOATS!

4. targets 32 bit processors, so all modifiers which promote to int are ignored

5. 'a' format is not not complete - optionally compiled out to save space

6. All 'capital letter' formats are handled, even if not valid (translated to lower
case equivalent)

7. m$ width/precision are parsed, but treated as unset...

8. floating point precision is limited to 8 decimals

9. 'g' precision interpretation differs from gcc - can't understand gccs interpretation

10. width is limited to 255, precision to 254

I must admit, I am quite pleased with the float->int conversion - code footprint is tiny, with no libgcc linkages. Accurate to 7 significant digits.

User avatar
Posts: 835
Joined: Mon Apr 27, 2015 2:28 pm
Location: Mt.Etna, Italy

Re: LibC - libc replacement/supplement

Post by zoomx » Thu Sep 06, 2018 6:04 am

Starred and watched on github. Thanks for sharing!

Posts: 111
Joined: Thu May 31, 2018 5:39 pm

Re: LibC - libc replacement/supplement

Post by heisan » Thu Sep 06, 2018 6:54 am

Thanks - glad you find it useful.

Post Reply