From 97a9245bdd3de9b9a58a684a4e8b2fe7772a0e2a Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Fri, 19 Jul 2013 18:12:50 -0700 Subject: [PATCH] Use calloc to allocate target memory It just calls mmap under the hood, anyway... --- riscv/sim.cc | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/riscv/sim.cc b/riscv/sim.cc index 8fafa51..c6ca04a 100644 --- a/riscv/sim.cc +++ b/riscv/sim.cc @@ -2,16 +2,11 @@ #include "sim.h" #include "htif.h" -#include #include #include #include -#include -#include - -#ifdef __linux__ -# define mmap mmap64 -#endif +#include +#include sim_t::sim_t(size_t _nprocs, size_t mem_mb, const std::vector& 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& 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) -- 2.30.2