public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Rob Meades <Rob.Meades@u-blox.com>
To: "newlib@sourceware.org" <newlib@sourceware.org>
Cc: Andreas Anderberg <andreas.anderberg@u-blox.com>
Subject: Where stdin/stdout/stderr pointers are allocated
Date: Wed, 13 Oct 2021 07:36:26 +0000	[thread overview]
Message-ID: <sig.1920f0bc05.CWLP265MB4267B437CCA1E87031BC9B78BEB79@CWLP265MB4267.GBRP265.PROD.OUTLOOK.COM> (raw)

We run our own library of driver C code on several microcontroller platforms (e.g. STM32F4 and nRF5) where the SDK provided by the chip vendor uses newlib [pre-built by the vendor or by GCC] and FreeRTOS.  We use dynamically allocated tasks in the driver code and so, in our unit tests, we check for memory leaks to make sure we're shutting them down correctly.  FreeRTOS is sufficiently aware of newlib to call _reclaim_reent when a task exits.

The vendors do _not_ define _REENT_GLOBAL_STDIO_STREAMS and so each dynamic task ends up allocating file pointers for stdin/stdout/stderr.  As we understand it these are allocated in a _GLOBAL_REENT and not _REENT and, when a task exits, the file pointers are marked as free but not actually free'd so that they can be re-used later.

The problem then is that, since the file pointers are not being cleaned up when a dynamic task exits, our code "leaks" memory.  It is not _really_ a leak since the pointers are usable later by our driver code but it is taking memory from the customer's application that is not being returned.

QUESTION: is this how it is meant to work, stdin/stdout/stderr allocated in a global pool rather then in _REENT where FreeRTOS would free them on task exit?

If so our workaround, at least when unit testing, is to allocate a load of file pointers in a "preamble" test to take them out of our heap-checking equation.

Rob


                 reply	other threads:[~2021-10-13  7:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sig.1920f0bc05.CWLP265MB4267B437CCA1E87031BC9B78BEB79@CWLP265MB4267.GBRP265.PROD.OUTLOOK.COM \
    --to=rob.meades@u-blox.com \
    --cc=andreas.anderberg@u-blox.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).