Don't pull in environ, not always safe. Global variables are bad anyway.
[oota-llvm.git] / lib / System / Unix / Memory.inc
1 //===- 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"
15 #include "llvm/System/Process.h"
16
17 #ifdef HAVE_SYS_MMAN_H
18 #include <sys/mman.h>
19 #endif
20
21 namespace llvm {
22
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.
27 ///
28 MemoryBlock Memory::AllocateRWX(unsigned NumBytes, const MemoryBlock* NearBlock) {
29   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   ;
50
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,
54                     flags, fd, 0);
55   if (pa == MAP_FAILED) {
56     if (NearBlock) //Try again without a near hint
57       return AllocateRWX(NumBytes, 0);
58     else
59       ThrowErrno("Can't allocate RWX Memory");
60   }
61   MemoryBlock result;
62   result.Address = pa;
63   result.Size = NumPages*pageSize;
64   return result;
65 }
66
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");
71   }
72 }
73
74 }
75