Reduce stack usage of _vfiprintf_r()
Freddie Chopin
freddie_chopin@op.pl
Fri Oct 12 21:22:00 GMT 2012
W dniu 2012-10-11 16:28, Claudio Lanconelli pisze:
> Hi,
> I use newlib for ARM Cortex-M3.
> I agree with Joel, a malloc()/free() call for each printf() is not
> acceptable for me, this would raise the risk of memory fragmentation.
>
> Just my two cents.
Please - first read, then post!
Currently EACH entry to _vf[i]printf_r() costs you 1.3kB of stack. When
you use it with an unbuffered stream it will use twice that (2.6kB).
With the change I suggested EACH entry to _vf[i]printf_r() costs you
300B of stack. ALWAYS! There is NO additional memory allocation unless
you use an unbuffered stream. Malloc() is called ONLY if the stream is
unbuffered! There risk of memory fragmentation is very low, because the
buffer is freed before the function returns - fragmentation would only
happen in multithreaded application when another task interrupts the
stream processing and allocates something more. No fragmentation is
possible for a single threaded application. Moreover memory
fragmentation usually happens if you allocate SMALL pieces of memory and
free them in random order, not a 1kB buffer which is freed right away...
Just my three cents. It's probably the third or fourth time I'm
explaining... Nobody is suggesting to replace unconditional allocation
on stack with an uncoditional allocation on heap! Not that it would be a
wrong idea in some places...
BTW - printf() style functions use malloc all the time, especially for
floating point, so your disagreement is a bit late...
4\/3!!
More information about the Newlib
mailing list