1 //===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- 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 defines some functions for various memory management utilities.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/System/Process.h"
17 #ifdef HAVE_SYS_MMAN_H
23 /// AllocateRWXMemory - Allocate a slab of memory with read/write/execute
24 /// permissions. This is typically used for JIT applications where we want
25 /// to emit code to the memory then jump to it. Getting this type of memory
26 /// is very OS specific.
28 MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
29 if (NumBytes == 0) return MemoryBlock();
31 long pageSize = Process::GetPageSize();
32 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
35 #ifdef NEED_DEV_ZERO_FOR_MMAP
36 static int zero_fd = open("/dev/zero", O_RDWR);
38 ThrowErrno("Can't open /dev/zero device");
43 int flags = MAP_PRIVATE |
44 #ifdef HAVE_MMAP_ANONYMOUS
50 void *pa = ::mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
52 if (pa == MAP_FAILED) {
53 ThrowErrno("Can't allocate RWX Memory");
57 result.Size = NumPages*pageSize;
61 void Memory::ReleaseRWX(MemoryBlock& M) {
62 if (M.Address == 0 || M.Size == 0) return;
63 if (0 != ::munmap(M.Address, M.Size)) {
64 ThrowErrno("Can't release RWX Memory");
70 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab