portclib.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
  4. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * https://www.FreeRTOS.org
  26. * https://github.com/FreeRTOS
  27. *
  28. */
  29. #include "FreeRTOS.h"
  30. #if XT_USE_THREAD_SAFE_CLIB
  31. #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
  32. #include <errno.h>
  33. #include <sys/reent.h>
  34. #include "semphr.h"
  35. typedef SemaphoreHandle_t _Rmtx;
  36. //-----------------------------------------------------------------------------
  37. // Override this and set to nonzero to enable locking.
  38. //-----------------------------------------------------------------------------
  39. int32_t _xclib_use_mt = 1;
  40. //-----------------------------------------------------------------------------
  41. // Init lock.
  42. //-----------------------------------------------------------------------------
  43. void
  44. _Mtxinit(_Rmtx * mtx)
  45. {
  46. *mtx = xSemaphoreCreateRecursiveMutex();
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Destroy lock.
  50. //-----------------------------------------------------------------------------
  51. void
  52. _Mtxdst(_Rmtx * mtx)
  53. {
  54. if ((mtx != NULL) && (*mtx != NULL)) {
  55. vSemaphoreDelete(*mtx);
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Lock.
  60. //-----------------------------------------------------------------------------
  61. void
  62. _Mtxlock(_Rmtx * mtx)
  63. {
  64. if ((mtx != NULL) && (*mtx != NULL)) {
  65. xSemaphoreTakeRecursive(*mtx, portMAX_DELAY);
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Unlock.
  70. //-----------------------------------------------------------------------------
  71. void
  72. _Mtxunlock(_Rmtx * mtx)
  73. {
  74. if ((mtx != NULL) && (*mtx != NULL)) {
  75. xSemaphoreGiveRecursive(*mtx);
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Called by malloc() to allocate blocks of memory from the heap.
  80. //-----------------------------------------------------------------------------
  81. void *
  82. _sbrk_r (struct _reent * reent, int32_t incr)
  83. {
  84. extern char _end;
  85. extern char _heap_sentry;
  86. static char * _heap_sentry_ptr = &_heap_sentry;
  87. static char * heap_ptr;
  88. char * base;
  89. if (!heap_ptr)
  90. heap_ptr = (char *) &_end;
  91. base = heap_ptr;
  92. if (heap_ptr + incr >= _heap_sentry_ptr) {
  93. reent->_errno = ENOMEM;
  94. return (char *) -1;
  95. }
  96. heap_ptr += incr;
  97. return base;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // Global initialization for C library.
  101. //-----------------------------------------------------------------------------
  102. void
  103. vPortClibInit(void)
  104. {
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Per-thread cleanup stub provided for linking, does nothing.
  108. //-----------------------------------------------------------------------------
  109. void
  110. _reclaim_reent(void * ptr)
  111. {
  112. }
  113. #endif /* XSHAL_CLIB == XTHAL_CLIB_XCLIB */
  114. #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
  115. #include <errno.h>
  116. #include <malloc.h>
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include "semphr.h"
  121. static SemaphoreHandle_t xClibMutex;
  122. static uint32_t ulClibInitDone = 0;
  123. //-----------------------------------------------------------------------------
  124. // Get C library lock.
  125. //-----------------------------------------------------------------------------
  126. void
  127. __malloc_lock(struct _reent * ptr)
  128. {
  129. if (!ulClibInitDone)
  130. return;
  131. xSemaphoreTakeRecursive(xClibMutex, portMAX_DELAY);
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Release C library lock.
  135. //-----------------------------------------------------------------------------
  136. void
  137. __malloc_unlock(struct _reent * ptr)
  138. {
  139. if (!ulClibInitDone)
  140. return;
  141. xSemaphoreGiveRecursive(xClibMutex);
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Lock for environment. Since we have only one global lock we can just call
  145. // the malloc() lock function.
  146. //-----------------------------------------------------------------------------
  147. void
  148. __env_lock(struct _reent * ptr)
  149. {
  150. __malloc_lock(ptr);
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Unlock environment.
  154. //-----------------------------------------------------------------------------
  155. void
  156. __env_unlock(struct _reent * ptr)
  157. {
  158. __malloc_unlock(ptr);
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Called by malloc() to allocate blocks of memory from the heap.
  162. //-----------------------------------------------------------------------------
  163. void *
  164. _sbrk_r (struct _reent * reent, int32_t incr)
  165. {
  166. extern char _end;
  167. extern char _heap_sentry;
  168. static char * _heap_sentry_ptr = &_heap_sentry;
  169. static char * heap_ptr;
  170. char * base;
  171. if (!heap_ptr)
  172. heap_ptr = (char *) &_end;
  173. base = heap_ptr;
  174. if (heap_ptr + incr >= _heap_sentry_ptr) {
  175. reent->_errno = ENOMEM;
  176. return (char *) -1;
  177. }
  178. heap_ptr += incr;
  179. return base;
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Global initialization for C library.
  183. //-----------------------------------------------------------------------------
  184. void
  185. vPortClibInit(void)
  186. {
  187. configASSERT(!ulClibInitDone);
  188. xClibMutex = xSemaphoreCreateRecursiveMutex();
  189. ulClibInitDone = 1;
  190. }
  191. #endif /* XSHAL_CLIB == XTHAL_CLIB_NEWLIB */
  192. #endif /* XT_USE_THREAD_SAFE_CLIB */