LLVM API Documentation

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

Interix/Memory.cpp

Go to the documentation of this file.
00001 //===- Interix/Memory.cpp - Interix 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 Interix 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 <fcntl.h>
00019 #include <sys/mman.h>
00020 
00021 namespace llvm {
00022 using namespace sys;
00023 
00024 //===----------------------------------------------------------------------===//
00025 //=== WARNING: Implementation here must contain only Interix 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   int fd = open("/dev/zero", O_RDWR);
00036   if (fd == -1) {
00037     throw std::string("Can't open /dev/zero device: ") + strerror(errno);
00038   }
00039 
00040   void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
00041                   MAP_SHARED, fd, 0);
00042   if (pa == (void*)-1) {
00043     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
00044   }
00045   MemoryBlock result;
00046   result.Address = pa;
00047   result.Size = NumPages*pageSize;
00048   return result;
00049 }
00050 
00051 void Memory::ReleaseRWX(MemoryBlock& M) {
00052   if (M.Address == 0 || M.Size == 0) return;
00053   if (0 != munmap(M.Address, M.Size)) {
00054     throw std::string("Can't release RWX Memory: ") + strerror(errno);
00055   }
00056 }
00057 
00058 }
00059 
00060 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab