esp-idf/components/newlib/reent_init.c
2022-06-17 16:59:56 +02:00

95 lines
2.6 KiB
C

/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/reent.h>
#include "esp_attr.h"
/**
* This is the replacement for newlib's _REENT_INIT_PTR and __sinit.
* The problem with __sinit is that it allocates three FILE structures
* (stdin, stdout, stderr). Having individual standard streams for each task
* is a bit too much on a small embedded system. So we point streams
* to the streams of the global struct _reent, which are initialized in
* startup code.
*/
void IRAM_ATTR esp_reent_init(struct _reent* r)
{
memset(r, 0, sizeof(*r));
r->_stdout = _GLOBAL_REENT->_stdout;
r->_stderr = _GLOBAL_REENT->_stderr;
r->_stdin = _GLOBAL_REENT->_stdin;
r->__cleanup = &_cleanup_r;
r->__sdidinit = 1;
r->__sglue._next = NULL;
r->__sglue._niobs = 0;
r->__sglue._iobs = NULL;
}
/* only declared in private stdio header file, local.h */
extern void __sfp_lock_acquire(void);
extern void __sfp_lock_release(void);
void esp_reent_cleanup(void)
{
struct _reent* r = __getreent();
/* Clean up storage used by mprec functions */
if (r->_mp) {
if (_REENT_MP_FREELIST(r)) {
for (unsigned int i = 0; i < _Kmax; ++i) {
struct _Bigint *cur, *next;
next = _REENT_MP_FREELIST(r)[i];
while (next) {
cur = next;
next = next->_next;
free(cur);
}
}
}
free(_REENT_MP_FREELIST(r));
free(_REENT_MP_RESULT(r));
}
/* Clean up "glue" (lazily-allocated FILE objects) */
struct _glue* prev = &_GLOBAL_REENT->__sglue;
for (struct _glue* cur = _GLOBAL_REENT->__sglue._next; cur != NULL;) {
if (cur->_niobs == 0) {
cur = cur->_next;
continue;
}
bool has_open_files = false;
for (int i = 0; i < cur->_niobs; ++i) {
FILE* fp = &cur->_iobs[i];
if (fp->_flags != 0) {
has_open_files = true;
break;
}
}
if (has_open_files) {
prev = cur;
cur = cur->_next;
continue;
}
struct _glue* next = cur->_next;
prev->_next = next;
free(cur);
cur = next;
}
/* Clean up various other buffers */
free(r->_mp);
r->_mp = NULL;
free(r->_r48);
r->_r48 = NULL;
free(r->_localtime_buf);
r->_localtime_buf = NULL;
free(r->_asctime_buf);
r->_asctime_buf = NULL;
}