support near allocations for the JIT
[oota-llvm.git] / lib / System / Win32 / Memory.inc
1 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Jeff Cohen and is distributed under the 
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
15 #include "Win32.h"
16 #include "llvm/System/Process.h"
17
18 namespace llvm {
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code 
23 //===          and must not be UNIX code
24 //===----------------------------------------------------------------------===//
25
26 MemoryBlock Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock) {
27   if (NumBytes == 0) return MemoryBlock();
28
29   static const long pageSize = Process::GetPageSize();
30   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
31
32   //FIXME: support NearBlock if ever needed on Win64.
33
34   void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
35                   PAGE_EXECUTE_READWRITE);
36   if (pa == NULL) {
37     ThrowError("Can't allocate RWX Memory: ");
38   }
39
40   MemoryBlock result;
41   result.Address = pa;
42   result.Size = NumPages*pageSize;
43   return result;
44 }
45
46 void Memory::ReleaseRWX(MemoryBlock& M) {
47   if (M.Address == 0 || M.Size == 0) return;
48   if (!VirtualFree(M.Address, 0, MEM_RELEASE)) {
49     ThrowError("Can't release RWX Memory: ");
50   }
51 }
52
53 }
54