LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Win32/Memory.cpp

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