Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 1 | //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 5 | // This file was developed by Jeff Cohen and is distributed under the |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of various Memory |
| 11 | // management utilities |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 15 | #include "Win32.h" |
| 16 | #include "llvm/System/Memory.h" |
| 17 | #include "llvm/System/Process.h" |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
| 20 | using namespace sys; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 23 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 24 | //=== and must not be UNIX code |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 27 | MemoryBlock Memory::AllocateRWX(unsigned NumBytes) { |
| 28 | if (NumBytes == 0) return MemoryBlock(); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 29 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 30 | static const long pageSize = Process::GetPageSize(); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 31 | unsigned NumPages = (NumBytes+pageSize-1)/pageSize; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 32 | |
| 33 | void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT, |
| 34 | PAGE_EXECUTE_READWRITE); |
| 35 | if (pa == NULL) { |
| 36 | ThrowError("Can't allocate RWX Memory: "); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 37 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 38 | |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 39 | MemoryBlock result; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 40 | result.Address = pa; |
| 41 | result.Size = NumPages*pageSize; |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 42 | return result; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 45 | void Memory::ReleaseRWX(MemoryBlock& M) { |
| 46 | if (M.Address == 0 || M.Size == 0) return; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 47 | if (!VirtualFree(M.Address, 0, MEM_RELEASE)) { |
| 48 | ThrowError("Can't release RWX Memory: "); |
| 49 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 50 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame^] | 51 | |
| 52 | } |
| 53 | |
| 54 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |