reent_init.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <sys/reent.h>
  19. #include "esp_attr.h"
  20. /* This function is not part on newlib API, it is defined in libc/stdio/local.h
  21. * There is no nice way to get __cleanup member populated while avoiding __sinit,
  22. * so extern declaration is used here.
  23. */
  24. extern void _cleanup_r(struct _reent* r);
  25. /**
  26. * This is the replacement for newlib's _REENT_INIT_PTR and __sinit.
  27. * The problem with __sinit is that it allocates three FILE structures
  28. * (stdin, stdout, stderr). Having individual standard streams for each task
  29. * is a bit too much on a small embedded system. So we point streams
  30. * to the streams of the global struct _reent, which are initialized in
  31. * startup code.
  32. */
  33. void IRAM_ATTR esp_reent_init(struct _reent* r)
  34. {
  35. memset(r, 0, sizeof(*r));
  36. r->_stdout = _GLOBAL_REENT->_stdout;
  37. r->_stderr = _GLOBAL_REENT->_stderr;
  38. r->_stdin = _GLOBAL_REENT->_stdin;
  39. r->__cleanup = &_cleanup_r;
  40. r->__sdidinit = 1;
  41. r->__sglue._next = NULL;
  42. r->__sglue._niobs = 0;
  43. r->__sglue._iobs = NULL;
  44. }
  45. /* only declared in private stdio header file, local.h */
  46. extern void __sfp_lock_acquire(void);
  47. extern void __sfp_lock_release(void);
  48. void esp_reent_cleanup(void)
  49. {
  50. struct _reent* r = __getreent();
  51. /* Clean up storage used by mprec functions */
  52. if (r->_mp) {
  53. if (_REENT_MP_FREELIST(r)) {
  54. for (int i = 0; i < _Kmax; ++i) {
  55. struct _Bigint *cur, *next;
  56. next = _REENT_MP_FREELIST(r)[i];
  57. while (next) {
  58. cur = next;
  59. next = next->_next;
  60. free(cur);
  61. }
  62. }
  63. }
  64. free(_REENT_MP_FREELIST(r));
  65. free(_REENT_MP_RESULT(r));
  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. /* Clean up various other buffers */
  93. free(r->_mp);
  94. r->_mp = NULL;
  95. free(r->_r48);
  96. r->_r48 = NULL;
  97. free(r->_localtime_buf);
  98. r->_localtime_buf = NULL;
  99. free(r->_asctime_buf);
  100. r->_asctime_buf = NULL;
  101. }