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 was developed by Jeff Cohen and is distributed under the 
00006 // University of Illinois Open Source 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/System/Process.h"
00017 
00018 namespace llvm {
00019 using namespace sys;
00020 
00021 //===----------------------------------------------------------------------===//
00022 //=== WARNING: Implementation here must contain only Win32 specific code 
00023 //===          and must not be UNIX code
00024 //===----------------------------------------------------------------------===//
00025 
00026 MemoryBlock Memory::AllocateRWX(unsigned NumBytes,
00027                                 const MemoryBlock *NearBlock,
00028                                 std::string *ErrMsg) {
00029   if (NumBytes == 0) return MemoryBlock();
00030 
00031   static const long pageSize = Process::GetPageSize();
00032   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
00033 
00034   //FIXME: support NearBlock if ever needed on Win64.
00035 
00036   void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
00037                   PAGE_EXECUTE_READWRITE);
00038   if (pa == NULL) {
00039     GetError("Can't allocate RWX Memory: ", ErrMsg);
00040     return MemoryBlock();
00041   }
00042 
00043   MemoryBlock result;
00044   result.Address = pa;
00045   result.Size = NumPages*pageSize;
00046   return result;
00047 }
00048 
00049 bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
00050   if (M.Address == 0 || M.Size == 0) return false;
00051   if (!VirtualFree(M.Address, 0, MEM_RELEASE))
00052     return GetError("Can't release RWX Memory: ", ErrMsg);
00053   return false;
00054 }
00055 
00056 }
00057