Browse Source

Use calloc to allocate target memory

It just calls mmap under the hood, anyway...
confprec
Andrew Waterman 13 years ago
parent
commit
97a9245bdd
  1. 25
      riscv/sim.cc

25
riscv/sim.cc

@ -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)

Loading…
Cancel
Save