FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [February 2017 Threads] Heap_2.cPosted by singosingh on February 5, 2017 void *pvPortMalloc( sizet xWantedSize )
{
xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
static portBASETYPE xHeapHasBeenInitialised = pdFALSE;
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( xHeapHasBeenInitialised == pdFALSE )
{
prvHeapInit();
xHeapHasBeenInitialised = pdTRUE;
}
/* The wanted size is increased so it can contain a xBlockLink
structure in addition to the requested amount of bytes. */
if( xWantedSize > 0 )
{
xWantedSize += heapSTRUCT_SIZE;
/* Ensure that blocks are always aligned to the required number of bytes. */
if( xWantedSize & portBYTE_ALIGNMENT_MASK )
{
/* Byte alignment required. */
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
}
if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )
{
/* Blocks are stored in byte order - traverse the list from the start
(smallest) block until one of adequate size is found. */
pxPreviousBlock = &xStart;
pxBlock = xStart.pxNextFreeBlock;
while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock ) )
{
pxPreviousBlock = pxBlock;
pxBlock = pxBlock->pxNextFreeBlock;
}
/* If we found the end marker then a block of adequate size was not found. */
if( pxBlock != &xEnd )
{
/* Return the memory space - jumping over the xBlockLink structure
at its start. */
pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
/* This block is being returned for use so must be taken our of the
list of free blocks. */
pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
/* If the block is larger than required it can be split into two. */
if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
{
/* This block is to be split into two. Create a new block
following the number of bytes requested. The void cast is
used to prevent byte alignment warnings from the compiler. */
pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );
/* Calculate the sizes of two blocks split from the single
block. */
pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
pxBlock->xBlockSize = xWantedSize;
/* Insert the new block into the list of free blocks. */
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
}
xFreeBytesRemaining -= pxBlock->xBlockSize;
}
}
}
xTaskResumeAll();
#if( configUSE_MALLOC_FAILED_HOOK == 1 )
{
if( pvReturn == NULL )
{
extern void vApplicationMallocFailedHook( void );
vApplicationMallocFailedHook();
}
}
#endif
return pvReturn;
}
/-----------------------------------------------------------/
void vPortFree( void *pv )
{
unsigned char *puc = ( unsigned char * ) pv;
xBlockLink *pxLink;
if( pv )
{
/* The memory being freed will have an xBlockLink structure immediately
before it. */
puc -= heapSTRUCT_SIZE;
/* This casting is to keep the compiler from issuing warnings. */
pxLink = ( void * ) puc;
vTaskSuspendAll();
{
/* Add this block to the list of free blocks. */
prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
xFreeBytesRemaining += pxLink->xBlockSize;
}
xTaskResumeAll();
}
}
In the FreeRtos heap2_.c file where is the xwantedsize declared before or what does that mean.Please do guide me I am new to embedded system.
Heap_2.cPosted by rtel on February 5, 2017
In the FreeRtos heap2_.c file where is the xwantedsize declared before
Sorry I don't understand your question. xWantedSize is passed in as a
parameter.
or what does that mean.Please do guide me I am new to embedded system.
Are you asking what the function does? It is doing exactly the same as
the standard library malloc() function.
Dynamic memory allocation in C:
https://en.wikipedia.org/wiki/Cdynamicmemory_allocation
Specific to FreeRTOS:
http://www.freertos.org/a00111.html
See the memory allocation information in the book:
http://www.freertos.org/Documentation/RTOS_book.html
Heap_2.cPosted by singosingh on February 6, 2017 My question is i have created a heap size of 10240 bytes and have used heap_2 memory management. xWantedsize is passed in as a parameter from where???. What is the size in number of xWantedSize(or) is it the size required by the application that is dynamically allocated by the free RTOS .
Thank you
Heap_2.cPosted by heinbali01 on February 6, 2017
What is the size in number of xWantedSize(or) is it the size required
by the application that is dynamically allocated by the free RTOS .
I'm sorry but your question is difficult to understand.
configTOTAL_HEAP_SIZE is in number of bytes, not words:
~~~~
static uint8t ucHeap[ configTOTALHEAP_SIZE ];
~~~~
xWantedSize , the parameter of pvPortMalloc indicates the number of bytes that you want to allocate:
~~~~
void *pvPortMalloc( size_t xWantedSize )
~~~~
The functions pvPortMalloc() and vPortFree() are alternatives for the C-functions malloc() and free() . The alternatives are created because they are thread-safe (not interrupt-safe). The standard C-functions can be used but they must be wrapped (this is done in heap_3.c).
It is up to you which module you want to use.
Here is an example:
~~~~
/* Allocate 128 bytes: /
char *pcBuffer = ( char * )pvPortMalloc( 128 );
if( pcBuffer != NULL )
{
/ Use the buffer: /
strcpy( pcBuffer, "Hello worldn" );
/ And free it again: */
vPortFree( pcBuffer );
}
~~~~
I hope this answered your questions.
Heap_2.cPosted by singosingh on February 6, 2017 Thank Bro Now I understood
I have another question
In the heap_2.c we create a free list and then add the memory blocks to the list from small size to large size memory blocks.My question is from where Do we get the size of the blocks.For Instance Let there be 25byte ,5byte and 35 byte free memory space in heap.I need a memory allocation of say 30byte.Now according tot the heap2 configuration the 35 byte memory space is selected and spiltted.My question is how the free space 25byte ,5byte and 35byte is recognised in the heap2 to coding file .Please make me understand the line 112 to 130 in the attached file and particularly I dont understand why pxportblockInsert is used as a pointer.But it is defined as a macro.
Thank you in advance
Heap_2.cPosted by heinbali01 on February 6, 2017 I would encourage you to use heap_4.c in stead of heap_2.c .
heap_4.c : the idea is as follows: in the beginning there is one large free block. You start allocating blocks from left to right. When a block of memory is freed, the algorithm looks if the adjacent blocks are also 'free blocks'. If so, they will be joined to become one large free block. After freeing all individual blocks, the original situation will be restored: one big free block of memory.
My question is how the free space 25byte, 5byte and 35byte
is recognised in the heap2 to coding file
heap_2.c contains less code and in some cases it will be more efficient. It keeps a linked-list of free memory blocks, sorted on size. But it doesn't bother re-joining those free memory blocks.
If you want to read more: have a look at all documentation, pointed out by Richard:
Dynamic memory allocation in C:
https://en.wikipedia.org/wiki/Cdynamicmemory_allocation
Specific to FreeRTOS:
http://www.freertos.org/a00111.html
See the memory allocation information in the book:
http://www.freertos.org/Documentation/RTOS_book.html
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|