2 * Contains OS-level routines needed by the garbage collector.
4 * Copyright: Copyright Digital Mars 2005 - 2013.
5 * License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 * Authors: Walter Bright, David Friedman, Sean Kelly, Leandro Lucarella
9 /* Copyright Digital Mars 2005 - 2013.
10 * Distributed under the Boost Software License, Version 1.0.
11 * (See accompanying file LICENSE or copy at
12 * http://www.boost.org/LICENSE_1_0.txt)
19 import core.sys.windows.winbase : GetCurrentThreadId, VirtualAlloc, VirtualFree;
20 import core.sys.windows.winnt : MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE;
24 pthread_t pthread_self() nothrow
26 return cast(pthread_t) GetCurrentThreadId();
29 //version = GC_Use_Alloc_Win32;
39 else version (WatchOS)
42 import core.sys.posix.sys.mman;
43 version (FreeBSD) import core.sys.freebsd.sys.mman : MAP_ANON;
44 version (DragonFlyBSD) import core.sys.dragonflybsd.sys.mman : MAP_ANON;
45 version (NetBSD) import core.sys.netbsd.sys.mman : MAP_ANON;
46 version (OpenBSD) import core.sys.openbsd.sys.mman : MAP_ANON;
47 version (CRuntime_Glibc) import core.sys.linux.sys.mman : MAP_ANON;
48 version (Darwin) import core.sys.darwin.sys.mman : MAP_ANON;
49 version (CRuntime_UClibc) import core.sys.linux.sys.mman : MAP_ANON;
50 import core.stdc.stdlib;
52 //version = GC_Use_Alloc_MMap;
56 import core.stdc.stdlib;
58 //version = GC_Use_Alloc_Malloc;
62 static if (is(typeof(VirtualAlloc)))
63 version = GC_Use_Alloc_Win32;
64 else static if (is(typeof(mmap)))
65 version = GC_Use_Alloc_MMap;
66 else static if (is(typeof(valloc)))
67 version = GC_Use_Alloc_Valloc;
68 else static if (is(typeof(malloc)))
69 version = GC_Use_Alloc_Malloc;
70 else static assert(false, "No supported allocation methods available.");
73 static if (is(typeof(VirtualAlloc))) // version (GC_Use_Alloc_Win32)
78 void *os_mem_map(size_t nbytes) nothrow
80 return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT,
86 * Unmap memory allocated with os_mem_map().
91 int os_mem_unmap(void *base, size_t nbytes) nothrow
93 return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
96 else static if (is(typeof(mmap))) // else version (GC_Use_Alloc_MMap)
98 void *os_mem_map(size_t nbytes) nothrow
101 p = mmap(null, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
102 return (p == MAP_FAILED) ? null : p;
106 int os_mem_unmap(void *base, size_t nbytes) nothrow
108 return munmap(base, nbytes);
111 else static if (is(typeof(valloc))) // else version (GC_Use_Alloc_Valloc)
113 void *os_mem_map(size_t nbytes) nothrow
115 return valloc(nbytes);
119 int os_mem_unmap(void *base, size_t nbytes) nothrow
125 else static if (is(typeof(malloc))) // else version (GC_Use_Alloc_Malloc)
127 // NOTE: This assumes malloc granularity is at least (void*).sizeof. If
128 // (req_size + PAGESIZE) is allocated, and the pointer is rounded up
129 // to PAGESIZE alignment, there will be space for a void* at the end
130 // after PAGESIZE bytes used by the GC.
136 const size_t PAGE_MASK = PAGESIZE - 1;
139 void *os_mem_map(size_t nbytes) nothrow
141 p = cast(byte *) malloc(nbytes + PAGESIZE);
142 q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
143 * cast(void**)(q + nbytes) = p;
148 int os_mem_unmap(void *base, size_t nbytes) nothrow
150 free( *cast(void**)( cast(byte*) base + nbytes ) );
156 static assert(false, "No supported allocation methods available.");
160 Check for any kind of memory pressure.
163 mapped = the amount of memory mapped by the GC in bytes
165 true if memory is scarce
167 // TOOD: get virtual mem sizes and current usage from OS
168 // TODO: compare current RSS and avail. physical memory
171 bool isLowOnMem(size_t mapped) nothrow @nogc
177 import core.sys.windows.winbase : GlobalMemoryStatus, MEMORYSTATUS;
179 GlobalMemoryStatus(&stat);
180 // Less than 5 % of virtual address space available
181 return stat.dwAvailVirtual < stat.dwTotalVirtual / 20;
185 else version (Darwin)
187 bool isLowOnMem(size_t mapped) nothrow @nogc
194 // 80 % of available 4GB is used for GC (excluding malloc and mmap)
195 enum size_t limit = 4UL * GB * 8 / 10;
196 return mapped > limit;
202 bool isLowOnMem(size_t mapped) nothrow @nogc
209 // be conservative and assume 3GB
210 enum size_t limit = 3UL * GB * 8 / 10;
211 return mapped > limit;