reent_init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  21. * This is the replacement for newlib's _REENT_INIT_PTR and __sinit.
  22. * The problem with __sinit is that it allocates three FILE structures
  23. * (stdin, stdout, stderr). Having individual standard streams for each task
  24. * is a bit too much on a small embedded system. So we point streams
  25. * to the streams of the global struct _reent, which are initialized in
  26. * startup code.
  27. */
  28. void IRAM_ATTR esp_reent_init(struct _reent* r)
  29. {
  30. memset(r, 0, sizeof(*r));
  31. r->_stdout = _GLOBAL_REENT->_stdout;
  32. r->_stderr = _GLOBAL_REENT->_stderr;
  33. r->_stdin = _GLOBAL_REENT->_stdin;
  34. r->__cleanup = &_cleanup_r;
  35. r->__sdidinit = 1;
  36. r->__sglue._next = NULL;
  37. r->__sglue._niobs = 0;
  38. r->__sglue._iobs = NULL;
  39. }
  40. /* only declared in private stdio header file, local.h */
  41. extern void __sfp_lock_acquire(void);
  42. extern void __sfp_lock_release(void);
  43. void esp_reent_cleanup(void)
  44. {
  45. struct _reent* r = __getreent();
  46. /* Clean up storage used by mprec functions */
  47. if (r->_mp) {
  48. if (_REENT_MP_FREELIST(r)) {
  49. for (unsigned int i = 0; i < _Kmax; ++i) {
  50. struct _Bigint *cur, *next;
  51. next = _REENT_MP_FREELIST(r)[i];
  52. while (next) {
  53. cur = next;
  54. next = next->_next;
  55. free(cur);
  56. }
  57. }
  58. }
  59. free(_REENT_MP_FREELIST(r));
  60. free(_REENT_MP_RESULT(r));
  61. }
  62. /* Clean up "glue" (lazily-allocated FILE objects) */
  63. struct _glue* prev = &_GLOBAL_REENT->__sglue;
  64. for (struct _glue* cur = _GLOBAL_REENT->__sglue._next; cur != NULL;) {
  65. if (cur->_niobs == 0) {
  66. cur = cur->_next;
  67. continue;
  68. }
  69. bool has_open_files = false;
  70. for (int i = 0; i < cur->_niobs; ++i) {
  71. FILE* fp = &cur->_iobs[i];
  72. if (fp->_flags != 0) {
  73. has_open_files = true;
  74. break;
  75. }
  76. }
  77. if (has_open_files) {
  78. prev = cur;
  79. cur = cur->_next;
  80. continue;
  81. }
  82. struct _glue* next = cur->_next;
  83. prev->_next = next;
  84. free(cur);
  85. cur = next;
  86. }
  87. /* Clean up various other buffers */
  88. free(r->_mp);
  89. r->_mp = NULL;
  90. free(r->_r48);
  91. r->_r48 = NULL;
  92. free(r->_localtime_buf);
  93. r->_localtime_buf = NULL;
  94. free(r->_asctime_buf);
  95. r->_asctime_buf = NULL;
  96. }