OSAllocatorPosix.cpp [plain text]
#include "config.h"
#include "OSAllocator.h"
#if OS(UNIX)
#include "PageAllocation.h"
#include <errno.h>
#include <sys/mman.h>
#include <wtf/Assertions.h>
namespace WTF {
void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages)
{
#if OS(LINUX)
UNUSED_PARAM(usage);
UNUSED_PARAM(writable);
UNUSED_PARAM(executable);
UNUSED_PARAM(includesGuardPages);
void* result = mmap(0, bytes, PROT_NONE, MAP_NORESERVE | MAP_PRIVATE | MAP_ANON, -1, 0);
if (result == MAP_FAILED)
CRASH();
madvise(result, bytes, MADV_DONTNEED);
#else
void* result = reserveAndCommit(bytes, usage, writable, executable, includesGuardPages);
#if HAVE(MADV_FREE_REUSE)
if (result) {
while (madvise(result, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
}
#endif
#endif
return result;
}
void* OSAllocator::reserveAndCommit(size_t bytes, Usage usage, bool writable, bool executable, bool includesGuardPages)
{
int protection = PROT_READ;
if (writable)
protection |= PROT_WRITE;
if (executable)
protection |= PROT_EXEC;
int flags = MAP_PRIVATE | MAP_ANON;
#if PLATFORM(IOS)
if (executable)
flags |= MAP_JIT;
#endif
#if OS(DARWIN)
int fd = usage;
#else
UNUSED_PARAM(usage);
int fd = -1;
#endif
void* result = 0;
#if (OS(DARWIN) && CPU(X86_64))
if (executable) {
ASSERT(includesGuardPages);
intptr_t randomLocation = 0;
randomLocation = arc4random() & ((1 << 25) - 1);
randomLocation += (1 << 24);
randomLocation <<= 21;
result = reinterpret_cast<void*>(randomLocation);
}
#endif
result = mmap(result, bytes, protection, flags, fd, 0);
if (result == MAP_FAILED) {
if (executable)
result = 0;
else
CRASH();
}
if (result && includesGuardPages) {
mmap(result, pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0);
mmap(static_cast<char*>(result) + bytes - pageSize(), pageSize(), PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, fd, 0);
}
return result;
}
void OSAllocator::commit(void* address, size_t bytes, bool writable, bool executable)
{
#if OS(LINUX)
int protection = PROT_READ;
if (writable)
protection |= PROT_WRITE;
if (executable)
protection |= PROT_EXEC;
if (mprotect(address, bytes, protection))
CRASH();
madvise(address, bytes, MADV_WILLNEED);
#elif HAVE(MADV_FREE_REUSE)
UNUSED_PARAM(writable);
UNUSED_PARAM(executable);
while (madvise(address, bytes, MADV_FREE_REUSE) == -1 && errno == EAGAIN) { }
#else
UNUSED_PARAM(address);
UNUSED_PARAM(bytes);
UNUSED_PARAM(writable);
UNUSED_PARAM(executable);
#endif
}
void OSAllocator::decommit(void* address, size_t bytes)
{
#if OS(LINUX)
madvise(address, bytes, MADV_DONTNEED);
if (mprotect(address, bytes, PROT_NONE))
CRASH();
#elif HAVE(MADV_FREE_REUSE)
while (madvise(address, bytes, MADV_FREE_REUSABLE) == -1 && errno == EAGAIN) { }
#elif HAVE(MADV_FREE)
while (madvise(address, bytes, MADV_FREE) == -1 && errno == EAGAIN) { }
#elif HAVE(MADV_DONTNEED)
while (madvise(address, bytes, MADV_DONTNEED) == -1 && errno == EAGAIN) { }
#else
UNUSED_PARAM(address);
UNUSED_PARAM(bytes);
#endif
}
void OSAllocator::releaseDecommitted(void* address, size_t bytes)
{
int result = munmap(address, bytes);
if (result == -1)
CRASH();
}
}
#endif // OS(UNIX)