[nhc-bugs] Fixing "OS allocated a heap in high memory"

Donald Bruce Stewart dons at cse.unsw.edu.au
Sat Oct 22 05:54:02 EDT 2005


Hey guys,

Recent changes to malloc on OpenBSD mean that nhc98 builds now die with
the "OS allocated a heap in high memory" error, as malloc is putting
things above the 0x80000000 line.

Switching the heap allocation to use mmap with a hint to allocate
somewhere lower fixes it at least on x86/openbsd. The ghc testsuite
runs, for example. The mmap code is stolen from MBlock.c in ghc.

Any options on this? Is it reasonable? (I'd like to use it as a custom
patch in the OpenBSD port of nhc98, but want to check I'm not doing
anything hideously wrong :)

-- Don

Here's what I'm doing:

--- src/runtime/Kernel/collector.c.orig	Tue Mar  8 03:53:38 2005
+++ src/runtime/Kernel/collector.c	Sat Oct 22 19:05:22 2005
@@ -7,6 +7,11 @@
 /*#include "runtime.h"    -- already included in node.h */
 #include "mark.h"
 
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+
+
 /*#define HEAPSIZE 100000  -- defined in top-level Makefile at config-time */
 #define GCSTACKSIZE 20000
 
@@ -45,11 +50,22 @@ void initGc(Int hpSize,NodePtr *ihp,Int 
 {
   Int totalSize = hpSize+spSize;
   Int tableSize = (totalSize+WORDSIZE)/(WORDSIZE+1)+1; /* Last one for end of marked */
+  int size      = (int)totalSize * sizeof(Node);
 
-  if(NULL == (hpStart = malloc ((int)totalSize * sizeof(Node)))) {
-    fprintf(stderr,"Not enough memory for heap and stack.\n");
-    exit(-1);
-  }
+  /* Ask for memory well below the 0x80000000 line. The nhc runtime
+   * reserves the high bit on addresses for its own purposes */
+  hpStart = mmap((void *)0x40000000, size,
+                        PROT_EXEC|PROT_READ|PROT_WRITE,
+                        MAP_PRIVATE|MAP_ANON, -1 , 0);
+
+  if (hpStart == MAP_FAILED) {
+        if (errno == ENOMEM) {
+            fprintf(stderr,"initGc: out of memory (requested %lu bytes)", size);
+            exit(-1);
+        } else {
+            fprintf(stderr, "initGc: mmap failed: %s", strerror(errno));
+        }
+    }
 
   hpEnd = hpStart + totalSize;
 


More information about the Nhc-bugs mailing list