A question about heap_5

I think, the pxNextFreeBlock of in struct A_BLOCK_LINK can be used when the block is allocated. It is not necessary for allocated blocks . So, we can use this to save our data.20210526163933

vPortFree() expects pxNextFreeBlock to be NULL, so if your application uses pxNextFreeBlock while the block is allocated, then it must set pxNextFreeBlock back to NULL before the block is freed.

pvPortMalloc() will set pxNextFreeBlock to be NULL while the block is allocated. Then vPortFree() will check this. I do not know what is target, for safty or checking whether the block is corrupt? I want to know, whether the design that pvPortMalloc() can allocate this memory to user, and vPortFree() do not check this is available. so I think pxNextFreeBlock can be used.

It’s a simple but fast verification to detect e.g. double free and/or that the block to be freed was indeed taken from heap and not from stack.
With a certain probability those kind of common mistakes are detected.
What do you want to achieve ?
Why not handling hidden members/elements of a memory block in your own pvPortMalloc/Free wrappers ? (Take care of alignment of the final memory address.)
It should be easy to add without touching the underlying heap implementation and allows to easily switch to another heap in case it’s desired.

This is a very good point. It reminds me of one of the mantras of software development: Always code as if the one who needs to maintain your code is a serial killer who knows your address.

To make assumptions about the inner workings of memory management just to shell out 4 additional bytes per block is a sure recipe for nightmares when changing your memory management layer.

Another thing to consider is what happens when the memory manger collates free blocks to counter fragmentation. I don’t know heap_5.c well enough to understand what impact the TOs assumption would have in that case, but since collating is a process that may only show up under heavier memory usage load, these fringe conditions may slip a good deal of testing and (according to Murphy 3.4, subclause 25) will manifest itself only in a mission critical scenario at full moon in the field.

1 Like

thanks for your answer. I got it.