작성
·
408
답변 1
0
안녕하세요. 김영선님!
이미 알고 계신 내용처럼 malloc 함수를 이용하면 heap 영역에서 메모리 할당이 일어나는 것 맞습니다.
FreeRTOS 는 자체적인 동적 메모리 할당자(pvPortMalloc)를 제공하고 있습니다. 이 함수는 BSS 영역을 동적 메모리 할당을 위한 메모리 풀(pool)로 사용합니다.
아래 소스 코드의 113 라인, 58 라인 참조
../Middlewares/Third_Party/FreeRTOS/Source/portable/MemMang/heap_4.c
113라인
void *pvPortMalloc( size_t xWantedSize )
{
BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
void *pvReturn = NULL;
vTaskSuspendAll();
{
/* If this is the first call to malloc then the heap will require
initialisation to setup the list of free blocks. */
if( pxEnd == NULL )
{
prvHeapInit(); // 메모리 풀 생성
}
else
{
mtCOVERAGE_TEST_MARKER();
}
58라인
/* Allocate the memory for the heap. */
#if( configAPPLICATION_ALLOCATED_HEAP == 1 )
/* The application writer has already defined the array used for the RTOS
heap - probably so it can be placed in a special segment or address. */
extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; // 사용자가 명시적으로 메모리 풀을 제공
#else
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; // BSS 에서 메모리 풀이 생성
#endif /* configAPPLICATION_ALLOCATED_HEAP */
heap 영역(동적메모리)에 굳이 할당하고 싶다면 다음과 같이 하시면 될 것 같네요.
FreeRTOSConfig.h 내에 '#define configAPPLICATION_ALLOCATED_HEAP 1' 선언하신 후
main 함수내에서 FreeRTOS 가 시작하기 전에 아래처럼 메모리 풀을 만들어 줍니다.
uint8_t* ucHeap; // 전역변수로 선언 필수
ucHeap = (uint8_t*)malloc(configTOTAL_HEAP_SIZE);
malloc 함수는 thread safe 하지 않기 때문에 FreeRTOS 가 동작하기 전에만 사용 가능하고요. 당연히 태스크 함수내에서는 사용할 수 없습니다.
xTaskCreateStatic() 의 경우 태스크가 사용할 TCB 와 스택 메모리를 사용자가 명시적으로 선언해 준다는 점 말고는 xTaskCreate() 와 별 차이가 없다고 생각하시면 됩니다.