2004-09-11 12:59:30 +08:00
|
|
|
//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-11 12:59:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides the Win32 specific implementation of various Memory
|
|
|
|
// management utilities
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
#include "Win32.h"
|
|
|
|
#include "llvm/System/Process.h"
|
2004-09-11 12:59:30 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-15 13:49:50 +08:00
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
|
|
|
//=== and must not be UNIX code
|
2004-09-11 12:59:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-08 01:32:37 +08:00
|
|
|
MemoryBlock Memory::AllocateRWX(unsigned NumBytes,
|
|
|
|
const MemoryBlock *NearBlock,
|
|
|
|
std::string *ErrMsg) {
|
2004-09-14 06:38:11 +08:00
|
|
|
if (NumBytes == 0) return MemoryBlock();
|
2004-09-11 12:59:30 +08:00
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
static const long pageSize = Process::GetPageSize();
|
2004-09-11 12:59:30 +08:00
|
|
|
unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2005-07-30 07:40:16 +08:00
|
|
|
//FIXME: support NearBlock if ever needed on Win64.
|
|
|
|
|
2004-09-15 13:49:50 +08:00
|
|
|
void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE);
|
|
|
|
if (pa == NULL) {
|
2006-08-26 05:37:17 +08:00
|
|
|
MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
|
2006-07-08 01:32:37 +08:00
|
|
|
return MemoryBlock();
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2004-09-14 06:38:11 +08:00
|
|
|
MemoryBlock result;
|
2004-09-15 13:49:50 +08:00
|
|
|
result.Address = pa;
|
|
|
|
result.Size = NumPages*pageSize;
|
2004-09-14 06:38:11 +08:00
|
|
|
return result;
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
|
|
|
|
2006-07-08 01:32:37 +08:00
|
|
|
bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
|
2006-07-27 04:37:11 +08:00
|
|
|
if (M.Address == 0 || M.Size == 0) return false;
|
2006-07-08 01:32:37 +08:00
|
|
|
if (!VirtualFree(M.Address, 0, MEM_RELEASE))
|
2006-08-26 05:37:17 +08:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
|
2006-07-27 04:37:11 +08:00
|
|
|
return false;
|
2004-09-11 12:59:30 +08:00
|
|
|
}
|
2004-09-15 13:49:50 +08:00
|
|
|
|
2008-10-21 05:39:23 +08:00
|
|
|
bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
|
2008-10-04 16:15:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-21 05:39:23 +08:00
|
|
|
bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
|
2008-10-04 16:15:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-21 05:39:23 +08:00
|
|
|
bool Memory::setRangeWritable(const void *Addr, size_t Size) {
|
|
|
|
return true;
|
2004-09-15 13:49:50 +08:00
|
|
|
}
|
|
|
|
|
2008-10-21 05:39:23 +08:00
|
|
|
bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|