LLVM API Documentation

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, const MemoryBlock* NearBlock) {
00027   if (NumBytes == 0) return MemoryBlock();
00028 
00029   static const long pageSize = Process::GetPageSize();
00030   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
00031 
00032   //FIXME: support NearBlock if ever needed on Win64.
00033 
00034   void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
00035                   PAGE_EXECUTE_READWRITE);
00036   if (pa == NULL) {
00037     ThrowError("Can't allocate RWX Memory: ");
00038   }
00039 
00040   MemoryBlock result;
00041   result.Address = pa;
00042   result.Size = NumPages*pageSize;
00043   return result;
00044 }
00045 
00046 void Memory::ReleaseRWX(MemoryBlock& M) {
00047   if (M.Address == 0 || M.Size == 0) return;
00048   if (!VirtualFree(M.Address, 0, MEM_RELEASE)) {
00049     ThrowError("Can't release RWX Memory: ");
00050   }
00051 }
00052 
00053 }
00054