Skip to content

Commit

Permalink
Improvements in malloc/free (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Sep 27, 2021
1 parent dd790a8 commit 58a5dc9
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/CLR/CorLib/corlib_native_System_BitConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/HAL/Include/nanoHAL_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>

// 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 <nanoCLR_Headers.h>

#include <nanoHAL_Capabilites.h>
Expand Down
28 changes: 23 additions & 5 deletions targets/ChibiOS/_common/platform_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@
//

#include <ch.h>
#include <nanoHAL_v2.h>

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -714,7 +714,7 @@ HRESULT Library_win_storage_native_Windows_Storage_StorageFolder::

// setup File operation
StorageFolderFileOperation *fileOperation =
reinterpret_cast<StorageFolderFileOperation *>(malloc(sizeof(StorageFolderFileOperation)));
reinterpret_cast<StorageFolderFileOperation *>(platform_malloc(sizeof(StorageFolderFileOperation)));

fileOperation->FilePath = filePath;
fileOperation->mode = modeFlags;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
31 changes: 28 additions & 3 deletions targets/FreeRTOS_ESP32/ESP32_WROOM_32/common/platform_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,40 @@
//

#include <esp32_os.h>
#include <nanoHAL_v2.h>

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
}
30 changes: 27 additions & 3 deletions targets/TI_SimpleLink/_common/platform_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,38 @@

#include <stdlib.h>

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
}
Loading

0 comments on commit 58a5dc9

Please sign in to comment.