Windows/Memory.inc: Support the ability to allocate memory "near" another block of...
authorNAKAMURA Takumi <geek4civic@gmail.com>
Sat, 15 Oct 2011 01:58:16 +0000 (01:58 +0000)
committerNAKAMURA Takumi <geek4civic@gmail.com>
Sat, 15 Oct 2011 01:58:16 +0000 (01:58 +0000)
Thanks to Aaron Ballman!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142039 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Windows/Memory.inc

index 8609d39dd6dfa152c2c6e7e94bc0417b692be33c..fcc72837c4563a53662f6f078a4874871161e3fd 100644 (file)
@@ -32,11 +32,16 @@ MemoryBlock Memory::AllocateRWX(size_t NumBytes,
   static const size_t pageSize = Process::GetPageSize();
   size_t NumPages = (NumBytes+pageSize-1)/pageSize;
 
-  //FIXME: support NearBlock if ever needed on Win64.
+  PVOID start = NearBlock ? static_cast<unsigned char *>(NearBlock->base()) +
+                                NearBlock->size() : NULL;
 
-  void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
+  void *pa = VirtualAlloc(start, NumPages*pageSize, MEM_RESERVE | MEM_COMMIT,
                   PAGE_EXECUTE_READWRITE);
   if (pa == NULL) {
+    if (NearBlock) {
+      // Try again without the NearBlock hint
+      return AllocateRWX(NumBytes, NULL, ErrMsg);
+    }
     MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
     return MemoryBlock();
   }