1 //===- FreeBSD/Memory.cpp - FreeBSD Memory Implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the FreeBSD specific implementation of various Memory
11 // management utilities
13 //===----------------------------------------------------------------------===//
15 // Include the generic unix implementation
16 #include "../Unix/Memory.cpp"
17 #include "llvm/System/Process.h"
23 //===----------------------------------------------------------------------===//
24 //=== WARNING: Implementation here must contain only FreeBSD specific code
25 //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
26 //===----------------------------------------------------------------------===//
28 MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
29 if (NumBytes == 0) return MemoryBlock();
31 static const long pageSize = Process::GetPageSize();
32 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
34 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
35 MAP_PRIVATE|MAP_ANON|MAP_NOCORE, -1, 0);
36 if (pa == (void*)-1) {
37 throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
42 result.Size = NumPages*pageSize;
46 void Memory::ReleaseRWX(Memory& M) {
47 if (M.Address == 0 || M.Size == 0) return;
48 if (0 != munmap(M.Address, M.Size)) {
49 throw std::string("Can't release RWX Memory: ") + strerror(errno);
55 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab