Imported garbage collection allocator and converted Type to a gc object.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 // :TODO: move into utility-style include
20 #define WIN32_LEAN_AND_MEAN
27 ToProtBits(OS::Protection prot)
30 case OS::Prot_ReadWrite:
31 return PAGE_READWRITE;
32 case OS::Prot_ReadWriteExecute:
33 return PAGE_EXECUTE_READWRITE;
35 assert(prot == OS::Prot_ReadExecute);
36 return PAGE_EXECUTE_READ;
41 OS::MapMemory(size_t amount, MapMode mode, Protection prot_)
43 DWORD prot = ToProtBits(prot_);
44 DWORD type = MEM_RESERVE;
45 if (mode == Map_Commit)
48 return (Address)VirtualAlloc(NULL, amount, type, prot);
52 OS::CommitMemory(Address address, size_t amount, Protection prot_)
54 DWORD prot = ToProtBits(prot_);
55 return !!VirtualAlloc(address, amount, MEM_COMMIT, prot);
59 OS::UnmapMemory(Address address, size_t size)
61 VirtualFree(address, 0, MEM_RELEASE);
65 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
67 Address addr = MapMemory(amount, mode, OS::Prot_ReadWrite);
69 return AlignableMapping(NULL, alignment, 0);
71 if (!IsAligned(addr, alignment)) {
72 UnmapMemory(addr, amount);
74 // Try to grab the next available aligned address.
75 addr = AlignedBase(addr, alignment);
76 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
78 // Try to over-allocate to get the right region. This can race
79 // with other threads; we expect this to be rare. If this turns
80 // out to be a problem, the API is set up such that we can just
81 // return two pointers: the base, and the aligned address into
82 // the over-allocated region.
83 addr = MapMemory(amount + alignment, mode, OS::Prot_ReadWrite);
85 return AlignableMapping(NULL, alignment, 0);
86 UnmapMemory(addr, amount + alignment);
87 addr = AlignedBase(addr, alignment);
88 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
92 return AlignableMapping(addr, alignment, amount);
96 OS::MapMemoryAt(Address address, size_t amount, MapMode mode, Protection prot_)
98 DWORD bits = ToProtBits(prot_);
99 DWORD flags = MEM_RESERVE;
100 if (mode == Map_Commit)
103 Address x = (Address)VirtualAlloc((LPVOID)address, amount, flags, bits);
105 DWORD err = GetLastError();
112 OS::UnmapAlignedMemory(Address address, size_t size)
114 OS::UnmapMemory(address, size);