When to enable configUSE_NEWLIB_REENTRANT macro?

Background I have a project that built with GCC and links against newlib-nano (nano.specs). Previously I was using standard library memory allocators(malloc(), etc.). I have since switched to using heap_5 as I have external memory which spand multiple memory boundaries. I have implemented link time wrapper to wrap all memory allocators calls and redirect them to the real memory allocator for heap5.c * –wrap,malloc,–wrap,mallocr,–wrap,realloc,–wrap,reallocr,–wrap,calloc,–wrap,callocr,–wrap,free,–wrap,free_r I also use some snprintf() calls and sscanf() as I have lightweight json decoding/encoding library. I have also rewrote my linker script to remove any standard symbol so linker error out if it needs them. Question * When should the configUSE_NEWLIB_REENTRANT be enabled given that all the memory allocators have been wrapped? I am still using snprintf() and sscanf() though.

When to enable configUSE_NEWLIB_REENTRANT macro?

See https://mcuoneclipse.com/2017/07/02/using-freertos-with-newlib-and-newlib-nano/ which goes into details on this.

When to enable configUSE_NEWLIB_REENTRANT macro?

I have read that post. The post basically says to enable configUSE_NEWLIB_REENTRANT if newlib-nano is used. But the post does not talk indept on how standard library uses struct _reent because if it’s not used by any of my function calls then I do not need it. If the struct _reent only has to do with memory allocation then I don’t need it because all standard library memory alloactors are wrapped (at the linker level) and point back to heap_5 implementation. Thus this leads me to believe I do not need to enable configUSE_NEWLIB_REENTRANT. I also know that sbrk is not being linked for 2 reason 1. The linker symbol it require doesn’t exist because I use custom linker script that I wrote 2. I can validate sbrk is remove based on the .map file generated by GCC Thus given this I should not be enabling configUSE_NEWLIB_REENTRANT but it seems that no one really know when struct _reent is really needed and that is what I want to know. Does anyone know is struct _reent has to do with more than just memory allocators or is that all it’s needed for?

When to enable configUSE_NEWLIB_REENTRANT macro?

I have read that post. The post basically says to enable configUSE_NEWLIB_REENTRANT if newlib-nano is used. But the post does not talk indept on how standard library uses struct _reent because if it’s not used by any of my function calls then I do not need it. If the struct _reent only has to do with memory allocation then I don’t need it because all standard library memory alloactors are wrapped (at the linker level) and point back to heap_5 implementation. Thus this leads me to believe I do not need to enable configUSE_NEWLIB_REENTRANT. I also know that sbrk is not being linked for 2 reason 1. The linker symbol it require doesn’t exist because I use custom linker script that I wrote 2. I can validate sbrk is remove based on the .map file generated by GCC Also all my startup code from the point the reset vector is called all the way to main() has been written by me and does not call any standard library calls. Which includes setup up data and bss and setting up MSP (main stack pointer). Thus given this I should not be enabling configUSE_NEWLIB_REENTRANT but it seems that no one really know when struct _reent is really needed and that is what I want to know. Does anyone know is struct _reent has to do with more than just memory allocators or is that all it’s needed for?

When to enable configUSE_NEWLIB_REENTRANT macro?

I would have to dig up all the details (a good read is as well http://www.nadler.com/embedded/newlibAndFreeRTOS.html). But basically if you are calling library functions, you need to make sure they are reentrant (from a task perspective). So yes, if using newlib functions and if you want to have it reentrant, then configUSENEWLIBREENTRANT should be set with all the needed hooks. This is especially true if using the newlib heap as the library might use malloc() and then this access to malloc needs to be reentrant. If you are not using newlib functionality (if linking against it) or only functions you know they are reentrant, than you don’t have to turn on configUSENEWLIBREENTRANT. There are some details discussed in https://www.embedded.com/design/prototyping-and-development/4024867/Embedding-with-GNU-Newlib about the hooks needed. There is as well a discussion on this topic here: https://www.freertos.org/FreeRTOSSupportForumArchive/November2009/freertosreentstructforfreertos_3447093.html I hope this helps.

When to enable configUSE_NEWLIB_REENTRANT macro?

My memory is that configUSENEWLIBREENTRANT doesn’t fix the issue to make malloc/free thread safe (for that you need to define functions __malloclock() and __mallocunlock(), but it provides thread local storage for a number of pieces of newlib that might need it. The biggest usage of which is for things like the file system (if you need it), and things like errno and the locale information. The structure is big, as it contains 3 FILE structures, so if you can be careful not to use the parts that need it, you can save significant memory. You can see what is in it if you find where the compiler stores the system headers, as the structure is defined in sys/reent.h

When to enable configUSE_NEWLIB_REENTRANT macro?

When I was using the standard library I had implemented malloc_lock() and malloc_unlock() no longer using standard library implementation. So I checked out the tool chain header. There alot stuff in there which relates to using floating point arithmetic such as mprec. I have a hard floating point unit but it seems that the mprec module is used when I dig through the standard library. I am now considering removing standard library all together and just adding my own implementations the down side is I do use some external stacks which rely on certains standard library implementation. I don’t do any FILE io operation in my code neither does any external library.