1 //===- Cygwin/Memory.cpp - Cygwin Memory Implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Cygwin specific implementation of various Memory
11 // management utilities
13 //===----------------------------------------------------------------------===//
15 // Include the generic unix implementation
16 #include "../Unix/Memory.cpp"
17 #include "llvm/System/Process.h"
18 #include <sys/types.h>
24 //===----------------------------------------------------------------------===//
25 //=== WARNING: Implementation here must contain only Cygwin specific code
26 //=== and must not be generic UNIX code (see ../Unix/Memory.cpp)
27 //===----------------------------------------------------------------------===//
29 void* Memory::AllocateRWX(Memory& M, unsigned NumBytes) {
30 if (NumBytes == 0) return 0;
32 static const long pageSize = Process::GetPageSize();
33 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
35 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
36 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
37 if (pa == (void*)-1) {
38 throw std::string("Can't allocate RWX Memory: ") + strerror(errno);
41 M.AllocSize = NumPages*pageSize;
45 void Memory::ReleaseRWX(Memory& M) {
46 if (M.Address == 0 || M.AllocSize == 0) return;
47 if (0 != munmap(M.Address, M.AllocSize)) {
48 throw std::string("Can't release RWX Memory: ") + sterror(errno);
54 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab