Skip to main content
Visitor II
October 20, 2022
Solved

New Lib reentrant using C++ and RTOS

  • October 20, 2022
  • 1 reply
  • 3467 views

I am confused on the topic of new lib reentrant and RTOS.

I use C++ and do some std::make_unique calls so malloc it's probably getting called under the hood.

I have enabled the setting configUSE_NEWLIB_REENTRANT for RTOS and I am using heap_4 memory scheme.

I am using a single core microcontroller from the family STM32L4.

Is this enough to avoid problems ?

I got confused by the posts on this topic by "nadler" and "mcuoneclipse"

    This topic has been closed for replies.
    Best answer by Pavel A.

    > I got confused by the posts on this topic by "nadler" and "mcuoneclipse"

    Welcome to the club :)

    >  I am using heap_4 memory scheme.

    All heap_N except of heap_3 are not aware of the C library heap (malloc/free/new...).

    Either make all allocations from the C heap before starting the RTOS scheduler (and avoid calling malloc &free from tasks), or read on thread-safe implementation of malloc/free for newlib.

    1 reply

    Pavel A.Answer
    Super User
    October 20, 2022

    > I got confused by the posts on this topic by "nadler" and "mcuoneclipse"

    Welcome to the club :)

    >  I am using heap_4 memory scheme.

    All heap_N except of heap_3 are not aware of the C library heap (malloc/free/new...).

    Either make all allocations from the C heap before starting the RTOS scheduler (and avoid calling malloc &free from tasks), or read on thread-safe implementation of malloc/free for newlib.

    MC.5Author
    Visitor II
    October 24, 2022

    Hi Pavel,

    thank you a lot for the reply.

    The codebase that I am working on makes all the objects allocations before starting freeRTOS. So I think that I am safe.

    Do you think that I can avoid using the setting configUSE_NEWLIB_REENTRANT if I don't do dynamic allocation and I don't use the regular snprintf ?

    Thank you again.

    Kind regards,

    Mattia

    Super User
    October 24, 2022

    Hi Mattia

    Newlib has some other functions that use the per-thread area (I don't remember which exactly). Likely yes, if the program does not use any of these in several threads, it can get away without configUSE_NEWLIB_REENTRANT.