LLVM API Documentation

Win32/Memory.inc

Go to the documentation of this file.
00001 //===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides the Win32 specific implementation of various Memory
00011 // management utilities
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "Win32.h"
00016 #include "llvm/Support/DataTypes.h"
00017 #include "llvm/System/Process.h"
00018 
00019 namespace llvm {
00020 using namespace sys;
00021 
00022 //===----------------------------------------------------------------------===//
00023 //=== WARNING: Implementation here must contain only Win32 specific code 
00024 //===          and must not be UNIX code
00025 //===----------------------------------------------------------------------===//
00026 
00027 MemoryBlock Memory::AllocateRWX(size_t NumBytes,
00028                                 const MemoryBlock *NearBlock,
00029                                 std::string *ErrMsg) {
00030   if (NumBytes == 0) return MemoryBlock();
00031 
00032   static const size_t pageSize = Process::GetPageSize();
00033   size_t NumPages = (NumBytes+pageSize-1)/pageSize;
00034 
00035   //FIXME: support NearBlock if ever needed on Win64.
00036 
00037   void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
00038                   PAGE_EXECUTE_READWRITE);
00039   if (pa == NULL) {
00040     MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
00041     return MemoryBlock();
00042   }
00043 
00044   MemoryBlock result;
00045   result.Address = pa;
00046   result.Size = NumPages*pageSize;
00047   return result;
00048 }
00049 
00050 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
00051   if (M.Address == 0 || M.Size == 0) return false;
00052   if (!VirtualFree(M.Address, 0, MEM_RELEASE))
00053     return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
00054   return false;
00055 }
00056 
00057 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
00058   return true;
00059 }
00060 
00061 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
00062   return false;
00063 }
00064 
00065 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
00066   return true;
00067 }
00068 
00069 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
00070   return false;
00071 }
00072 
00073 }