-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[freertos] Make malloc/free thread-safe
- Loading branch information
1 parent
642461d
commit cb82eec
Showing
4 changed files
with
86 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,66 @@ | ||
|
||
/* | ||
* Copyright (c) 2019-2020 Niklas Hauser | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#include <freertos/FreeRTOS.h> | ||
#include <freertos/task.h> | ||
#include <modm/architecture/interface/assert.hpp> | ||
#include <cstdlib> | ||
|
||
extern "C" void vApplicationStackOverflowHook(TaskHandle_t, char *); | ||
|
||
void vApplicationStackOverflowHook(TaskHandle_t /*pxTask*/, char *pcTaskName) | ||
{ | ||
modm_assert(false, "freertos.stack", "FreeRTOS detected a stack overflow!", pcTaskName); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Make the Newlib heap thread-safe with FreeRTOS | ||
|
||
extern "C" void __malloc_lock(struct _reent *); | ||
void __malloc_lock(struct _reent *) | ||
{ | ||
vTaskSuspendAll(); | ||
} | ||
|
||
extern "C" void __malloc_unlock(struct _reent *); | ||
void __malloc_unlock(struct _reent *) | ||
{ | ||
xTaskResumeAll(); | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Make the FreeRTOS heap use Newlib heap | ||
|
||
extern "C" void *pvPortMalloc(size_t xWantedSize); | ||
void *pvPortMalloc(size_t xWantedSize) | ||
{ | ||
void *pvReturn = malloc(xWantedSize); | ||
traceMALLOC(pvReturn, xWantedSize); | ||
|
||
#if configUSE_MALLOC_FAILED_HOOK == 1 | ||
if(pvReturn == nullptr) | ||
{ | ||
extern "C" void vApplicationMallocFailedHook(void); | ||
vApplicationMallocFailedHook(); | ||
} | ||
#endif | ||
|
||
return pvReturn; | ||
} | ||
|
||
extern "C" void vPortFree(void *pv); | ||
void vPortFree(void *pv) | ||
{ | ||
if(pv) | ||
{ | ||
free(pv); | ||
traceFREE(pv, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters