LLVM API Documentation

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

FreeBSD/Memory.cpp

Go to the documentation of this file.
00001 //===- FreeBSD/Memory.cpp - FreeBSD 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 FreeBSD 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/mman.h>
00019 
00020 namespace llvm {
00021 using namespace sys;
00022 
00023 //===----------------------------------------------------------------------===//
00024 //=== WARNING: Implementation here must contain only FreeBSD specific code 
00025 //===          and must not be generic UNIX code (see ../Unix/Memory.cpp)
00026 //===----------------------------------------------------------------------===//
00027 
00028 MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
00029   if (NumBytes == 0) return MemoryBlock();
00030 
00031   static const long pageSize = Process::GetPageSize();
00032   unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
00033 
00034   void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
00035                   MAP_PRIVATE|MAP_ANON|MAP_NOCORE, -1, 0);
00036   if (pa == (void*)-1) {
00037     throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
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 (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