am 46215a8b: am dfeb42ed: Merge "If the kernel doesn\'t have MADV_MERGEABLE, stop asking for it."

* commit '46215a8b53e149de3fc28600c2b2eebe66d36716':
  If the kernel doesn't have MADV_MERGEABLE, stop asking for it.
This commit is contained in:
Elliott Hughes 2014-03-03 20:55:35 +00:00 committed by Android Git Automerger
commit 1eb606df0a

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;