Debian Codegraph @

runtime/platform/cygwin.c

raw symbols SCIP
1 #define _GNU_SOURCE
2
3 #include "platform.h"
4
5 #include "platform/mmap.c"
6 #if not HAS_MSG_DONTWAIT
7 #include "platform/recv.nonblock.c"
8 #endif
9 #include "platform/windows.c"
10 #include "platform/mremap.c"
11
12 /*
13 * The sysconf(_SC_PAGESIZE) is the necessary alignment for using
14 * mmap. Windows has another notion of page size (that corresponds to
15 * physical page size?). Just to be safe, we take the least common
16 * multiple of the sysconf and Windows notions of page size.
17 *
18 * Since sysconf(_SC_PAGESIZE) might not correspond to the physical
19 * page size, we can't use sysconf(_SC_PHYS_PAGES) to get physical
20 * memory. So, use the Windows function.
21 *
22 * See: http://cygwin.com/ml/cygwin/2006-06/msg00341.html
23 */
24 static size_t GC_pageSize_sysconf (void) {
25 SYSTEM_INFO sysinfo;
26 long int pageSize;
27
28 pageSize = sysconf (_SC_PAGESIZE);
29 GetSystemInfo(&sysinfo);
30
31 /* MLton_Platform_CygwinUseMmap is not set when this is called.
32 * Assume the worst; choose the larger allocation unit.
33 */
34 if ((size_t)pageSize < (size_t)sysinfo.dwAllocationGranularity)
35 return (size_t)sysinfo.dwAllocationGranularity;
36 else
37 return (size_t)pageSize;
38 }
39
40 static size_t GC_pageSize_windows (void) {
41 SYSTEM_INFO sysinfo;
42 GetSystemInfo(&sysinfo);
43 return (size_t)sysinfo.dwPageSize;
44 }
45
46 size_t GC_pageSize (void) {
47 size_t pageSize_sysconf = GC_pageSize_sysconf ();
48 size_t pageSize_windows = GC_pageSize_windows ();
49
50 size_t a = pageSize_sysconf;
51 size_t b = pageSize_windows;
52 size_t t;
53 while (b != 0) {
54 t = b;
55 b = a % b;
56 a = t;
57 }
58 size_t gcd = a;
59
60 size_t lcm = (pageSize_sysconf / gcd) * pageSize_windows;
61
62 return lcm;
63 }
64
65 uintmax_t GC_physMem (void) {
66 MEMORYSTATUS memstat;
67
68 memstat.dwLength = sizeof(memstat);
69 GlobalMemoryStatus(&memstat);
70 return (uintmax_t)memstat.dwTotalPhys;
71 }
72
73 void *GC_mmapAnon (void *start, size_t length) {
74 if (MLton_Platform_CygwinUseMmap)
75 return mmapAnon (start, length);
76 else
77 return Windows_mmapAnon (start, length);
78 }
79
80 void *GC_mmapAnonFlags (void *start, size_t length, __attribute__ ((unused)) int flags) {
81 return GC_mmapAnon(start, length);
82 }
83
84 void GC_release (void *base, size_t length) {
85 if (MLton_Platform_CygwinUseMmap)
86 munmap_safe (base, length);
87 else
88 Windows_release (base, length);
89 }
90
91 void* GC_extendHead (void *base, size_t length) {
92 if (MLton_Platform_CygwinUseMmap)
93 return mmapAnon (base, length);
94 else
95 return Windows_mmapAnon (base, length);
96 }
97
98 void* GC_extendTail (void *base, size_t length) {
99 if (MLton_Platform_CygwinUseMmap)
100 return mmapAnon (base, length);
101 else
102 return Windows_extend (base, length);
103 }
104
105 HANDLE fileDesHandle (int fd) {
106 // The temporary prevents a "cast does not match function type" warning.
107 long t;
108
109 t = get_osfhandle (fd);
110 return (HANDLE)t;
111 }
112
113 /* ------------------------------------------------- */
114 /* Cygwin */
115 /* ------------------------------------------------- */
116
117 C_String_t Cygwin_toFullWindowsPath (NullString8_t path) {
118 static char res[MAX_PATH];
119
120 #if ((CYGWIN_VERSION_API_MAJOR > 0) || (CYGWIN_VERSION_API_MINOR > 181))
121 cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
122 (char*)path, &res[0], MAX_PATH);
123 #else
124 cygwin_conv_to_full_win32_path ((char*)path, &res[0]);
125 #endif
126 return (C_String_t)&res[0];
127 }
128
129 /* ------------------------------------------------- */
130 /* Posix */
131 /* ------------------------------------------------- */
132
133 void Posix_IO_setbin (C_Fd_t fd) {
134 /* cygwin has a different method for working with its fds */
135 setmode (fd, O_BINARY);
136 }
137
138 void Posix_IO_settext (C_Fd_t fd) {
139 /* cygwin has a different method for working with its fds */
140 setmode (fd, O_TEXT);
141 }