Provide initial implementations of Memory and Process concepts for various
[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 Reid Spencer 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 <llvm/System/Process.h>
16 #include "windows.h"
17
18 namespace llvm {
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code.
23 //===----------------------------------------------------------------------===//
24
25 void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
26   if (NumBytes == 0) return 0;
27
28   unsigned pageSize = Process::GetPageSize();
29   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
30   void *P = VirtualAlloc(0, NumPages*pageSize, MEM_COMMIT, 
31                          PAGE_EXECUTE_READWRITE);
32   if (P == 0) {
33     throw std::string("Couldn't allocate ") + utostr(NumBytes) + 
34         " bytes of executable memory!";
35   }
36   M.Address = P;
37   M.AllocSize = NumBytes;
38   return P;
39 }
40
41 void Memory::ReleaseRWX(Memory& M) {
42   if (M.Address == 0 ) return;
43   VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS);
44 }