blob: f7b05abe037f25d3a9cf15a1f111f78c94004d08 [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//
Reid Spencerb016a372004-09-15 05:49:50 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencercbad7012004-09-11 04:59:30 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
Reid Spencer33189e72004-09-13 22:38:11 +000026MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
27 if (NumBytes == 0) return MemoryBlock();
Reid Spencercbad7012004-09-11 04:59:30 +000028
Reid Spencerb016a372004-09-15 05:49:50 +000029 static const long pageSize = Process::GetPageSize();
Reid Spencercbad7012004-09-11 04:59:30 +000030 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
Reid Spencerb016a372004-09-15 05:49:50 +000031
32 void *pa = VirtualAlloc(NULL, NumPages*pageSize, MEM_COMMIT,
33 PAGE_EXECUTE_READWRITE);
34 if (pa == NULL) {
35 ThrowError("Can't allocate RWX Memory: ");
Reid Spencercbad7012004-09-11 04:59:30 +000036 }
Reid Spencerb016a372004-09-15 05:49:50 +000037
Reid Spencer33189e72004-09-13 22:38:11 +000038 MemoryBlock result;
Reid Spencerb016a372004-09-15 05:49:50 +000039 result.Address = pa;
40 result.Size = NumPages*pageSize;
Reid Spencer33189e72004-09-13 22:38:11 +000041 return result;
Reid Spencercbad7012004-09-11 04:59:30 +000042}
43
Reid Spencer33189e72004-09-13 22:38:11 +000044void Memory::ReleaseRWX(MemoryBlock& M) {
45 if (M.Address == 0 || M.Size == 0) return;
Reid Spencerb016a372004-09-15 05:49:50 +000046 if (!VirtualFree(M.Address, 0, MEM_RELEASE)) {
47 ThrowError("Can't release RWX Memory: ");
48 }
Reid Spencercbad7012004-09-11 04:59:30 +000049}
Reid Spencerb016a372004-09-15 05:49:50 +000050
51}
52
53// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab