configTOTAL_HEAP_SIZE byte exactly with heap_1


I use heap_2 memory management in my application. All objects are initialised during init, so I use dynamically allocated RAM. I optimized value of configTOTAL_HEAP_SIZE that in the end it holds exact amount of bytes to setup objects.

Let us use following values as complicity:

configTOTAL_HEAP_SIZE = 12 (assume RTOS objects will use 8 bytes (let us say 2 objects and each is 4 bytes) and portBYTE_ALIGNMENT is 4 bytes)
configADJUSTED_HEAP_SIZE = 8 (calculated in memory management with portBYTE_ALIGNMENT )

After init is done value of xFreeBytesRemaining is 0 -> fine because it counts down.

Now I replaced heap_2 handling with heap_1.

With same example from above I see the following Problem during init when the second object (4 bytes) is created in function pvPortMalloc of heap_1 :

	/* Check there is enough room left for the allocation. */
	if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&
		( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )	)/* Check for overflow. */

First if condition with the example would lead into:

if( ( (4 + 4) < 8)

So it will not create the object. But in my opinion the heap is allocated correctly with 8 bytes for the two objects and the statement must be less than or equal instead just less than.

Am I missing something?

Thanks in advance!

Is the problem you are reporting that the heap_1 claims to be empty when it still has one byte left in it? If so I would not bee too concerned about it - especially as it is not possible to allocate one byte because each allocation also includes some metadata, so requesting 1 byte will actually remove more than 1 byte from the available heap space - could that be the cause of the issue?

The whole heap size in my application is something around 32k bytes (tasks, mutex, queues). When the TCB for the last task is created it needs 88 bytes and I have 88 bytes left, but it fails with mentioned if-statement.

I think his problem is that if the allocation will exactly remove the last byte in the heap memory pool, because xNextFreeByte + xWantedSize == configADJUSTED_HEAP_SIZE, then the allocation fails, so you need to over allocate you heap to allow for at least a single unused byte at the end.

Allocate one additional byte (e.g. configTOTAL_HEAP_SIZE +1) is of course a working solution, but in my opinion a waste of 1 byte if the heap is allocated correctly and it just fails because of the if-statement.