Added a mark-sweep garbage collector.
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 Address x = (Address)VirtualAlloc((LPVOID)address, amount, flags, bits);
75 DWORD err = GetLastError();
82 OS::MapAlignedMemory(size_t amount, size_t alignment, MapMode mode)
84 Address addr = MapMemory(amount, mode, OS::Prot_ReadWrite);
86 return AlignableMapping(NULL, alignment, 0);
88 if (!IsAligned(addr, alignment)) {
89 UnmapMemory(addr, amount);
91 // Try to grab the next available aligned address.
92 addr = AlignedBase(addr, alignment);
93 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
95 // Try to over-allocate to get the right region. This can race
96 // with other threads; we expect this to be rare. If this turns
97 // out to be a problem, the API is set up such that we can just
98 // return two pointers: the base, and the aligned address into
99 // the over-allocated region.
100 addr = MapMemory(amount + alignment, mode, OS::Prot_ReadWrite);
102 return AlignableMapping(NULL, alignment, 0);
103 UnmapMemory(addr, amount + alignment);
104 addr = AlignedBase(addr, alignment);
105 addr = MapMemoryAt(addr + alignment, amount, mode, OS::Prot_ReadWrite);
109 return AlignableMapping(addr, alignment, amount);
113 OS::UnmapAlignedMemory(Address address, size_t size)
115 OS::UnmapMemory(address, size);