1 /*******************************************************************************
\r
2 // Copyright (c) 2003-2015 Cadence Design Systems, Inc.
\r
4 // Permission is hereby granted, free of charge, to any person obtaining
\r
5 // a copy of this software and associated documentation files (the
\r
6 // "Software"), to deal in the Software without restriction, including
\r
7 // without limitation the rights to use, copy, modify, merge, publish,
\r
8 // distribute, sublicense, and/or sell copies of the Software, and to
\r
9 // permit persons to whom the Software is furnished to do so, subject to
\r
10 // the following conditions:
\r
12 // The above copyright notice and this permission notice shall be included
\r
13 // in all copies or substantial portions of the Software.
\r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
\r
18 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
\r
19 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
\r
20 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
\r
21 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 --------------------------------------------------------------------------------
\r
25 #include "FreeRTOS.h"
\r
27 #if XT_USE_THREAD_SAFE_CLIB
\r
29 #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
\r
32 #include <sys/reent.h>
\r
36 typedef SemaphoreHandle_t _Rmtx;
\r
38 //-----------------------------------------------------------------------------
\r
39 // Override this and set to nonzero to enable locking.
\r
40 //-----------------------------------------------------------------------------
\r
41 int32_t _xclib_use_mt = 1;
\r
44 //-----------------------------------------------------------------------------
\r
46 //-----------------------------------------------------------------------------
\r
48 _Mtxinit(_Rmtx * mtx)
\r
50 *mtx = xSemaphoreCreateRecursiveMutex();
\r
53 //-----------------------------------------------------------------------------
\r
55 //-----------------------------------------------------------------------------
\r
57 _Mtxdst(_Rmtx * mtx)
\r
59 if ((mtx != NULL) && (*mtx != NULL)) {
\r
60 vSemaphoreDelete(*mtx);
\r
64 //-----------------------------------------------------------------------------
\r
66 //-----------------------------------------------------------------------------
\r
68 _Mtxlock(_Rmtx * mtx)
\r
70 if ((mtx != NULL) && (*mtx != NULL)) {
\r
71 xSemaphoreTakeRecursive(*mtx, portMAX_DELAY);
\r
75 //-----------------------------------------------------------------------------
\r
77 //-----------------------------------------------------------------------------
\r
79 _Mtxunlock(_Rmtx * mtx)
\r
81 if ((mtx != NULL) && (*mtx != NULL)) {
\r
82 xSemaphoreGiveRecursive(*mtx);
\r
86 //-----------------------------------------------------------------------------
\r
87 // Called by malloc() to allocate blocks of memory from the heap.
\r
88 //-----------------------------------------------------------------------------
\r
90 _sbrk_r (struct _reent * reent, int32_t incr)
\r
93 extern char _heap_sentry;
\r
94 static char * _heap_sentry_ptr = &_heap_sentry;
\r
95 static char * heap_ptr;
\r
99 heap_ptr = (char *) &_end;
\r
102 if (heap_ptr + incr >= _heap_sentry_ptr) {
\r
103 reent->_errno = ENOMEM;
\r
104 return (char *) -1;
\r
111 //-----------------------------------------------------------------------------
\r
112 // Global initialization for C library.
\r
113 //-----------------------------------------------------------------------------
\r
115 vPortClibInit(void)
\r
119 //-----------------------------------------------------------------------------
\r
120 // Per-thread cleanup stub provided for linking, does nothing.
\r
121 //-----------------------------------------------------------------------------
\r
123 _reclaim_reent(void * ptr)
\r
127 #endif /* XSHAL_CLIB == XTHAL_CLIB_XCLIB */
\r
129 #if XSHAL_CLIB == XTHAL_CLIB_NEWLIB
\r
132 #include <malloc.h>
\r
134 #include <stdlib.h>
\r
135 #include <string.h>
\r
137 #include "semphr.h"
\r
139 static SemaphoreHandle_t xClibMutex;
\r
140 static uint32_t ulClibInitDone = 0;
\r
142 //-----------------------------------------------------------------------------
\r
143 // Get C library lock.
\r
144 //-----------------------------------------------------------------------------
\r
146 __malloc_lock(struct _reent * ptr)
\r
148 if (!ulClibInitDone)
\r
151 xSemaphoreTakeRecursive(xClibMutex, portMAX_DELAY);
\r
154 //-----------------------------------------------------------------------------
\r
155 // Release C library lock.
\r
156 //-----------------------------------------------------------------------------
\r
158 __malloc_unlock(struct _reent * ptr)
\r
160 if (!ulClibInitDone)
\r
163 xSemaphoreGiveRecursive(xClibMutex);
\r
166 //-----------------------------------------------------------------------------
\r
167 // Lock for environment. Since we have only one global lock we can just call
\r
168 // the malloc() lock function.
\r
169 //-----------------------------------------------------------------------------
\r
171 __env_lock(struct _reent * ptr)
\r
173 __malloc_lock(ptr);
\r
177 //-----------------------------------------------------------------------------
\r
178 // Unlock environment.
\r
179 //-----------------------------------------------------------------------------
\r
181 __env_unlock(struct _reent * ptr)
\r
183 __malloc_unlock(ptr);
\r
186 //-----------------------------------------------------------------------------
\r
187 // Called by malloc() to allocate blocks of memory from the heap.
\r
188 //-----------------------------------------------------------------------------
\r
190 _sbrk_r (struct _reent * reent, int32_t incr)
\r
193 extern char _heap_sentry;
\r
194 static char * _heap_sentry_ptr = &_heap_sentry;
\r
195 static char * heap_ptr;
\r
199 heap_ptr = (char *) &_end;
\r
202 if (heap_ptr + incr >= _heap_sentry_ptr) {
\r
203 reent->_errno = ENOMEM;
\r
204 return (char *) -1;
\r
211 //-----------------------------------------------------------------------------
\r
212 // Global initialization for C library.
\r
213 //-----------------------------------------------------------------------------
\r
215 vPortClibInit(void)
\r
217 configASSERT(!ulClibInitDone);
\r
219 xClibMutex = xSemaphoreCreateRecursiveMutex();
\r
220 ulClibInitDone = 1;
\r
223 #endif /* XSHAL_CLIB == XTHAL_CLIB_NEWLIB */
\r
225 #endif /* XT_USE_THREAD_SAFE_CLIB */
\r