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 void* Memory::AllocateRWX(Memory&M, unsigned NumBytes) {
26 if (NumBytes == 0) return 0;
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!";
37 M.AllocSize = NumBytes;
41 void Memory::ReleaseRWX(Memory& M) {
42 if (M.Address == 0 ) return;
43 VirtualFree(M.Address, M.AllocSize, MEM_DECOMMIT, PAGE_NOACCESS);