Index: src/platform-linux.cc |
diff --git a/src/platform-linux.cc b/src/platform-linux.cc |
index 00852baeebf66cd3be61a51ee10ade941bc98603..da5d81bd45fae5001a2bf7074bc38cc3a7e490ba 100644 |
--- a/src/platform-linux.cc |
+++ b/src/platform-linux.cc |
@@ -650,6 +650,34 @@ void* VirtualMemory::ReserveRegion(size_t size) { |
} |
+void* VirtualMemory::ReserveAlignedRegion(size_t size, size_t alignment) { |
+ ASSERT(IsAligned(alignment, static_cast<intptr_t>(OS::AllocateAlignment()))); |
+ size_t request_size = RoundUp(size + alignment, |
+ static_cast<intptr_t>(OS::AllocateAlignment())); |
+ void* reservation = mmap(GetRandomMmapAddr(), |
+ request_size, |
+ PROT_NONE, |
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, |
+ kMmapFd, |
+ kMmapFdOffset); |
+ if (reservation == MAP_FAILED) return NULL; |
+ Address base = static_cast<Address>(reservation); |
+ Address aligned_base = RoundUp(base, alignment); |
+ ASSERT(base <= aligned_base); |
+ |
+ // Unmap extra memory reserved before and after the desired block. |
+ size_t bytes_prior = static_cast<size_t>(aligned_base - base); |
+ if (bytes_prior > 0) { |
+ munmap(base, bytes_prior); |
+ } |
+ if (static_cast<size_t>(aligned_base - base) < request_size - size) { |
+ munmap(aligned_base + size, request_size - size - bytes_prior); |
+ } |
+ |
+ return static_cast<void*>(aligned_base); |
+} |
+ |
+ |
bool VirtualMemory::CommitRegion(void* base, size_t size, bool is_executable) { |
int prot = PROT_READ | PROT_WRITE | (is_executable ? PROT_EXEC : 0); |
if (MAP_FAILED == mmap(base, |