2004-09-11 04:59:30 +00:00
|
|
|
//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-09-15 05:49:50 +00:00
|
|
|
// This file was developed by Jeff Cohen and is distributed under the
|
2004-09-11 04:59:30 +00: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 05:49:50 +00:00
|
|
|
#include "Win32.h"
|
|
|
|
#include "llvm/System/Process.h"
|
2004-09-11 04:59:30 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-15 05:49:50 +00:00
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
|
|
|
//=== and must not be UNIX code
|
2004-09-11 04:59:30 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-07 17:32:37 +00:00
|
|
|
MemoryBlock Memory::AllocateRWX(unsigned NumBytes,
|
|
|
|
const MemoryBlock *NearBlock,
|
|
|
|
std::string *ErrMsg) {
|
2004-09-13 22:38:11 +00:00
|
|
|
if (NumBytes == 0) return MemoryBlock();
|
2004-09-11 04:59:30 +00:00
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
static const long pageSize = Process::GetPageSize();
|
2004-09-11 04:59:30 +00:00
|
|
|
unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
|
2004-09-15 05:49:50 +00:00
|
|
|
|
2005-07-29 23:40:16 +00:00
|
|
|
//FIXME: support NearBlock if ever needed on Win64.
|
|
|
|
|
2004-09-15 05:49:50 +00:00
|
|
|
void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE);
|
|
|
|
if (pa == NULL) {
|
2006-08-25 21:37:17 +00:00
|
|
|
MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
|
2006-07-07 17:32:37 +00:00
|
|
|
return MemoryBlock();
|
2004-09-11 04:59:30 +00:00
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
|
2004-09-13 22:38:11 +00:00
|
|
|
MemoryBlock result;
|
2004-09-15 05:49:50 +00:00
|
|
|
result.Address = pa;
|
|
|
|
result.Size = NumPages*pageSize;
|
2004-09-13 22:38:11 +00:00
|
|
|
return result;
|
2004-09-11 04:59:30 +00:00
|
|
|
}
|
|
|
|
|
2006-07-07 17:32:37 +00:00
|
|
|
bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
|
2006-07-26 20:37:11 +00:00
|
|
|
if (M.Address == 0 || M.Size == 0) return false;
|
2006-07-07 17:32:37 +00:00
|
|
|
if (!VirtualFree(M.Address, 0, MEM_RELEASE))
|
2006-08-25 21:37:17 +00:00
|
|
|
return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
|
2006-07-26 20:37:11 +00:00
|
|
|
return false;
|
2004-09-11 04:59:30 +00:00
|
|
|
}
|
2004-09-15 05:49:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|