2004-09-11 12:59:30 +08:00
|
|
|
//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-09-15 13:49:50 +08:00
|
|
|
// This file was developed by Jeff Cohen and is distributed under the
|
2004-09-11 12:59:30 +08:00
|
|
|
// University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-07-08 01:32:37 +08:00
|
|
|
GetError("Can't allocate RWX Memory: ", ErrMsg);
|
|
|
|
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))
|
|
|
|
return GetError("Can't release RWX Memory: ", ErrMsg);
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|