2011-10-08 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / mem.c
blob90c2c61aff97608c703de97694614fcee90e2517
1 #include <errno.h>
2 #include <unistd.h>
4 #include "runtime.h"
5 #include "malloc.h"
7 #ifndef MAP_ANON
8 #ifdef MAP_ANONYMOUS
9 #define MAP_ANON MAP_ANONYMOUS
10 #else
11 #define USE_DEV_ZERO
12 #define MAP_ANON 0
13 #endif
14 #endif
16 #ifdef USE_DEV_ZERO
17 static int dev_zero = -1;
18 #endif
20 static _Bool
21 addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
23 #ifdef HAVE_MINCORE
24 size_t page_size = getpagesize();
25 size_t off;
26 char one_byte;
28 errno = 0;
29 for(off = 0; off < n; off += page_size)
30 if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
31 || errno != ENOMEM)
32 return 0;
33 #endif
34 return 1;
37 void*
38 runtime_SysAlloc(uintptr n)
40 void *p;
41 int fd = -1;
43 mstats.sys += n;
45 #ifdef USE_DEV_ZERO
46 if (dev_zero == -1) {
47 dev_zero = open("/dev/zero", O_RDONLY);
48 if (dev_zero < 0) {
49 printf("open /dev/zero: errno=%d\n", errno);
50 exit(2);
53 fd = dev_zero;
54 #endif
56 p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
57 if (p == MAP_FAILED) {
58 if(errno == EACCES) {
59 printf("runtime: mmap: access denied\n");
60 printf("if you're running SELinux, enable execmem for this process.\n");
61 exit(2);
63 return nil;
65 return p;
68 void
69 runtime_SysUnused(void *v, uintptr n)
71 USED(v);
72 USED(n);
73 // TODO(rsc): call madvise MADV_DONTNEED
76 void
77 runtime_SysFree(void *v, uintptr n)
79 mstats.sys -= n;
80 runtime_munmap(v, n);
83 void*
84 runtime_SysReserve(void *v, uintptr n)
86 int fd = -1;
88 // On 64-bit, people with ulimit -v set complain if we reserve too
89 // much address space. Instead, assume that the reservation is okay
90 // and check the assumption in SysMap.
91 if(sizeof(void*) == 8)
92 return v;
94 #ifdef USE_DEV_ZERO
95 if (dev_zero == -1) {
96 dev_zero = open("/dev/zero", O_RDONLY);
97 if (dev_zero < 0) {
98 printf("open /dev/zero: errno=%d\n", errno);
99 exit(2);
102 fd = dev_zero;
103 #endif
105 return runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
108 void
109 runtime_SysMap(void *v, uintptr n)
111 void *p;
112 int fd = -1;
114 mstats.sys += n;
116 #ifdef USE_DEV_ZERO
117 if (dev_zero == -1) {
118 dev_zero = open("/dev/zero", O_RDONLY);
119 if (dev_zero < 0) {
120 printf("open /dev/zero: errno=%d\n", errno);
121 exit(2);
124 fd = dev_zero;
125 #endif
127 // On 64-bit, we don't actually have v reserved, so tread carefully.
128 if(sizeof(void*) == 8) {
129 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
130 if(p != v && addrspace_free(v, n)) {
131 // On some systems, mmap ignores v without
132 // MAP_FIXED, so retry if the address space is free.
133 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
135 if(p != v) {
136 runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
137 runtime_throw("runtime: address space conflict");
139 return;
142 p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
143 if(p != v)
144 runtime_throw("runtime: cannot map pages in arena address space");