[PATCH] Nano-malloc: Fix for unwanted external heap fragmentation

Keith Packard keithp@keithp.com
Mon Apr 26 15:31:30 GMT 2021


Ola Olsson <ola1olsson@gmail.com> writes:

> The solution for this problem is to check if the last
> item in the free list is adjacent to sbrk(0). If it is,
> as it is in this case, we can just ask sbrk for the
> remainder of what is needed. In this case 1 byte.

I've already implemented both of these suggestions in the nano malloc
version included as part of picolibc. This version has some significant
additional work which fixes issues with memalign (does anyone use this
on embedded systems?).

https://github.com/picolibc/picolibc/blob/main/newlib/libc/stdlib/nano-mallocr.c

Growing the chunk adjacent to the brk is useful for both malloc and
realloc as this allows the common pattern of a buffer which grows
incrementally to be realloc'd in place.

I was hoping to get some time to push this upstream to newlib as you're
quite correct that having these two changes is very useful on limited
memory devices. However, I have been redirected away from this work.

There are also a couple of tests included with picolibc that uncovered
the issues with the original implementation:

https://github.com/picolibc/picolibc/blob/main/test/malloc.c
https://github.com/picolibc/picolibc/blob/main/test/malloc_stress.c

-- 
-keith
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/newlib/attachments/20210426/01ce0beb/attachment.sig>


More information about the Newlib mailing list