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