* const-uniq-1.c: Expand regex to match AIX XCOFF labels.
[official-gcc.git] / libgo / runtime / mem.c
blob9df4c870dcfdc9d27697ff4df86abda669d75141
1 /* Defining _XOPEN_SOURCE hides the declaration of madvise() on Solaris <
2 11 and the MADV_DONTNEED definition on IRIX 6.5. */
3 #undef _XOPEN_SOURCE
5 #include <errno.h>
6 #include <unistd.h>
8 #include "runtime.h"
9 #include "arch.h"
10 #include "malloc.h"
12 #ifndef MAP_ANON
13 #ifdef MAP_ANONYMOUS
14 #define MAP_ANON MAP_ANONYMOUS
15 #else
16 #define USE_DEV_ZERO
17 #define MAP_ANON 0
18 #endif
19 #endif
21 #ifdef USE_DEV_ZERO
22 static int dev_zero = -1;
23 #endif
25 static _Bool
26 addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
28 #ifdef HAVE_MINCORE
29 size_t page_size = getpagesize();
30 size_t off;
31 char one_byte;
33 errno = 0;
34 for(off = 0; off < n; off += page_size)
35 if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
36 || errno != ENOMEM)
37 return 0;
38 #endif
39 return 1;
42 static void *
43 mmap_fixed(byte *v, uintptr n, int32 prot, int32 flags, int32 fd, uint32 offset)
45 void *p;
47 p = runtime_mmap((void *)v, n, prot, flags, fd, offset);
48 if(p != v && addrspace_free(v, n)) {
49 // On some systems, mmap ignores v without
50 // MAP_FIXED, so retry if the address space is free.
51 if(p != MAP_FAILED)
52 runtime_munmap(p, n);
53 p = runtime_mmap((void *)v, n, prot, flags|MAP_FIXED, fd, offset);
55 return p;
58 void*
59 runtime_SysAlloc(uintptr n)
61 void *p;
62 int fd = -1;
64 mstats.sys += n;
66 #ifdef USE_DEV_ZERO
67 if (dev_zero == -1) {
68 dev_zero = open("/dev/zero", O_RDONLY);
69 if (dev_zero < 0) {
70 runtime_printf("open /dev/zero: errno=%d\n", errno);
71 exit(2);
74 fd = dev_zero;
75 #endif
77 p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
78 if (p == MAP_FAILED) {
79 if(errno == EACCES) {
80 runtime_printf("runtime: mmap: access denied\n");
81 runtime_printf("if you're running SELinux, enable execmem for this process.\n");
82 exit(2);
84 return nil;
86 return p;
89 void
90 runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
92 #ifdef MADV_DONTNEED
93 runtime_madvise(v, n, MADV_DONTNEED);
94 #endif
97 void
98 runtime_SysFree(void *v, uintptr n)
100 mstats.sys -= n;
101 runtime_munmap(v, n);
104 void*
105 runtime_SysReserve(void *v, uintptr n)
107 int fd = -1;
108 void *p;
110 #ifdef USE_DEV_ZERO
111 if (dev_zero == -1) {
112 dev_zero = open("/dev/zero", O_RDONLY);
113 if (dev_zero < 0) {
114 runtime_printf("open /dev/zero: errno=%d\n", errno);
115 exit(2);
118 fd = dev_zero;
119 #endif
121 // On 64-bit, people with ulimit -v set complain if we reserve too
122 // much address space. Instead, assume that the reservation is okay
123 // if we can reserve at least 64K and check the assumption in SysMap.
124 // Only user-mode Linux (UML) rejects these requests.
125 if(sizeof(void*) == 8 && (uintptr)v >= 0xffffffffU) {
126 p = mmap_fixed(v, 64<<10, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
127 if (p != v)
128 return nil;
129 runtime_munmap(p, 64<<10);
130 return v;
133 p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
134 if(p == MAP_FAILED)
135 return nil;
136 return p;
139 void
140 runtime_SysMap(void *v, uintptr n)
142 void *p;
143 int fd = -1;
145 mstats.sys += n;
147 #ifdef USE_DEV_ZERO
148 if (dev_zero == -1) {
149 dev_zero = open("/dev/zero", O_RDONLY);
150 if (dev_zero < 0) {
151 runtime_printf("open /dev/zero: errno=%d\n", errno);
152 exit(2);
155 fd = dev_zero;
156 #endif
158 // On 64-bit, we don't actually have v reserved, so tread carefully.
159 if(sizeof(void*) == 8 && (uintptr)v >= 0xffffffffU) {
160 p = mmap_fixed(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
161 if(p == MAP_FAILED && errno == ENOMEM)
162 runtime_throw("runtime: out of memory");
163 if(p != v) {
164 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
165 runtime_throw("runtime: address space conflict");
167 return;
170 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
171 if(p != v)
172 runtime_throw("runtime: cannot map pages in arena address space");