Memory is allocated correctly, but a subsequent free(p) on the pointer to memory returned from malloc does not add the chunk to the fastbin list, meaning future mallocs less than 64 bytes will be unable to utilize this chunk, and will allocate new memory instead.
Memory allocation / freeing of chunks 64 bytes and larger are unaffected.
This bug occurs in the MicroBlaze tool chain, and will be addressed in the next PetaLinux release.
A workaround is to disable the use of fastbins for small memory chunks, by calling;
mallopt( M_MXFAST, 0 );
BEFORE the malloc call.
Alternatively,users can lower the M_MMAP_THRESHOLD to 0, which will tune malloc to use the mmap syscall for all memory allocations (normally used only for allocating larger chunks >= ~128Kbytes), again bypassing the usage of fastbin by setting the threshold to 0 bytes via mallopt;
mallopt( M_MMAP_THRESHOLD, 0 );
or by exporting environment variable;
(Note the trailing '_')
The use of mmap for all memory allocations though has the disadvantage of being a more expensive task for the kernel than brk, and not being able to reuse deallocated space in future allocations. However, this approach has the advantage that it can be enabled without modifying the application program, instead simply setting an environment variable.