The OpenD Programming Language

1 module rt.sys.darwin.osmemory;
2 
3 version (OSX)
4     version = Darwin;
5 else version (iOS)
6     version = Darwin;
7 else version (TVOS)
8     version = Darwin;
9 else version (WatchOS)
10     version = Darwin;
11 
12 version (Darwin):
13 
14 public import rt.sys.posix.osmemory: os_mem_map, os_mem_unmap, wait_pid, ChildStatus,
15     getPageSize, pid_t, fork;
16 
17 /**
18    Check for any kind of memory pressure.
19 
20    Params:
21       mapped = the amount of memory mapped by the GC in bytes
22    Returns:
23        true if memory is scarce
24 */
25 // TODO: get virtual mem sizes and current usage from OS
26 // TODO: compare current RSS and avail. physical memory
27 bool isLowOnMem(size_t mapped) nothrow @nogc
28 {
29     enum GB = 2 ^^ 30;
30     version (D_LP64)
31         return false;
32   
33     // 80 % of available 4GB is used for GC (excluding malloc and mmap)
34     enum size_t limit = 4UL * GB * 8 / 10;
35     return mapped > limit;
36 }
37 
38 
39 /**
40    Get the size of available physical memory
41 
42    Returns:
43        size of installed physical RAM
44 */
45 
46 extern (C) int sysctl(const int* name, uint namelen, void* oldp, size_t* oldlenp, const void* newp, size_t newlen) @nogc nothrow;
47 ulong os_physical_mem() nothrow @nogc
48 {
49     enum
50     {
51         CTL_HW = 6,
52         HW_MEMSIZE = 24,
53     }
54     int[2] mib = [ CTL_HW, HW_MEMSIZE ];
55     ulong system_memory_bytes;
56     size_t len = system_memory_bytes.sizeof;
57     if (sysctl(mib.ptr, 2, &system_memory_bytes, &len, null, 0) != 0)
58         return 0;
59     return system_memory_bytes;
60 }