msize function?

bremenpl wrote on Tuesday, September 25, 2018:

Hello there,
I was wondering either there is a msize function available that would determine the amount of dynamically allocated memory with freertos malloc port function? I would appreciate all help.

rtel wrote on Tuesday, September 25, 2018:

FreeRTOS keeps memory allocation in the port layer - so the answer is ‘it depends on which memory allocator you are using’. If you are using heap_1, heap_2, heap_4 or heap_5 that come with the kernel download then the answer is no - there is no msize function - but (in heap_4 and heap_5 at least) each block starts with the following structure:

typedef struct A_BLOCK_LINK
	struct A_BLOCK_LINK *pxNextFreeBlock;	/*<< The next free block in the list. */
	size_t xBlockSize;						/*<< The size of the free block. */
} BlockLink_t;

so you could create one quite easily. This structure is BEFORE the memory returned by malloc(). If you look at the implementation of vPortFree() in heap_4.c you will see how to access it. here is a snippet of the code, you will see that you have to mask off the most significant bit to get the size:

	if( pv != NULL )
		/* The memory being freed will have an BlockLink_t structure immediately
		before it. */
		puc -= xHeapStructSize;

		/* This casting is to keep the compiler from issuing warnings. */
		pxLink = ( void * ) puc;

		/* Check the block is actually allocated. */
		configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
		configASSERT( pxLink->pxNextFreeBlock == NULL );

bremenpl wrote on Tuesday, September 25, 2018:

Thank you!