I have the following problem:
I allocate a large chunk of memory (multiple GiB) via mmap
with MAP_ANONYMOUS
. That chunk holds a large hash map w
This madvise
behavior is certainly not standard, so this wouldn't be portable.
If the part that you want to zero out happens to be at the end of your mapping you could get away with ftruncate
. You'd have to introduce one step more:
shm_open
to have a "persistent" file descriptor to your dataftruncate
to the needed sizemmap
of that FDThen you could always
munmap
ftruncate
to something shortftruncate
to the real length you needmmap
againand then the part that you "remapped" would be zero initialized.
But have also in mind that the system has to do the zeroing of the pages. This might be a bit more efficient than the inline stuff that your compiler produces for memset
, but that is not sure.