public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* Implementing a heap watermark
@ 2020-11-08 17:14 Rob Meades
  2020-11-09 14:08 ` Federico Terraneo
  0 siblings, 1 reply; 2+ messages in thread
From: Rob Meades @ 2020-11-08 17:14 UTC (permalink / raw)
  To: newlib

I run/test my library code on various embedded processors (STM32F4, NRF52/53, ESP32), all of which use newlib.

I need to implement a "minimum heap ever available" watermark kind of function so that, after my tests are run, I can be sure that I have left sufficient room for user code to obtain heap memory.

Such a facility does not seem to be available in newlib out of the box: does anyone have any recommendations on how to go about implementing such a function?

For instance, is the newlib malloc()ater guaranteed to only call sbrk() when it really needs more memory, as opposed to when it might have free blocks available but it is "cheaper" to call sbrk() then to allocate from those blocks?  If the former then simply monitoring sbrk() would do the job for me.  If the latter then I probably have to intercept the malloc()/free() calls to track what's going on.

Rob


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Implementing a heap watermark
  2020-11-08 17:14 Implementing a heap watermark Rob Meades
@ 2020-11-09 14:08 ` Federico Terraneo
  0 siblings, 0 replies; 2+ messages in thread
From: Federico Terraneo @ 2020-11-09 14:08 UTC (permalink / raw)
  To: newlib

Dear Rob,
functionality is available in the Miosix OS, which uses newlib. You can
have a look at the code to see how to implement such a functionality.

The functionality you are interested in is the getAbsoluteFreeHeap()
member function of the MemoryProfiling class.

https://github.com/fedetft/miosix-kernel/blob/master/miosix/util/util.h#L88

It is implemented in terms of getMaxHeap() function that tracks heap
requests directly from _sbrk_r

https://github.com/fedetft/miosix-kernel/blob/master/miosix/stdlib_integration/libc_integration.cpp#L61

Hope this helps,
Federico


On 08/11/20 18:14, Rob Meades via Newlib wrote:
> I run/test my library code on various embedded processors (STM32F4, NRF52/53, ESP32), all of which use newlib.
> 
> I need to implement a "minimum heap ever available" watermark kind of function so that, after my tests are run, I can be sure that I have left sufficient room for user code to obtain heap memory.
> 
> Such a facility does not seem to be available in newlib out of the box: does anyone have any recommendations on how to go about implementing such a function?
> 
> For instance, is the newlib malloc()ater guaranteed to only call sbrk() when it really needs more memory, as opposed to when it might have free blocks available but it is "cheaper" to call sbrk() then to allocate from those blocks?  If the former then simply monitoring sbrk() would do the job for me.  If the latter then I probably have to intercept the malloc()/free() calls to track what's going on.
> 
> Rob
> 
> .
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-09 14:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-08 17:14 Implementing a heap watermark Rob Meades
2020-11-09 14:08 ` Federico Terraneo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).