Browse Source

Add int_map and use it to speed up sparse mem

sparse-mem
Andrew Waterman 5 years ago
parent
commit
c234f51c04
  1. 2
      riscv/devices.cc
  2. 14
      riscv/int_map.h

2
riscv/devices.cc

@ -92,7 +92,7 @@ bool mmio_plugin_device_t::store(reg_t addr, size_t len, const uint8_t* bytes)
}
mem_t::mem_t(reg_t size)
: sparse_memory_map(log2(size) + (size & (size-1) ? 1 : 0)), sz(size)
: sz(size)
{
if (size == 0 || size % PGSIZE != 0)
throw std::runtime_error("memory size must be a positive multiple of 4 KiB");

14
riscv/int_map.h

@ -1,16 +1,15 @@
#ifndef _RISCV_INT_MAP_H
#define _RISCV_INT_MAP_H
#include "common.h"
#include <cstdint>
#include <climits>
#include <cassert>
// Like std::map, but keys are integers
template<typename K, typename V, int lg_radix = 11>
template<typename K, typename V, int lg_n = sizeof(K) * CHAR_BIT, int lg_radix = 11>
class int_map {
public:
int_map(int lg_n = sizeof(K) * CHAR_BIT)
: lg_n(lg_n)
int_map()
{
memset(array, 0, sizeof(array));
}
@ -24,15 +23,14 @@ class int_map {
return reinterpret_cast<V*>(&array[idx]);
}
if (array[idx] == nullptr)
array[idx] = new int_map<K, V, lg_radix>(lg_n - lg_radix);
if (unlikely(array[idx] == nullptr))
array[idx] = new int_map<K, V, lg_n < 0 ? 0 : lg_n - lg_radix, lg_radix>;
return array[idx]->lookup(k >> lg_radix);
}
private:
int lg_n;
int_map<K, V, lg_radix>* array[size_t(1) << lg_radix];
int_map<K, V, lg_n < 0 ? 0 : lg_n - lg_radix, lg_radix>* array[size_t(1) << lg_radix];
};
#endif

Loading…
Cancel
Save