Zero a large memory mapping with `madvise`

前端 未结 3 1674
南旧
南旧 2021-02-06 05:50

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

3条回答
  •  忘掉有多难
    2021-02-06 06:08

    There is a much easier solution to your problem that is fairly portable:

    mmap(ptr, length, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    

    Since MAP_FIXED is permitted to fail for fairly arbitrary implementation-specific reasons, falling back to memset if it returns MAP_FAILED would be advisable.

提交回复
热议问题