Use calloc to allocate target memory
authorAndrew Waterman <waterman@cs.berkeley.edu>
Sat, 20 Jul 2013 01:12:50 +0000 (18:12 -0700)
committerAndrew Waterman <waterman@cs.berkeley.edu>
Sat, 20 Jul 2013 01:12:50 +0000 (18:12 -0700)
It just calls mmap under the hood, anyway...

riscv/sim.cc

index 8fafa511069a868db2016aab5f81ad8c42f1c940..c6ca04a8c2eb3c5142bf5ed8de11586580453633 100644 (file)
@@ -2,16 +2,11 @@
 
 #include "sim.h"
 #include "htif.h"
-#include <sys/mman.h>
 #include <map>
 #include <iostream>
 #include <climits>
-#include <assert.h>
-#include <unistd.h>
-
-#ifdef __linux__
-# define mmap mmap64
-#endif
+#include <cstdlib>
+#include <cassert>
 
 sim_t::sim_t(size_t _nprocs, size_t mem_mb, const std::vector<std::string>& args)
   : htif(new htif_isasim_t(this, args)),
@@ -20,23 +15,17 @@ sim_t::sim_t(size_t _nprocs, size_t mem_mb, const std::vector<std::string>& args
   // allocate target machine's memory, shrinking it as necessary
   // until the allocation succeeds
   size_t memsz0 = (size_t)mem_mb << 20;
+  size_t quantum = 1L << 20;
   if (memsz0 == 0)
     memsz0 = 1L << (sizeof(size_t) == 8 ? 32 : 30);
 
-  size_t quantum = std::max(PGSIZE, (reg_t)sysconf(_SC_PAGESIZE));
-  memsz0 = memsz0/quantum*quantum;
-
   memsz = memsz0;
-  mem = (char*)mmap(NULL, memsz, PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
+  while ((mem = (char*)calloc(1, memsz)) == NULL)
+    memsz = memsz*10/11/quantum*quantum;
 
-  if(mem == MAP_FAILED)
-  {
-    while(mem == MAP_FAILED && (memsz = memsz*10/11/quantum*quantum))
-      mem = (char*)mmap(NULL, memsz, PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
-    assert(mem != MAP_FAILED);
+  if (memsz != memsz)
     fprintf(stderr, "warning: only got %lu bytes of target mem (wanted %lu)\n",
             (unsigned long)memsz, (unsigned long)memsz0);
-  }
 
   mmu = new mmu_t(mem, memsz);
 
@@ -55,7 +44,7 @@ sim_t::~sim_t()
     delete pmmu;
   }
   delete mmu;
-  munmap(mem, memsz);
+  free(mem);
 }
 
 void sim_t::send_ipi(reg_t who)