1 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of various Memory
11 // management utilities
13 //===----------------------------------------------------------------------===//
16 #include "llvm/System/Process.h"
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code
23 //=== and must not be UNIX code
24 //===----------------------------------------------------------------------===//
26 MemoryBlock Memory::AllocateRWX(unsigned NumBytes,
27 const MemoryBlock *NearBlock,
28 std::string *ErrMsg) {
29 if (NumBytes == 0) return MemoryBlock();
31 static const long pageSize = Process::GetPageSize();
32 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
34 //FIXME: support NearBlock if ever needed on Win64.
36 void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
37 PAGE_EXECUTE_READWRITE);
39 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
45 result.Size = NumPages*pageSize;
49 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
50 if (M.Address == 0 || M.Size == 0) return false;
51 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
52 return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
56 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
60 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
64 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
68 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {