Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #28786

Re: No dynamic memory at all: newlib and sprintf

From pozz <pozzugno@gmail.com>
Newsgroups comp.arch.embedded
Subject Re: No dynamic memory at all: newlib and sprintf
Date 2019-08-30 14:21 +0200
Organization A noiseless patient Spider
Message-ID <qkb4bc$fea$1@dont-email.me> (permalink)
References <qkaj7s$gki$1@dont-email.me> <qkakta$qbg$1@dont-email.me> <qkamdp$2ka$1@dont-email.me> <qkantj$ao8$1@dont-email.me>

Show all headers | View raw


Il 30/08/2019 10:48, David Brown ha scritto:
> On 30/08/2019 10:23, pozz wrote:
>> Il 30/08/2019 09:57, David Brown ha scritto:
>>> On 30/08/2019 09:29, pozz wrote:
>>>> I'd like to avoid using dynamic memory (heap) at all. I'm using gcc for
>>>> ARM and newlib (really I'm using NXP MCUXpresso IDE, but I don't think
>>>> this is important).
>>>>
>>>> When the code is minimal, I noticed heap related functions (malloc, free
>>>> and so on) aren't included in the output binary file.
>>>> However, as soon as I use sprintf-like functions, heap is used.
>>>>
>>>> I looked at the newlib source code and indeed I noticed __ssputs_r(), a
>>>> common shared function between sprintf-like functions, that uses malloc.
>>>> I think it uses malloc for asprintf-like functions that I'm not using.
>>>>
>>>> How to remove completely the heap with sprintf functions? Maybe by
>>>> implementing a custom minimal sprintf function?
>>>
>>> Check how __ssputs_r is using the heap.  Maybe you can get away with
>>> just adding these to your code somewhere:
>>>
>>> static char heap[100];
>>> void * malloc(size_t size) {
>>>      (void) size;
>>>      return heap;
>>> }
>>>
>>> void free(void *ptr) {
>>>      (void) ptr;
>>> }
>>>
>>> If the __ssputs_r function only ever calls malloc once while it works,
>>> and you don't use it from different threads or contexts (the "_r" in
>>> newlib is for reentrant code), that might do the job, while saving you
>>> from the effort of making your own printf stuff.
>>>
>>
>> IMHO, I will have an error from the linker, because of multiple
>> definition of malloc and free.
> 
> No, you won't.
> 
> The linker first loads the object files you have specified for linking.
>   It resolves all the symbols there when it can, and collects the
> unresolved ones.  Then it pulls in the libraries, and uses them to
> resolved those symbols.  That can lead to new unresolved symbols, which
> are again pulled from the libraries (and so on).
> 
> Symbols defined in your object code - or in a library that is loaded
> before newlib - will get priority.
> 
> It is not uncommon to have your own stub definitions of library
> functions like "exit" or "atexit" in order to avoid extra code being
> pulled in from libraries when you know your code will never call them.

I didn't know this, yes it works.

However I tried to replace sprintf() with a custom sprintf(), but in 
this case the linker complains with:

   multiple definition of `sprintf'

It sees two definitions in my printf.c and in libc_nano.a. Could you 
argue why this happens with sprintf() and not with malloc()?

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 09:29 +0200
  Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-08-30 09:57 +0200
    Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 10:23 +0200
      Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-08-30 10:48 +0200
        Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 14:21 +0200
          Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 15:39 +0200
            Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-09-01 22:47 +0200
              Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-09-01 23:53 +0200
                Re: No dynamic memory at all: newlib and sprintf David Brown <david.brown@hesbynett.no> - 2019-09-02 13:09 +0200
    Re: No dynamic memory at all: newlib and sprintf pozz <pozzugno@gmail.com> - 2019-08-30 10:35 +0200
  Re: No dynamic memory at all: newlib and sprintf Paul Rubin <no.email@nospam.invalid> - 2019-08-30 01:46 -0700
  Re: No dynamic memory at all: newlib and sprintf Dave Nadler <drn@nadler.com> - 2019-08-30 06:17 -0700

csiph-web