diff -r 0c3bcd50e361 -r a350c2cc736a jitcraft/Platform-Windows.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/Platform-Windows.cpp Fri Jun 29 19:35:17 2012 -0700 @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ + +// :TODO: move into utility-style include +#define WIN32_LEAN_AND_MEAN +#include +#include "Platform.h" + +using namespace jc; + +static inline DWORD +ToProtBits(OS::Protection prot) +{ + switch (prot) { + case OS::Prot_ReadWrite: + return PAGE_READWRITE; + case OS::Prot_ReadWriteExecute: + return PAGE_EXECUTE_READWRITE; + default: + assert(prot == OS::Prot_ReadExecute); + return PAGE_EXECUTE_READ; + } +} + +Address +OS::MapMemory(size_t amount, MapMode mode, Protection prot_) +{ + DWORD prot = ToProtBits(prot_); + DWORD type = MEM_RESERVE; + if (mode == Map_Commit) + type |= MEM_COMMIT; + + return (Address)VirtualAlloc(NULL, amount, type, prot); +} + +bool +OS::CommitMemory(Address address, size_t amount, Protection prot_) +{ + DWORD prot = ToProtBits(prot_); + return !!VirtualAlloc(address, amount, MEM_COMMIT, prot); +} + +void +OS::UnmapMemory(Address address, size_t size) +{ + VirtualFree(address, 0, MEM_RELEASE); +} + +OS::AlignableMapping +OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode) +{ + Address addr = MapMemory(amount, mode, OS::Prot_ReadWrite); + if (!addr) + return AlignableMapping(NULL, alignment, 0); + + if (!IsAligned(addr, alignment)) { + UnmapMemory(addr, amount); + + // Try to grab the next available aligned address. + addr = AlignedBase(addr, alignment); + addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite); + while (!addr) { + // Try to over-allocate to get the right region. This can race + // with other threads; we expect this to be rare. If this turns + // out to be a problem, the API is set up such that we can just + // return two pointers: the base, and the aligned address into + // the over-allocated region. + addr = MapMemory(amount + alignment, mode, OS::Prot_ReadWrite); + if (!addr) + return AlignableMapping(NULL, alignment, 0); + UnmapMemory(addr, amount + alignment); + addr = AlignedBase(addr, alignment); + addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite); + } + } + + return AlignableMapping(addr, alignment, amount); +} + +Address +OS::MapMemoryAt(Address address, size_t amount, MapMode mode, Protection prot_) +{ + DWORD bits = ToProtBits(prot_); + DWORD flags = MEM_RESERVE; + if (mode == Map_Commit) + flags |= MEM_COMMIT; + + Address x = (Address)VirtualAlloc((LPVOID)address, amount, flags, bits); + if (!x) { + DWORD err = GetLastError(); + return x; + } + return x; +} + +void +OS::UnmapAlignedMemory(Address address, size_t size) +{ + OS::UnmapMemory(address, size); +}