• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string.h>
16 #include <stdbool.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <sys/reent.h>
20 #include "esp_attr.h"
21 
22 /**
23  * This is the replacement for newlib's _REENT_INIT_PTR and __sinit.
24  * The problem with __sinit is that it allocates three FILE structures
25  * (stdin, stdout, stderr). Having individual standard streams for each task
26  * is a bit too much on a small embedded system. So we point streams
27  * to the streams of the global struct _reent, which are initialized in
28  * startup code.
29  */
esp_reent_init(struct _reent * r)30 void IRAM_ATTR esp_reent_init(struct _reent* r)
31 {
32     memset(r, 0, sizeof(*r));
33     r->_stdout = _GLOBAL_REENT->_stdout;
34     r->_stderr = _GLOBAL_REENT->_stderr;
35     r->_stdin  = _GLOBAL_REENT->_stdin;
36     r->__cleanup = &_cleanup_r;
37     r->__sdidinit = 1;
38     r->__sglue._next = NULL;
39     r->__sglue._niobs = 0;
40     r->__sglue._iobs = NULL;
41 }
42 
43 /* only declared in private stdio header file, local.h */
44 extern void __sfp_lock_acquire(void);
45 extern void __sfp_lock_release(void);
46 
esp_reent_cleanup(void)47 void esp_reent_cleanup(void)
48 {
49     struct _reent* r = __getreent();
50     /* Clean up storage used by mprec functions */
51     if (r->_mp) {
52         if (_REENT_MP_FREELIST(r)) {
53             for (unsigned int i = 0; i < _Kmax; ++i) {
54                 struct _Bigint *cur, *next;
55                 next = _REENT_MP_FREELIST(r)[i];
56                 while (next) {
57                     cur = next;
58                     next = next->_next;
59                     free(cur);
60                 }
61             }
62         }
63         free(_REENT_MP_FREELIST(r));
64         free(_REENT_MP_RESULT(r));
65     }
66 
67     /* Clean up "glue" (lazily-allocated FILE objects) */
68     struct _glue* prev = &_GLOBAL_REENT->__sglue;
69     for (struct _glue* cur = _GLOBAL_REENT->__sglue._next; cur != NULL;) {
70         if (cur->_niobs == 0) {
71             cur = cur->_next;
72             continue;
73         }
74         bool has_open_files = false;
75         for (int i = 0; i < cur->_niobs; ++i) {
76             FILE* fp = &cur->_iobs[i];
77             if (fp->_flags != 0) {
78                 has_open_files = true;
79                 break;
80             }
81         }
82         if (has_open_files) {
83             prev = cur;
84             cur = cur->_next;
85             continue;
86         }
87         struct _glue* next = cur->_next;
88         prev->_next = next;
89         free(cur);
90         cur = next;
91     }
92 
93     /* Clean up various other buffers */
94     free(r->_mp);
95     r->_mp = NULL;
96     free(r->_r48);
97     r->_r48 = NULL;
98     free(r->_localtime_buf);
99     r->_localtime_buf = NULL;
100     free(r->_asctime_buf);
101     r->_asctime_buf = NULL;
102 }
103