Enable dynamic array declarations.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * JITCraft. If not, see http://www.gnu.org/licenses/.
20 // :TODO: move into utility-style include
21 #define WIN32_LEAN_AND_MEAN
28 ToProtBits(OS::Protection prot)
31 case OS::Prot_ReadWrite:
32 return PAGE_READWRITE;
33 case OS::Prot_ReadWriteExecute:
34 return PAGE_EXECUTE_READWRITE;
36 assert(prot == OS::Prot_ReadExecute);
37 return PAGE_EXECUTE_READ;
42 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
44 DWORD prot = ToProtBits(prot_);
45 DWORD type = MEM_RESERVE;
46 if (mode == Map_Commit)
49 return (Address)VirtualAlloc(NULL, amount, type, prot);
53 OS::CommitMemory(Address address, size_t amount, Protection prot_)
55 DWORD prot = ToProtBits(prot_);
56 return !!VirtualAlloc(address, amount, MEM_COMMIT, prot);
60 OS::UnmapMemory(Address address, size_t size)
62 VirtualFree(address, 0, MEM_RELEASE);
66 MapMemoryAt(Address address, size_t amount, OS::MapMode mode, OS::Protection prot_)
68 DWORD bits = ToProtBits(prot_);
69 DWORD flags = MEM_RESERVE;
70 if (mode == OS::Map_Commit)
73 return (Address)VirtualAlloc((LPVOID)address, amount, flags, bits);
77 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
79 Address addr = MapMemory(amount, mode, OS::Prot_ReadWrite);
81 return AlignableMapping(NULL, alignment, 0);
83 if (!IsAligned(addr, alignment)) {
84 UnmapMemory(addr, amount);
86 // Try to grab the next available aligned address.
87 addr = AlignedBase(addr, alignment);
88 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
90 // Try to over-allocate to get the right region. This can race
91 // with other threads; we expect this to be rare. If this turns
92 // out to be a problem, the API is set up such that we can just
93 // return two pointers: the base, and the aligned address into
94 // the over-allocated region.
95 addr = MapMemory(amount + alignment, mode, OS::Prot_ReadWrite);
97 return AlignableMapping(NULL, alignment, 0);
98 UnmapMemory(addr, amount + alignment);
99 addr = AlignedBase(addr, alignment);
100 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
104 return AlignableMapping(addr, alignment, amount);
108 OS::UnmapAlignedMemory(Address address, size_t size)
110 OS::UnmapMemory(address, size);