1 //===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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
21 /// AllocateRWXMemory - Allocate a slab of memory with read/write/execute
22 /// permissions. This is typically used for JIT applications where we want
23 /// to emit code to the memory then jump to it. Getting this type of memory
24 /// is very OS specific.
26 llvm::sys::MemoryBlock
27 llvm::sys::Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock,
28 std::string *ErrMsg) {
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 MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
44 int flags = MAP_PRIVATE |
45 #ifdef HAVE_MMAP_ANONYMOUS
52 void* start = NearBlock ? (unsigned char*)NearBlock->base() +
53 NearBlock->size() : 0;
55 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
57 if (pa == MAP_FAILED) {
58 if (NearBlock) //Try again without a near hint
59 return AllocateRWX(NumBytes, 0);
61 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
66 result.Size = NumPages*pageSize;
70 /// AllocateRWMemory - Allocate a slab of memory with read/write permissions.
71 /// This memory needs to have executable permissions set before it can be used
72 /// to execute JIT'ed code.
73 llvm::sys::MemoryBlock
74 llvm::sys::Memory::AllocateRW(unsigned NumBytes, const MemoryBlock* NearBlock,
75 std::string *ErrMsg) {
76 if (NumBytes == 0) return MemoryBlock();
78 long pageSize = Process::GetPageSize();
79 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
82 #ifdef NEED_DEV_ZERO_FOR_MMAP
83 static int zero_fd = open("/dev/zero", O_RDWR);
85 MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
91 int flags = MAP_PRIVATE |
92 #ifdef HAVE_MMAP_ANONYMOUS
99 void* start = NearBlock ? (unsigned char*)NearBlock->base() +
100 NearBlock->size() : 0;
102 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE,
104 if (pa == MAP_FAILED) {
105 if (NearBlock) //Try again without a near hint
106 return AllocateRWX(NumBytes, 0);
108 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
109 return MemoryBlock();
113 result.Size = NumPages*pageSize;
117 bool llvm::sys::Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
118 if (M.Address == 0 || M.Size == 0) return false;
119 if (0 != ::munmap(M.Address, M.Size))
120 return MakeErrMsg(ErrMsg, "Can't release RWX Memory");