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

Keith Packard <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Ola Olsson <[email protected]> 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
signature.asc (application/pgp-signature, 832 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEw4O3eCVWE9/bQJ2R2yIaaQAAABEFAmCG3NIACgkQ2yIaaQAA
ABG7aBAAjuk8zvxgVI8CGuHhQAejXRkdG0j465ewpVU9gvbu2/epJAD0AvvqPgoa
BuzZ6P9XKFbl/7YrS9jimR3wAQscDI1W5ONSYExwJI3C60hJb4VmfxiEMjCgYEMO
4VqxDjEBnOenztAi2ktF5fLrOFf7WHiZA0+46iMTk4ioA6KBrD+Nlj+m2XTcWIAD
nwKS+o0+GfNbF5EeQs4BapI09kgNqNRu47gaaRLDaQMndiuGjcu3I0FZQVnALGIT
SnWHvY9TD4+cc8blIdFSJOMAfZJFtKN8E771+dkSZ0cT855PP/BWBvlIXZkk5iwn
wxmNtblnlaVkxZFtm3E3ai/CuKCGP8N7gvvjP8Tf0AMPMf3Ib4AIosBjmypKwfrx
iLGfDIhD8VvYe5bQ5JFHRpv5+B6ofotrMAPVj5A6mJ2QCo6+g2xGmHHppzMP8luI
o/VN89kT08ED2WyFRuvxutqrP9xT1cJDrmQ8+E9o2oxeWvTwjAmZ1+3qm1sXW57m
jpar7Pm/Qbs+76CSx77WX54gitXWqvK83M8t3j4MogZLbZQ/P0CwSm5xPI65SqIF
ww6o11bLOK4tX7TwyMMTSrQKbxepX0WHOq8JLsmOkL4zWd1jjRBuc8ymPEfKdVkZ
P9ljOcoscLd8d1W7K8QD4Rn73MNx0PVnU7kt9Ts30z2APz7W5fI=
=FYX2
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.