Patchwork [gccgo] In absence of mmap, use posix_memalign and not malloc

login
register
mail settings
Submitter Ian Taylor
Date June 15, 2010, 2:15 p.m.
Message ID <mcrhbl4bcv7.fsf@dhcp-172-17-9-151.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/55673/
State New
Headers show

Comments

Ian Taylor - June 15, 2010, 2:15 p.m.
This patch from Vinu Rajashekhar fixes libgo to use posix_memalign
rather than malloc when mmap is not available.  Otherwise the
alignment expected by malloc is not available.  Committed to gccgo
branch.

Ian

Patch

diff -r 6462cc9e6b81 libgo/Makefile.am
--- a/libgo/Makefile.am	Sun Jun 13 23:12:53 2010 -0700
+++ b/libgo/Makefile.am	Tue Jun 15 07:13:56 2010 -0700
@@ -236,6 +236,12 @@ 
 	testing/quick.gox \
 	testing/script.gox
 
+if HAVE_SYS_MMAN_H
+runtime_mem_file = runtime/mem.c
+else
+runtime_mem_file = runtime/mem_posix_memalign.c
+endif
+
 runtime_files = \
 	runtime/go-bad-index.c \
 	runtime/go-byte-array-to-string.c \
@@ -313,7 +319,7 @@ 
 	runtime/go-unwind.c \
 	runtime/mcache.c \
 	runtime/mcentral.c \
-	runtime/mem.c \
+	$(runtime_mem_file) \
 	runtime/mfinal.c \
 	runtime/mfixalloc.c \
 	runtime/mgc0.c \
diff -r 6462cc9e6b81 libgo/configure.ac
--- a/libgo/configure.ac	Sun Jun 13 23:12:53 2010 -0700
+++ b/libgo/configure.ac	Tue Jun 15 07:13:56 2010 -0700
@@ -185,6 +185,7 @@ 
 GCC_CHECK_UNWIND_GETIPINFO
 
 AC_CHECK_HEADERS(sys/mman.h syscall.h sys/epoll.h sys/ptrace.h sys/user.h)
+AM_CONDITIONAL(HAVE_SYS_MMAN_H, test "$ac_cv_header_sys_mman_h" = yes)
 AC_CHECK_FUNCS(srandom random strsignal)
 
 CFLAGS_hold=$CFLAGS
diff -r 6462cc9e6b81 libgo/runtime/mem_posix_memalign.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgo/runtime/mem_posix_memalign.c	Tue Jun 15 07:13:56 2010 -0700
@@ -0,0 +1,35 @@ 
+#include <errno.h>
+
+#include "runtime.h"
+#include "malloc.h"
+
+void*
+SysAlloc(uintptr n)
+{
+	void *p;
+
+	mstats.sys += n;
+	errno = posix_memalign(&p, PageSize, n);
+	if (errno > 0) {
+		perror("posix_memalign");
+		exit(2);
+	}
+	return p;
+}
+
+void
+SysUnused(void *v, uintptr n)
+{
+	USED(v);
+	USED(n);
+	// TODO(rsc): call madvise MADV_DONTNEED
+}
+
+void
+SysFree(void *v, uintptr n)
+{
+	USED(v);
+	USED(n);
+	// TODO(rsc): call munmap
+}
+
diff -r 6462cc9e6b81 libgo/runtime/runtime.h
--- a/libgo/runtime/runtime.h	Sun Jun 13 23:12:53 2010 -0700
+++ b/libgo/runtime/runtime.h	Tue Jun 15 07:13:56 2010 -0700
@@ -126,12 +126,5 @@ 
 MCache*	allocmcache(void);
 void	free(void *v);
 void	addfinalizer(void*, void(*fn)(void*), int32);
-
-#ifdef HAVE_SYS_MMAN_H
 #define runtime_mmap mmap
-#else
-#define runtime_mmap(start, len, prot, flags, fd, offset) malloc(len)
-#define MAP_FAILED NULL
-#endif
-
 #define cas(pval, old, new) __sync_bool_compare_and_swap (pval, old, new)