blob: 946e50cd6fc2af0481b81d43819ca441a50e098f [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
Reid Spencer33189e72004-09-13 22:38:11 +000025MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
26 if (NumBytes == 0) return MemoryBlock();
Reid Spencercbad7012004-09-11 04:59:30 +000027
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 }
Reid Spencer33189e72004-09-13 22:38:11 +000036 MemoryBlock result;
37 result.Address = P;
38 result.Size = NumBytes;
39 return result;
Reid Spencercbad7012004-09-11 04:59:30 +000040}
41
Reid Spencer33189e72004-09-13 22:38:11 +000042void Memory::ReleaseRWX(MemoryBlock& M) {
43 if (M.Address == 0 || M.Size == 0) return;
44 VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);
Reid Spencercbad7012004-09-11 04:59:30 +000045}