1 //===- Win32/Memory.cpp - Win32 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 Win32 specific implementation of various Memory
11 // management utilities
13 //===----------------------------------------------------------------------===//
15 #include <llvm/System/Process.h>
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only Win32 specific code.
23 //===----------------------------------------------------------------------===//
25 MemoryBlock Memory::AllocateRWX(unsigned NumBytes) {
26 if (NumBytes == 0) return MemoryBlock();
28 unsigned pageSize = Process::GetPageSize();
29 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
30 void *P = VirtualAlloc(0, NumPages*pageSize, MEM_COMMIT,
31 PAGE_EXECUTE_READWRITE);
33 throw std::string("Couldn't allocate ") + utostr(NumBytes) +
34 " bytes of executable memory!";
38 result.Size = NumBytes;
42 void Memory::ReleaseRWX(MemoryBlock& M) {
43 if (M.Address == 0 || M.Size == 0) return;
44 VirtualFree(M.Address, M.Size, MEM_DECOMMIT, PAGE_NOACCESS);