If the kernel doesn't have MADV_MERGEABLE, stop asking for it.

Note that a dynamically-linked binary will still probably see two attempts ---
one by the dynamic linker (which will set its copy of the flag so it won't try
again) and then one by the executable itself (which gets a new uninitialized
copy of the flag).

Change-Id: Id6b7e47780f0f24d2ca0384a75373f4824fa8f12
This commit is contained in:
Elliott Hughes 2014-03-03 11:54:21 -08:00
parent 15e71cd760
commit 9bd9b7dd20

View file

@ -37,16 +37,23 @@ extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
#define MMAP2_SHIFT 12 // 2**12 == 4096
static bool kernel_has_MADV_MERGEABLE = true;
void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offset) {
if (offset < 0 || (offset & ((1UL << MMAP2_SHIFT)-1)) != 0) {
errno = EINVAL;
return MAP_FAILED;
}
bool is_private_anonymous = (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0;
void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
if (result != MAP_FAILED && (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0) {
if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE && is_private_anonymous) {
ErrnoRestorer errno_restorer;
madvise(result, size, MADV_MERGEABLE);
int rc = madvise(result, size, MADV_MERGEABLE);
if (rc == -1 && errno == EINVAL) {
kernel_has_MADV_MERGEABLE = false;
}
}
return result;