blob: 4475960e1176235b7aa41ce4c096e67e5b307791 [file] [log] [blame]
Reid Spencer41b21bf2004-09-11 04:57:25 +00001//===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines some functions for various memory management utilities.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Unix.h"
Reid Spencer07c00d32004-12-27 06:17:03 +000015#include "llvm/System/Process.h"
16
17#ifdef HAVE_SYS_MMAN_H
18#include <sys/mman.h>
19#endif
Reid Spencer41b21bf2004-09-11 04:57:25 +000020
21namespace llvm {
22
Reid Spencer07c00d32004-12-27 06:17:03 +000023/// 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.
27///
Andrew Lenhartha00269b2005-07-29 23:40:16 +000028MemoryBlock Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock) {
Reid Spencer07c00d32004-12-27 06:17:03 +000029 if (NumBytes == 0) return MemoryBlock();
30
31 long pageSize = Process::GetPageSize();
32 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
33
34 int fd = -1;
35#ifdef NEED_DEV_ZERO_FOR_MMAP
36 static int zero_fd = open("/dev/zero", O_RDWR);
37 if (zero_fd == -1) {
38 ThrowErrno("Can't open /dev/zero device");
39 }
40 fd = zero_fd;
41#endif
42
43 int flags = MAP_PRIVATE |
44#ifdef HAVE_MMAP_ANONYMOUS
45 MAP_ANONYMOUS
46#else
47 MAP_ANON
48#endif
49 ;
Andrew Lenhartha00269b2005-07-29 23:40:16 +000050
51 void* start = NearBlock ? (unsigned char*) NearBlock->base() + NearBlock->size() : 0;
52
53 void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
Reid Spencer07c00d32004-12-27 06:17:03 +000054 flags, fd, 0);
55 if (pa == MAP_FAILED) {
Andrew Lenhartha00269b2005-07-29 23:40:16 +000056 if (NearBlock) //Try again without a near hint
57 return AllocateRWX(NumBytes, 0);
58 else
59 ThrowErrno("Can't allocate RWX Memory");
Reid Spencer07c00d32004-12-27 06:17:03 +000060 }
61 MemoryBlock result;
62 result.Address = pa;
63 result.Size = NumPages*pageSize;
64 return result;
65}
66
67void 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");
71 }
72}
73
Reid Spencer41b21bf2004-09-11 04:57:25 +000074}
75