1 /**
2  * Contains OS-level routines needed by the garbage collector.
3  *
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
7  */
8 
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)
13  */
14 module gc.os;
15 
16 
version(Windows)17 version (Windows)
18 {
19     import core.sys.windows.winbase : GetCurrentThreadId, VirtualAlloc, VirtualFree;
20     import core.sys.windows.winnt : MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE;
21 
22     alias int pthread_t;
23 
24     pthread_t pthread_self() nothrow
25     {
26         return cast(pthread_t) GetCurrentThreadId();
27     }
28 
29     //version = GC_Use_Alloc_Win32;
30 }
version(Posix)31 else version (Posix)
32 {
33     version (OSX)
34         version = Darwin;
35     else version (iOS)
36         version = Darwin;
37     else version (TVOS)
38         version = Darwin;
39     else version (WatchOS)
40         version = Darwin;
41 
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 (CRuntime_Glibc) import core.sys.linux.sys.mman : MAP_ANON;
47     version (Darwin) import core.sys.darwin.sys.mman : MAP_ANON;
48     version (CRuntime_UClibc) import core.sys.linux.sys.mman : MAP_ANON;
49     import core.stdc.stdlib;
50 
51     //version = GC_Use_Alloc_MMap;
52 }
53 else
54 {
55     import core.stdc.stdlib;
56 
57     //version = GC_Use_Alloc_Malloc;
58 }
59 
60 /+
61 static if (is(typeof(VirtualAlloc)))
62     version = GC_Use_Alloc_Win32;
63 else static if (is(typeof(mmap)))
64     version = GC_Use_Alloc_MMap;
65 else static if (is(typeof(valloc)))
66     version = GC_Use_Alloc_Valloc;
67 else static if (is(typeof(malloc)))
68     version = GC_Use_Alloc_Malloc;
69 else static assert(false, "No supported allocation methods available.");
70 +/
71 
72 static if (is(typeof(VirtualAlloc))) // version (GC_Use_Alloc_Win32)
73 {
74     /**
75      * Map memory.
76      */
os_mem_map(size_t nbytes)77     void *os_mem_map(size_t nbytes) nothrow
78     {
79         return VirtualAlloc(null, nbytes, MEM_RESERVE | MEM_COMMIT,
80                 PAGE_READWRITE);
81     }
82 
83 
84     /**
85      * Unmap memory allocated with os_mem_map().
86      * Returns:
87      *      0       success
88      *      !=0     failure
89      */
os_mem_unmap(void * base,size_t nbytes)90     int os_mem_unmap(void *base, size_t nbytes) nothrow
91     {
92         return cast(int)(VirtualFree(base, 0, MEM_RELEASE) == 0);
93     }
94 }
95 else static if (is(typeof(mmap)))  // else version (GC_Use_Alloc_MMap)
96 {
os_mem_map(size_t nbytes)97     void *os_mem_map(size_t nbytes) nothrow
98     {   void *p;
99 
100         p = mmap(null, nbytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
101         return (p == MAP_FAILED) ? null : p;
102     }
103 
104 
os_mem_unmap(void * base,size_t nbytes)105     int os_mem_unmap(void *base, size_t nbytes) nothrow
106     {
107         return munmap(base, nbytes);
108     }
109 }
110 else static if (is(typeof(valloc))) // else version (GC_Use_Alloc_Valloc)
111 {
os_mem_map(size_t nbytes)112     void *os_mem_map(size_t nbytes) nothrow
113     {
114         return valloc(nbytes);
115     }
116 
117 
os_mem_unmap(void * base,size_t nbytes)118     int os_mem_unmap(void *base, size_t nbytes) nothrow
119     {
120         free(base);
121         return 0;
122     }
123 }
124 else static if (is(typeof(malloc))) // else version (GC_Use_Alloc_Malloc)
125 {
126     // NOTE: This assumes malloc granularity is at least (void*).sizeof.  If
127     //       (req_size + PAGESIZE) is allocated, and the pointer is rounded up
128     //       to PAGESIZE alignment, there will be space for a void* at the end
129     //       after PAGESIZE bytes used by the GC.
130 
131 
132     import gc.gc;
133 
134 
135     const size_t PAGE_MASK = PAGESIZE - 1;
136 
137 
os_mem_map(size_t nbytes)138     void *os_mem_map(size_t nbytes) nothrow
139     {   byte *p, q;
140         p = cast(byte *) malloc(nbytes + PAGESIZE);
141         q = p + ((PAGESIZE - ((cast(size_t) p & PAGE_MASK))) & PAGE_MASK);
142         * cast(void**)(q + nbytes) = p;
143         return q;
144     }
145 
146 
os_mem_unmap(void * base,size_t nbytes)147     int os_mem_unmap(void *base, size_t nbytes) nothrow
148     {
149         free( *cast(void**)( cast(byte*) base + nbytes ) );
150         return 0;
151     }
152 }
153 else
154 {
155     static assert(false, "No supported allocation methods available.");
156 }
157 
158 /**
159    Check for any kind of memory pressure.
160 
161    Params:
162       mapped = the amount of memory mapped by the GC in bytes
163    Returns:
164        true if memory is scarce
165 */
166 // TOOD: get virtual mem sizes and current usage from OS
167 // TODO: compare current RSS and avail. physical memory
version(Windows)168 version (Windows)
169 {
170     bool isLowOnMem(size_t mapped) nothrow @nogc
171     {
172         version (D_LP64)
173             return false;
174         else
175         {
176             import core.sys.windows.winbase : GlobalMemoryStatus, MEMORYSTATUS;
177             MEMORYSTATUS stat;
178             GlobalMemoryStatus(&stat);
179             // Less than 5 % of virtual address space available
180             return stat.dwAvailVirtual < stat.dwTotalVirtual / 20;
181         }
182     }
183 }
version(Darwin)184 else version (Darwin)
185 {
186     bool isLowOnMem(size_t mapped) nothrow @nogc
187     {
188         enum GB = 2 ^^ 30;
189         version (D_LP64)
190             return false;
191         else
192         {
193             // 80 % of available 4GB is used for GC (excluding malloc and mmap)
194             enum size_t limit = 4UL * GB * 8 / 10;
195             return mapped > limit;
196         }
197     }
198 }
199 else
200 {
isLowOnMem(size_t mapped)201     bool isLowOnMem(size_t mapped) nothrow @nogc
202     {
203         enum GB = 2 ^^ 30;
204         version (D_LP64)
205             return false;
206         else
207         {
208             // be conservative and assume 3GB
209             enum size_t limit = 3UL * GB * 8 / 10;
210             return mapped > limit;
211         }
212     }
213 }
214