From 58a5dc925c0fdd777713b024abf7339028b57bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Mon, 27 Sep 2021 22:16:22 +0100 Subject: [PATCH] Improvements in malloc/free (#2076) --- .../corlib_native_System_BitConverter.cpp | 8 ++-- src/HAL/Include/nanoHAL_v2.h | 14 +++++++ targets/ChibiOS/_common/platform_heap.c | 28 ++++++++++--- ..._ser_native_System_IO_Ports_SerialPort.cpp | 10 ++--- ...vices_SerialCommunication_SerialDevice.cpp | 10 ++--- ...e_native_Windows_Storage_StorageFolder.cpp | 40 +++++++++---------- .../ESP32_WROOM_32/common/platform_heap.c | 31 ++++++++++++-- targets/TI_SimpleLink/_common/platform_heap.c | 30 ++++++++++++-- .../targetSimpleLinkCC32xx_LinkLocalTask.c | 20 +++++----- targets/win32/nanoCLR/platform_heap.cpp | 25 +++++++++++- 10 files changed, 160 insertions(+), 56 deletions(-) diff --git a/src/CLR/CorLib/corlib_native_System_BitConverter.cpp b/src/CLR/CorLib/corlib_native_System_BitConverter.cpp index 9f7836dea0..0dc00004e6 100644 --- a/src/CLR/CorLib/corlib_native_System_BitConverter.cpp +++ b/src/CLR/CorLib/corlib_native_System_BitConverter.cpp @@ -12,7 +12,7 @@ char DigitalToHex(unsigned char x) char *ByteArrayToHex(unsigned char *pInput, int index, int length) { - char *pOutput = (char *)malloc(length * 3); + char *pOutput = (char *)platform_malloc(length * 3); char *p = pOutput; pInput += index; @@ -560,7 +560,7 @@ HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZ unsigned char *p = pArray->GetFirstElement(); char *pOutput = ByteArrayToHex(p, 0, pArray->m_numOfElements); NANOCLR_CHECK_HRESULT(stack.SetResult_String(pOutput)); - free(pOutput); + platform_free(pOutput); } NANOCLR_NOCLEANUP(); @@ -589,7 +589,7 @@ HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZ unsigned char *p = pArray->GetFirstElement(); char *pOutput = ByteArrayToHex(p, index, pArray->m_numOfElements - index); NANOCLR_CHECK_HRESULT(stack.SetResult_String(pOutput)); - free(pOutput); + platform_free(pOutput); } NANOCLR_NOCLEANUP(); @@ -623,7 +623,7 @@ HRESULT Library_corlib_native_System_BitConverter::ToString___STATIC__STRING__SZ unsigned char *p = pArray->GetFirstElement(); char *pOutput = ByteArrayToHex(p, index, length); NANOCLR_CHECK_HRESULT(stack.SetResult_String(pOutput)); - free(pOutput); + platform_free(pOutput); } NANOCLR_NOCLEANUP(); diff --git a/src/HAL/Include/nanoHAL_v2.h b/src/HAL/Include/nanoHAL_v2.h index 4725b630e4..5e78553560 100644 --- a/src/HAL/Include/nanoHAL_v2.h +++ b/src/HAL/Include/nanoHAL_v2.h @@ -7,6 +7,20 @@ #ifndef NANOHAL_V2_H #define NANOHAL_V2_H +#if !defined(_WIN32) + +// need to include stdlib.h **BEFORE** redefining malloc/free/realloc otherwise bad things happen +#include + +// defines to prevent use of malloc, free and realloc +// the platform implementations: platform_malloc(), platform_free and platform_realloc +// are the preferred calls to use as they ensure thread safety and RTOS integration +#define malloc YOU_SHALL_NOT_USE_malloc +#define free YOU_SHALL_NOT_USE_free +#define realloc YOU_SHALL_NOT_USE_realloc + +#endif + #include #include diff --git a/targets/ChibiOS/_common/platform_heap.c b/targets/ChibiOS/_common/platform_heap.c index 37e559f312..0f273189f3 100644 --- a/targets/ChibiOS/_common/platform_heap.c +++ b/targets/ChibiOS/_common/platform_heap.c @@ -4,16 +4,34 @@ // #include +#include -void* platform_malloc(size_t size) { - return chHeapAlloc(NULL, size); +void *platform_malloc(size_t size) +{ + +// need to undef in order to call the real function +#undef malloc + + return malloc(size); + +// define back +#define malloc YOU_SHALL_NOT_USE_malloc } -void platform_free(void* ptr) { - chHeapFree(ptr); +void platform_free(void *ptr) +{ + +// need to undef in order to call the real function +#undef free + + free(ptr); + +// define back +#define free YOU_SHALL_NOT_USE_free } -void* platform_realloc(void* ptr, size_t size) { +void *platform_realloc(void *ptr, size_t size) +{ (void)size; return ptr; diff --git a/targets/FreeRTOS/NXP/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp b/targets/FreeRTOS/NXP/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp index e55a6fc9bb..d83d2b3de3 100644 --- a/targets/FreeRTOS/NXP/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp +++ b/targets/FreeRTOS/NXP/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp @@ -201,8 +201,8 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeDispose___VO palUart = Uart_PAL[uartNum]; // Free ring buffers memory - free(palUart->TxBuffer); - free(palUart->RxBuffer); + platform_free(palUart->TxBuffer); + platform_free(palUart->RxBuffer); // Deinitialize device and delete FreeRTOS idle tasks LPUART_Deinit(base); @@ -237,16 +237,16 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeInit___VOID( NF_PAL_UART *palUart = Uart_PAL[uartNum]; // Allocate memory for TX and RX circular buffer - palUart->TxBuffer = (uint8_t *)malloc(UART_TX_BUFER_SIZE * sizeof(uint8_t)); + palUart->TxBuffer = (uint8_t *)platform_malloc(UART_TX_BUFER_SIZE * sizeof(uint8_t)); if (palUart->TxBuffer == NULL) { NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); } - palUart->RxBuffer = (uint8_t *)malloc(UART_RX_BUFER_SIZE * sizeof(uint8_t)); + palUart->RxBuffer = (uint8_t *)platform_malloc(UART_RX_BUFER_SIZE * sizeof(uint8_t)); if (palUart->RxBuffer == NULL) { - free(palUart->TxBuffer); + platform_free(palUart->TxBuffer); NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); } diff --git a/targets/FreeRTOS/NXP/_nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp b/targets/FreeRTOS/NXP/_nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp index 67baf891b2..1b53f8998a 100644 --- a/targets/FreeRTOS/NXP/_nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp +++ b/targets/FreeRTOS/NXP/_nanoCLR/Windows.Devices.SerialCommunication/win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice.cpp @@ -170,8 +170,8 @@ HRESULT Library_win_dev_serial_native_Windows_Devices_SerialCommunication_Serial NF_PAL_UART *palUart = Uart_PAL[uartNum]; // Free ring buffers memory - free(palUart->TxBuffer); - free(palUart->RxBuffer); + platform_free(palUart->TxBuffer); + platform_free(palUart->RxBuffer); // Deinitialize device and delete FreeRTOS idle tasks LPUART_Deinit(base); @@ -208,16 +208,16 @@ HRESULT Library_win_dev_serial_native_Windows_Devices_SerialCommunication_Serial NF_PAL_UART *palUart = Uart_PAL[uartNum]; // Allocate memory for TX and RX circular buffer - palUart->TxBuffer = (uint8_t *)malloc(UART_TX_BUFER_SIZE * sizeof(uint8_t)); + palUart->TxBuffer = (uint8_t *)platform_malloc(UART_TX_BUFER_SIZE * sizeof(uint8_t)); if (palUart->TxBuffer == NULL) { NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); } - palUart->RxBuffer = (uint8_t *)malloc(UART_RX_BUFER_SIZE * sizeof(uint8_t)); + palUart->RxBuffer = (uint8_t *)platform_malloc(UART_RX_BUFER_SIZE * sizeof(uint8_t)); if (palUart->RxBuffer == NULL) { - free(palUart->TxBuffer); + platform_free(palUart->TxBuffer); NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); } diff --git a/targets/FreeRTOS/NXP/_nanoCLR/Windows.Storage/win_storage_native_Windows_Storage_StorageFolder.cpp b/targets/FreeRTOS/NXP/_nanoCLR/Windows.Storage/win_storage_native_Windows_Storage_StorageFolder.cpp index acdf29d9ae..e8158630e8 100644 --- a/targets/FreeRTOS/NXP/_nanoCLR/Windows.Storage/win_storage_native_Windows_Storage_StorageFolder.cpp +++ b/targets/FreeRTOS/NXP/_nanoCLR/Windows.Storage/win_storage_native_Windows_Storage_StorageFolder.cpp @@ -124,7 +124,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: workingDrive)); // malloc stringBuffer to work with FS - char *stringBuffer = (char *)malloc(FF_LFN_BUF + 1); + char *stringBuffer = (char *)platform_malloc(FF_LFN_BUF + 1); // sanity check for successfull malloc if (stringBuffer == NULL) @@ -157,7 +157,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: stringBuffer)); // free stringBuffer - free(stringBuffer); + platform_free(stringBuffer); } // move pointer to the next folder item @@ -274,8 +274,8 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: if (directoryCount > 0) { // allocate memory for buffers - stringBuffer = (char *)malloc(FF_LFN_BUF + 1); - workingBuffer = (char *)malloc(2 * FF_LFN_BUF + 1); + stringBuffer = (char *)platform_malloc(FF_LFN_BUF + 1); + workingBuffer = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (stringBuffer == NULL || workingBuffer == NULL) @@ -351,11 +351,11 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: // free buffers memory, if allocated if (stringBuffer != NULL) { - free(stringBuffer); + platform_free(stringBuffer); } if (workingBuffer != NULL) { - free(workingBuffer); + platform_free(workingBuffer); } NANOCLR_CLEANUP_END(); @@ -467,8 +467,8 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: if (fileCount > 0) { // allocate memory for buffers - stringBuffer = (char *)malloc(FF_LFN_BUF + 1); - workingBuffer = (char *)malloc(2 * FF_LFN_BUF + 1); + stringBuffer = (char *)platform_malloc(FF_LFN_BUF + 1); + workingBuffer = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (stringBuffer == NULL || workingBuffer == NULL) @@ -567,11 +567,11 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: // free buffers memory, if allocated if (stringBuffer != NULL) { - free(stringBuffer); + platform_free(stringBuffer); } if (workingBuffer != NULL) { - free(workingBuffer); + platform_free(workingBuffer); } NANOCLR_CLEANUP_END(); @@ -621,8 +621,8 @@ static void CreateFileWorkingThread(void *arg) f_close(&file); // free memory - free((void *)fileIoOperation->FilePath); - free(fileIoOperation); + platform_free((void *)fileIoOperation->FilePath); + platform_free(fileIoOperation); // fire event for FileIO operation complete Events_Set(SYSTEM_EVENT_FLAG_STORAGE_IO); @@ -672,7 +672,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: { // setup file path - filePath = (char *)malloc(2 * FF_LFN_BUF + 1); + filePath = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (filePath == NULL) @@ -714,7 +714,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: // setup File operation StorageFolderFileOperation *fileOperation = - reinterpret_cast(malloc(sizeof(StorageFolderFileOperation))); + reinterpret_cast(platform_malloc(sizeof(StorageFolderFileOperation))); fileOperation->FilePath = filePath; fileOperation->mode = modeFlags; @@ -780,7 +780,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: storageFile = stack.TopValue().Dereference(); // setup file path - filePath = (char *)malloc(2 * FF_LFN_BUF + 1); + filePath = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (filePath == NULL) @@ -810,7 +810,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: Library_win_storage_native_Windows_Storage_StorageFile::FIELD___dateCreated, HAL_Time_CurrentDateTime(false)); - free(filePath); + platform_free(filePath); // done here break; @@ -857,7 +857,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: // get a pointer to the desired folder name folderName = stack.Arg1().DereferenceString()->StringText(); - folderPath = (char *)malloc(2 * FF_LFN_BUF + 1); + folderPath = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (folderPath == NULL) @@ -967,7 +967,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder:: // free buffer memory, if allocated if (folderPath != NULL) { - free(folderPath); + platform_free(folderPath); } NANOCLR_CLEANUP_END(); @@ -1109,7 +1109,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::GetFolderNativ // get a pointer to the desired folder name folderName = stack.Arg1().DereferenceString()->StringText(); - folderPath = (char *)malloc(2 * FF_LFN_BUF + 1); + folderPath = (char *)platform_malloc(2 * FF_LFN_BUF + 1); // sanity check for successfull malloc if (folderPath == NULL) @@ -1177,7 +1177,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::GetFolderNativ // free buffer memory, if allocated if (folderPath != NULL) { - free(folderPath); + platform_free(folderPath); } NANOCLR_CLEANUP_END(); diff --git a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/platform_heap.c b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/platform_heap.c index 8324e9a085..174b7fc57a 100644 --- a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/platform_heap.c +++ b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/platform_heap.c @@ -4,15 +4,40 @@ // #include +#include + +void *platform_malloc(size_t size) +{ + + // need to undef in order to call the real function +#undef malloc -void* platform_malloc(size_t size) { return malloc(size); + + // define back +#define malloc YOU_SHALL_NOT_USE_malloc } -void platform_free(void* ptr) { +void platform_free(void *ptr) +{ + +// need to undef in order to call the real function +#undef free + free(ptr); + + // define back +#define free YOU_SHALL_NOT_USE_free } -void* platform_realloc(void* ptr, size_t size) { +void *platform_realloc(void *ptr, size_t size) +{ + +// need to undef in order to call the real function +#undef realloc + return realloc(ptr, size); + + // define back +#define realloc YOU_SHALL_NOT_USE_realloc } diff --git a/targets/TI_SimpleLink/_common/platform_heap.c b/targets/TI_SimpleLink/_common/platform_heap.c index 4c356201ab..894418e76d 100644 --- a/targets/TI_SimpleLink/_common/platform_heap.c +++ b/targets/TI_SimpleLink/_common/platform_heap.c @@ -5,14 +5,38 @@ #include -void* platform_malloc(size_t size) { +void *platform_malloc(size_t size) +{ + + // need to undef in order to call the real function +#undef malloc + return malloc(size); + + // define back +#define malloc YOU_SHALL_NOT_USE_malloc } -void platform_free(void* ptr) { +void platform_free(void *ptr) +{ + +// need to undef in order to call the real function +#undef free + free(ptr); + + // define back +#define malloc YOU_SHALL_NOT_USE_free } -void* platform_realloc(void* ptr, size_t size) { +void *platform_realloc(void *ptr, size_t size) +{ + +// need to undef in order to call the real function +#undef realloc + return realloc(ptr, size); + + // define back +#define realloc YOU_SHALL_NOT_USE_realloc } diff --git a/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_LinkLocalTask.c b/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_LinkLocalTask.c index ecfcf29a5c..05a2ca0fb1 100644 --- a/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_LinkLocalTask.c +++ b/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_LinkLocalTask.c @@ -570,7 +570,7 @@ void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest, SlNe return; } - netAppRequest = (SlNetAppRequest_t *)malloc(sizeof(SlNetAppRequest_t)); + netAppRequest = (SlNetAppRequest_t *)platform_malloc(sizeof(SlNetAppRequest_t)); if (NULL == netAppRequest) { NetAppRequestErrorResponse(pNetAppResponse); @@ -587,11 +587,11 @@ void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest, SlNe /* Copy Metadata */ if (pNetAppRequest->requestData.MetadataLen > 0) { - netAppRequest->requestData.pMetadata = (uint8_t *)malloc(pNetAppRequest->requestData.MetadataLen); + netAppRequest->requestData.pMetadata = (uint8_t *)platform_malloc(pNetAppRequest->requestData.MetadataLen); if (NULL == netAppRequest->requestData.pMetadata) { NetAppRequestErrorResponse(pNetAppResponse); - free(netAppRequest); + platform_free(netAppRequest); return; } sl_Memcpy( @@ -608,16 +608,16 @@ void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest, SlNe /* Copy the payload */ if (pNetAppRequest->requestData.PayloadLen > 0) { - netAppRequest->requestData.pPayload = (uint8_t *)malloc(pNetAppRequest->requestData.PayloadLen); + netAppRequest->requestData.pPayload = (uint8_t *)platform_malloc(pNetAppRequest->requestData.PayloadLen); if (NULL == netAppRequest->requestData.pPayload) { NetAppRequestErrorResponse(pNetAppResponse); if (netAppRequest->requestData.pMetadata != NULL) { - free(netAppRequest->requestData.pMetadata); + platform_free(netAppRequest->requestData.pMetadata); } - free(netAppRequest); + platform_free(netAppRequest); return; } sl_Memcpy( @@ -2604,7 +2604,7 @@ void *linkLocalTask(void *pvParameters) // } /* Setup mutex operations for sensors reading */ - sensorLockObj = malloc(sizeof(pthread_mutex_t)); + sensorLockObj = platform_malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(sensorLockObj, (pthread_mutexattr_t *)NULL); /* initializes mailbox for http messages */ @@ -2685,13 +2685,13 @@ void *linkLocalTask(void *pvParameters) if (netAppRequest->requestData.MetadataLen > 0) { - free(netAppRequest->requestData.pMetadata); + platform_free(netAppRequest->requestData.pMetadata); } if (netAppRequest->requestData.PayloadLen > 0) { - free(netAppRequest->requestData.pPayload); + platform_free(netAppRequest->requestData.pPayload); } - free(netAppRequest); + platform_free(netAppRequest); } } diff --git a/targets/win32/nanoCLR/platform_heap.cpp b/targets/win32/nanoCLR/platform_heap.cpp index ac1739c57f..d895eb7327 100644 --- a/targets/win32/nanoCLR/platform_heap.cpp +++ b/targets/win32/nanoCLR/platform_heap.cpp @@ -5,17 +5,40 @@ #include "stdafx.h" +#include + void *platform_malloc(size_t size) { + + // need to undef in order to call the real function +#undef malloc + return malloc(size); + + // define back +#define malloc YOU_SHALL_NOT_USE_malloc } void platform_free(void *ptr) { + +// need to undef in order to call the real function +#undef free + free(ptr); + + // define back +#define free YOU_SHALL_NOT_USE_free } void *platform_realloc(void *ptr, size_t size) { - return ptr; + +// need to undef in order to call the real function +#undef realloc + + return realloc(ptr, size); + + // define back +#define realloc YOU_SHALL_NOT_USE_realloc }