[PATCH v5] malloc: Mark pages with MADV_DONTNEED to shrink and grow rather than mremap()

Florian Weimer fweimer@redhat.com
Fri Sep 5 17:02:06 GMT 2025


* Wilco Dijkstra:

>> This doesn't produce an unmapped region:
>>
>>      if ((char *) MMAP ((char *) h + new_size, diff, PROT_NONE,
>>                         MAP_FIXED) == (char *) MAP_FAILED)
>
> Wait, it's actually using mmap? How is that legal on a region that is
> already mmapped?  Shouldn't it use mprotect instead?

It's using MAP_FIXED.  That's only valid if we control the address space
being update, which is the case here.

The behavior of mprotect is quite different: it never deallocates
anything.  PROT_NONE just temporarily hides the memory from view.
The old contents comes back if the protection flags are restored.

Thanks,
Florian



More information about the Libc-alpha mailing list