2022-06-17 14:59:56 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-10-25 14:16:08 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2019-04-15 13:07:38 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-10-25 14:16:08 +00:00
|
|
|
#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;
|
|
|
|
}
|
2019-04-15 13:07:38 +00:00
|
|
|
|
|
|
|
/* only declared in private stdio header file, local.h */
|
2019-07-16 09:33:30 +00:00
|
|
|
extern void __sfp_lock_acquire(void);
|
|
|
|
extern void __sfp_lock_release(void);
|
2019-04-15 13:07:38 +00:00
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void esp_reent_cleanup(void)
|
2019-04-15 13:07:38 +00:00
|
|
|
{
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
/* Clean up storage used by mprec functions */
|
|
|
|
if (r->_mp) {
|
|
|
|
if (_REENT_MP_FREELIST(r)) {
|
2020-11-17 04:48:35 +00:00
|
|
|
for (unsigned int i = 0; i < _Kmax; ++i) {
|
2019-04-15 13:07:38 +00:00
|
|
|
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;
|
|
|
|
}
|