Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 1 | //===- Interix/Memory.cpp - Interix Memory Implementation -------*- 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 provides the Interix specific implementation of various Memory |
| 11 | // management utilities |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | // Include the generic unix implementation |
| 16 | #include "../Unix/Memory.cpp" |
| 17 | #include "llvm/System/Process.h" |
Reid Spencer | 111a348 | 2004-09-18 19:34:09 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 19 | #include <sys/mman.h> |
| 20 | |
| 21 | namespace llvm { |
| 22 | using namespace sys; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | //=== WARNING: Implementation here must contain only Interix specific code |
| 26 | //=== and must not be generic UNIX code (see ../Unix/Memory.cpp) |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 29 | MemoryBlock Memory::AllocateRWX(unsigned NumBytes) { |
| 30 | if (NumBytes == 0) return MemoryBlock(); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 31 | |
| 32 | static const long pageSize = Process::GetPageSize(); |
| 33 | unsigned NumPages = (NumBytes+pageSize-1)/pageSize; |
Reid Spencer | 111a348 | 2004-09-18 19:34:09 +0000 | [diff] [blame] | 34 | |
| 35 | int fd = open("/dev/zero", O_RDWR); |
| 36 | if (fd == -1) { |
| 37 | throw std::string("Can't open /dev/zero device: ") + strerror(errno); |
| 38 | } |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 39 | |
| 40 | void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, |
Reid Spencer | 111a348 | 2004-09-18 19:34:09 +0000 | [diff] [blame] | 41 | MAP_SHARED, fd, 0); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 42 | if (pa == (void*)-1) { |
| 43 | throw std::string("Can't allocate RWX Memory: ") + strerror(errno); |
| 44 | } |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 45 | MemoryBlock result; |
| 46 | result.Address = pa; |
| 47 | result.Size = NumPages*pageSize; |
| 48 | return result; |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 51 | void Memory::ReleaseRWX(MemoryBlock& M) { |
| 52 | if (M.Address == 0 || M.Size == 0) return; |
| 53 | if (0 != munmap(M.Address, M.Size)) { |
Reid Spencer | 7144707 | 2004-09-11 20:18:08 +0000 | [diff] [blame] | 54 | throw std::string("Can't release RWX Memory: ") + strerror(errno); |
Reid Spencer | cbad701 | 2004-09-11 04:59:30 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |
| 60 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |