Patchwork libgo patch committed: If no MAP_ANON, use /dev/zero

login
register
mail settings
Submitter Ian Taylor
Date Jan. 29, 2011, 7:16 a.m.
Message ID <mcr8vy4qiwn.fsf@google.com>
Download mbox | patch
Permalink /patch/80922/
State New
Headers show

Comments

Ian Taylor - Jan. 29, 2011, 7:16 a.m.
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

Patch

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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <pthread.h>
 #include <semaphore.h>