blob: eed2b100e6d805db961d93c1b8d7f081837d2de6 [file] [log] [blame]
Reid Spencercbad7012004-09-11 04:59:30 +00001//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencercbad7012004-09-11 04:59:30 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of various Memory
11// management utilities
12//
13//===----------------------------------------------------------------------===//
14
Reid Spencerb016a372004-09-15 05:49:50 +000015#include "Win32.h"
Reid Spencerb016a372004-09-15 05:49:50 +000016#include "llvm/System/Process.h"
Reid Spencercbad7012004-09-11 04:59:30 +000017
18namespace llvm {
19using namespace sys;
20
21//===----------------------------------------------------------------------===//
Reid Spencerb016a372004-09-15 05:49:50 +000022//=== WARNING: Implementation here must contain only Win32 specific code
23//=== and must not be UNIX code
Reid Spencercbad7012004-09-11 04:59:30 +000024//===----------------------------------------------------------------------===//
25
Chris Lattnerbed22d82006-07-07 17:32:37 +000026MemoryBlock Memory::AllocateRWX(unsigned NumBytes,
27 const MemoryBlock *NearBlock,
28 std::string *ErrMsg) {
Reid Spencer33189e72004-09-13 22:38:11 +000029 if (NumBytes == 0) return MemoryBlock();
Reid Spencercbad7012004-09-11 04:59:30 +000030
Reid Spencerb016a372004-09-15 05:49:50 +000031 static const long pageSize = Process::GetPageSize();
Reid Spencercbad7012004-09-11 04:59:30 +000032 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
Reid Spencerb016a372004-09-15 05:49:50 +000033
Andrew Lenhartha00269b2005-07-29 23:40:16 +000034 //FIXME: support NearBlock if ever needed on Win64.
35
Reid Spencerb016a372004-09-15 05:49:50 +000036 void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
37 PAGE_EXECUTE_READWRITE);
38 if (pa == NULL) {
Reid Spencer05545752006-08-25 21:37:17 +000039 MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
Chris Lattnerbed22d82006-07-07 17:32:37 +000040 return MemoryBlock();
Reid Spencercbad7012004-09-11 04:59:30 +000041 }
Reid Spencerb016a372004-09-15 05:49:50 +000042
Reid Spencer33189e72004-09-13 22:38:11 +000043 MemoryBlock result;
Reid Spencerb016a372004-09-15 05:49:50 +000044 result.Address = pa;
45 result.Size = NumPages*pageSize;
Reid Spencer33189e72004-09-13 22:38:11 +000046 return result;
Reid Spencercbad7012004-09-11 04:59:30 +000047}
48
Chris Lattnerbed22d82006-07-07 17:32:37 +000049bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
Chris Lattnerfab30f22006-07-26 20:37:11 +000050 if (M.Address == 0 || M.Size == 0) return false;
Chris Lattnerbed22d82006-07-07 17:32:37 +000051 if (!VirtualFree(M.Address, 0, MEM_RELEASE))
Reid Spencer05545752006-08-25 21:37:17 +000052 return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
Chris Lattnerfab30f22006-07-26 20:37:11 +000053 return false;
Reid Spencercbad7012004-09-11 04:59:30 +000054}
Reid Spencerb016a372004-09-15 05:49:50 +000055
56}
57