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, const MemoryBlock* NearBlock) {
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
51 void* start = NearBlock ? (unsigned char*) NearBlock->base() + NearBlock->size() : 0;
53 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
55 if (pa == MAP_FAILED) {
56 if (NearBlock) //Try again without a near hint
57 return AllocateRWX(NumBytes, 0);
59 ThrowErrno("Can't allocate RWX Memory");
63 result.Size = NumPages*pageSize;
67 void Memory::ReleaseRWX(MemoryBlock& M) {
68 if (M.Address == 0 || M.Size == 0) return;
69 if (0 != ::munmap(M.Address, M.Size)) {
70 ThrowErrno("Can't release RWX Memory");