blob: 8a9ae05f37454ac5be4049b11b4a70fd97c8d8e0 [file] [log] [blame]
Reid Spencercbad7012004-09-11 04:59:30 +00001//===- 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
18namespace llvm {
19using namespace sys;
20
21//===----------------------------------------------------------------------===//
22//=== WARNING: Implementation here must contain only Win32 specific code.
23//===----------------------------------------------------------------------===//
24
25void* 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
41void Memory::ReleaseRWX(Memory& M) {
42 if (M.Address == 0 ) return;
43 VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS);
44}