LLVM API Documentation
00001 //===- Cygwin/Memory.cpp - Cygwin Memory Implementation ---------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by Reid Spencer 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 Cygwin specific implementation of various Memory 00011 // management utilities 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 // Include the generic unix implementation 00016 #include "../Unix/Memory.cpp" 00017 #include "llvm/System/Process.h" 00018 #include <sys/types.h> 00019 #include <sys/mman.h> 00020 00021 namespace llvm { 00022 using namespace sys; 00023 00024 //===----------------------------------------------------------------------===// 00025 //=== WARNING: Implementation here must contain only Cygwin specific code 00026 //=== and must not be generic UNIX code (see ../Unix/Memory.cpp) 00027 //===----------------------------------------------------------------------===// 00028 00029 MemoryBlock Memory::AllocateRWX(unsigned NumBytes) { 00030 if (NumBytes == 0) return MemoryBlock(); 00031 00032 static const long pageSize = Process::GetPageSize(); 00033 unsigned NumPages = (NumBytes+pageSize-1)/pageSize; 00034 00035 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, 00036 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 00037 if (pa == (void*)-1) { 00038 throw std::string("Can't allocate RWX Memory: ") + strerror(errno); 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 (0 != munmap(M.Address, M.Size)) { 00049 throw std::string("Can't release RWX Memory: ") + strerror(errno); 00050 } 00051 } 00052 00053 } 00054 00055 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab