From patchwork Sat Jan 29 07:16:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: libgo patch committed: If no MAP_ANON, use /dev/zero Date: Fri, 28 Jan 2011 21:16:24 -0000 From: Ian Taylor X-Patchwork-Id: 80922 Message-Id: To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com This patch adjusts libgo so that if the system does not support MAP_ANON/MAP_ANONYMOUS, it mmaps from /dev/zero. Bootstrapped and ran Go testsuite (both using /dev/zero and not) on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 93d03525a193 libgo/runtime/mem.c --- a/libgo/runtime/mem.c Fri Jan 28 16:05:44 2011 -0800 +++ b/libgo/runtime/mem.c Fri Jan 28 23:10:41 2011 -0800 @@ -3,13 +3,39 @@ #include "runtime.h" #include "malloc.h" +#ifndef MAP_ANON +#ifdef MAP_ANONYMOUS +#define MAP_ANON MAP_ANONYMOUS +#else +#define USE_DEV_ZERO +#define MAP_ANON 0 +#endif +#endif + +#ifdef USE_DEV_ZERO +static int dev_zero = -1; +#endif + void* runtime_SysAlloc(uintptr n) { void *p; + int fd = -1; mstats.sys += n; - p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0); + +#ifdef USE_DEV_ZERO + if (dev_zero == -1) { + dev_zero = open("/dev/zero", O_RDONLY); + if (dev_zero < 0) { + printf("open /dev/zero: errno=%d\n", errno); + exit(2); + } + } + fd = dev_zero; +#endif + + p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0); if (p == MAP_FAILED) { if(errno == EACCES) { printf("mmap: access denied\n"); diff -r 93d03525a193 libgo/runtime/runtime.h --- a/libgo/runtime/runtime.h Fri Jan 28 16:05:44 2011 -0800 +++ b/libgo/runtime/runtime.h Fri Jan 28 23:10:41 2011 -0800 @@ -12,6 +12,9 @@ #include #include #include +#include +#include +#include #include #include