Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of various Memory |
| 11 | // management utilities |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Win32.h" |
Chandler Carruth | 1ec7df1 | 2009-10-26 01:35:46 +0000 | [diff] [blame] | 16 | #include "llvm/System/DataTypes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/System/Process.h" |
| 18 | |
| 19 | namespace llvm { |
| 20 | using namespace sys; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 24 | //=== and must not be UNIX code |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Reid Kleckner | cc49229 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 27 | MemoryBlock Memory::AllocateRWX(size_t NumBytes, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | const MemoryBlock *NearBlock, |
| 29 | std::string *ErrMsg) { |
| 30 | if (NumBytes == 0) return MemoryBlock(); |
| 31 | |
Reid Kleckner | cc49229 | 2009-07-23 21:46:56 +0000 | [diff] [blame] | 32 | static const size_t pageSize = Process::GetPageSize(); |
| 33 | size_t NumPages = (NumBytes+pageSize-1)/pageSize; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | |
| 35 | //FIXME: support NearBlock if ever needed on Win64. |
| 36 | |
| 37 | void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT, |
| 38 | PAGE_EXECUTE_READWRITE); |
| 39 | if (pa == NULL) { |
| 40 | MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: "); |
| 41 | return MemoryBlock(); |
| 42 | } |
| 43 | |
| 44 | MemoryBlock result; |
| 45 | result.Address = pa; |
| 46 | result.Size = NumPages*pageSize; |
| 47 | return result; |
| 48 | } |
| 49 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) { |
| 51 | if (M.Address == 0 || M.Size == 0) return false; |
| 52 | if (!VirtualFree(M.Address, 0, MEM_RELEASE)) |
| 53 | return MakeErrMsg(ErrMsg, "Can't release RWX Memory: "); |
| 54 | return false; |
| 55 | } |
| 56 | |
Jim Grosbach | c599a35 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 57 | bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) { |
Argiris Kirtzidis | dd3f6c1 | 2008-10-04 08:15:32 +0000 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
Jim Grosbach | c599a35 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 61 | bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | bool Memory::setRangeWritable(const void *Addr, size_t Size) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool Memory::setRangeExecutable(const void *Addr, size_t Size) { |
Argiris Kirtzidis | dd3f6c1 | 2008-10-04 08:15:32 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | } |