blob: 94e5893932893e4eb1f51f30b71d11c23b90788f [file] [log] [blame]
Reid Spencercbad7012004-09-11 04:59:30 +00001//===- 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"
18#include <sys/mman.h>
19
20namespace llvm {
21using namespace sys;
22
23//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only Interix specific code
25//=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
26//===----------------------------------------------------------------------===//
27
Reid Spencer33189e72004-09-13 22:38:11 +000028MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
29 if (NumBytes == 0) return MemoryBlock();
Reid Spencercbad7012004-09-11 04:59:30 +000030
31 static const long pageSize = Process::GetPageSize();
32 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
33
34 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
35 MAP_PRIVATE|MAP_ANON|MAP_NOCORE, -1, 0);
36 if (pa == (void*)-1) {
37 throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
38 }
Reid Spencer33189e72004-09-13 22:38:11 +000039 MemoryBlock result;
40 result.Address = pa;
41 result.Size = NumPages*pageSize;
42 return result;
Reid Spencercbad7012004-09-11 04:59:30 +000043}
44
Reid Spencer33189e72004-09-13 22:38:11 +000045void Memory::ReleaseRWX(MemoryBlock& M) {
46 if (M.Address == 0 || M.Size == 0) return;
47 if (0 != munmap(M.Address, M.Size)) {
Reid Spencer71447072004-09-11 20:18:08 +000048 throw std::string("Can't release RWX Memory: ") + strerror(errno);
Reid Spencercbad7012004-09-11 04:59:30 +000049 }
50}
51
52}
53
54// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab