heinbali01 wrote on Tuesday, December 16, 2014:
A few months ago I have tested heap_4.c thoroughly for several hours in a simulator. I was amazed by its simpleness and its speed. The test was randomly allocating memory and returning other memories. As long as my testing program was well-behaving, it didn’t encounter any problem.
There are two obvious rules for “good behaviour”:
- Never write outside the allocated space
- Only call vPortFree() once for each memory pointer
The heap_x.c modules do have a bit of error checking but it’s very minimal to save CPU time.
I hope you have configASSERT defined:
void vPortFree( void *pv )
/* Check the block is actually allocated. */
configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );
configASSERT( pxLink->pxNextFreeBlock == NULL );
I once added an extra field to check the validity of the memory blocks:
#define INTEGRITY_STAMP_TAKEN 33333
#define INTEGRITY_STAMP_FREE 11111
/* Define the linked list structure.
* This is used to link free blocks in order
* of their memory address. */
typedef struct A_BLOCK_LINK
#if( HEAP_INTEGRITY_TEST != 0 )
struct A_BLOCK_LINK *pxNextFreeBlock; /* The next free block in the list. */
size_t xBlockSize; /* The size of the free block. */
Find attached a version of heap_4.c which has such extension. You might want to have a try with it.