Skip to content

Use shared_ptr and weak_ptr instead of unique_ptr and raw pointers #673

Merged
merged 19 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion simulator/func_sim/func_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ template <typename ISA>
FuncSim<ISA>::FuncSim( bool log) : Simulator( log) { }

template <typename ISA>
void FuncSim<ISA>::set_memory( FuncMemory* m)
void FuncSim<ISA>::set_memory( std::shared_ptr<FuncMemory> m)
{
mem = m;
imem.set_memory( m);
Expand Down
4 changes: 2 additions & 2 deletions simulator/func_sim/func_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FuncSim : public Simulator
RF<ISA> rf;
Addr PC = NO_VAL32;
uint64 sequence_id = 0;
FuncMemory* mem = nullptr;
std::shared_ptr<FuncMemory> mem;
InstrMemoryCached<FuncInstr> imem;

uint64 nops_in_a_row = 0;
Expand All @@ -42,7 +42,7 @@ class FuncSim : public Simulator
public:
explicit FuncSim( bool log = false);

void set_memory( FuncMemory* memory) final;
void set_memory( std::shared_ptr<FuncMemory> memory) final;
void init_checker() final { };
FuncInstr step();
Trap run(uint64 instrs_to_run) final;
Expand Down
4 changes: 2 additions & 2 deletions simulator/func_sim/instr_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
template<typename Instr>
class InstrMemory
{
FuncMemory* mem = nullptr;
std::shared_ptr<FuncMemory> mem;
public:
void set_memory( FuncMemory* m) { mem = m; }
void set_memory( std::shared_ptr<FuncMemory> m) { mem = m; }
auto fetch( Addr pc) const { return mem->read<uint32, Instr::endian>( pc); }
auto fetch_instr( Addr PC) { return Instr( fetch( PC), PC); }
};
Expand Down
8 changes: 4 additions & 4 deletions simulator/func_sim/t/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ TEST_CASE( "FuncSim: create empty memory and get lost")
{
auto m = FuncMemory::create_hierarchied_memory();
FuncSim<MIPS32> sim( false);
sim.set_memory( m.get());
sim.set_memory( m);
CHECK_THROWS_AS( sim.run_no_limit(), BearingLost);
}

// TODO: remove that class, use true FuncSim interfaces instead
template <typename ISA>
struct FuncSimAndMemory : FuncSim<ISA>
{
std::unique_ptr<FuncMemory> mem;
std::shared_ptr<FuncMemory> mem;

FuncSimAndMemory() : FuncSim<ISA>(), mem( FuncMemory::create_hierarchied_memory())
{
this->set_memory( mem.get());
this->set_memory( mem);
}

void init_trace( const std::string& tr) {
Expand All @@ -64,7 +64,7 @@ TEST_CASE( "FuncSim: get lost without pc")
{
auto m = FuncMemory::create_hierarchied_memory();
FuncSim<MIPS32> sim( false);
sim.set_memory( m.get());
sim.set_memory( m);
ElfLoader( valid_elf_file).load_to( m.get());
CHECK_THROWS_AS( sim.run_no_limit(), BearingLost);
}
Expand Down
8 changes: 4 additions & 4 deletions simulator/interface/cen64/cen64_intf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
#include <memory/memory.h>
#include <simulator.h>

extern std::unique_ptr<FuncMemory> generate_cen64_memory( bus_controller * bus_ptr);
extern std::shared_ptr<FuncMemory> generate_cen64_memory( bus_controller * bus_ptr);

struct vr4300
{
std::unique_ptr<CycleAccurateSimulator> sim;
std::unique_ptr<FuncMemory> bus;
std::shared_ptr<CycleAccurateSimulator> sim;
std::shared_ptr<FuncMemory> bus;

int init( struct bus_controller * bus_ptr)
{
sim = CycleAccurateSimulator::create_simulator("mips64", true);
bus = generate_cen64_memory( bus_ptr);
sim->set_memory( bus.get());
sim->set_memory( bus);
sim->set_pc(0x1fc00000ull);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions simulator/interface/cen64/cen64_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CEN64Memory : public FuncMemory
size_t memcpy_host_to_guest( Addr dst, const Byte* src, size_t size) final;
size_t memcpy_guest_to_host( Byte* dst, Addr src, size_t size) const noexcept final;
std::string dump() const final { assert(0); return {}; }
void duplicate_to( FuncMemory* /* target */) const final { assert(0); }
void duplicate_to( std::shared_ptr<FuncMemory> /* target */) const final { assert(0); }
private:
bus_controller* bus = nullptr;

Expand Down Expand Up @@ -70,7 +70,7 @@ size_t CEN64Memory::memcpy_host_to_guest( Addr dst, const Byte* src, size_t size
return result;
}

std::unique_ptr<FuncMemory> generate_cen64_memory( bus_controller * bus_ptr)
std::shared_ptr<FuncMemory> generate_cen64_memory( bus_controller * bus_ptr)
{
return std::make_unique<CEN64Memory>( bus_ptr);
return std::make_shared<CEN64Memory>( bus_ptr);
}
2 changes: 1 addition & 1 deletion simulator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main( int argc, const char* argv[]) try {
elf.load_to( memory.get());

auto sim = Simulator::create_configured_simulator();
sim->set_memory( memory.get());
sim->set_memory( memory);
sim->init_checker();
sim->set_pc( elf.get_startPC());
sim->run( config::num_steps);
Expand Down
5 changes: 3 additions & 2 deletions simulator/memory/elf/elf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
static void load_elf_section( FuncMemory* memory, const ELFIO::section& section, AddrDiff offset)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) Connecting ELFIO to our guidelines
memory->memcpy_host_to_guest( section.get_address() + offset, reinterpret_cast<const Byte*>(section.get_data()), section.get_size());
auto src = reinterpret_cast<const Byte*>( section.get_data ());
memory->memcpy_host_to_guest( section.get_address() + offset, src, section.get_size());
}

ElfLoader::ElfLoader( const std::string& filename, AddrDiff offset)
Expand All @@ -23,7 +24,7 @@ ElfLoader::ElfLoader( const std::string& filename, AddrDiff offset)
throw InvalidElfFile( filename);
}

void ElfLoader::load_to( FuncMemory* memory) const
void ElfLoader::load_to( FuncMemory *memory) const
{
for ( const auto& section : reader->sections)
if ( section->get_address() != 0)
Expand Down
2 changes: 1 addition & 1 deletion simulator/memory/elf/elf_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ElfLoader
ElfLoader& operator=( const ElfLoader&) = delete;
ElfLoader& operator=( ElfLoader&&) = delete;

void load_to(FuncMemory* memory) const;
void load_to( FuncMemory *memory) const;
Addr get_startPC() const;
private:
const std::unique_ptr<ELFIO::elfio> reader;
Expand Down
8 changes: 4 additions & 4 deletions simulator/memory/hierarchied_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HierarchiedMemory : public FuncMemory
std::string dump() const final;
size_t memcpy_host_to_guest( Addr dst, const Byte* src, size_t size) final;
size_t memcpy_guest_to_host( Byte* dst, Addr src, size_t size) const noexcept final;
void duplicate_to( FuncMemory* target) const final;
void duplicate_to( std::shared_ptr<FuncMemory> target) const final;

private:
const uint32 page_bits;
Expand Down Expand Up @@ -66,10 +66,10 @@ class HierarchiedMemory : public FuncMemory
void alloc_and_write_byte( Addr addr, Byte value);
};

std::unique_ptr<FuncMemory>
std::shared_ptr<FuncMemory>
FuncMemory::create_hierarchied_memory( uint32 addr_bits, uint32 page_bits, uint32 offset_bits)
{
return std::make_unique<HierarchiedMemory>( addr_bits, page_bits, offset_bits);
return std::make_shared<HierarchiedMemory>( addr_bits, page_bits, offset_bits);
}

HierarchiedMemory::HierarchiedMemory( uint32 addr_bits,
Expand Down Expand Up @@ -149,7 +149,7 @@ bool HierarchiedMemory::check( Addr addr) const noexcept
return !set.empty() && !set[get_page(addr)].empty();
}

void HierarchiedMemory::duplicate_to( FuncMemory* target) const
void HierarchiedMemory::duplicate_to( std::shared_ptr<FuncMemory> target) const
{
for ( auto set_it = memory.begin(); set_it != memory.end(); ++set_it)
for ( auto page_it = set_it->begin(); page_it != set_it->end(); ++page_it)
Expand Down
6 changes: 3 additions & 3 deletions simulator/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ struct FuncMemoryOutOfRange final : Exception
class FuncMemory
{
public:
static std::unique_ptr<FuncMemory>
static std::shared_ptr<FuncMemory>
create_hierarchied_memory( uint32 addr_bits = 32,
uint32 page_bits = 10,
uint32 offset_bits = 12);
static std::unique_ptr<FuncMemory>
static std::shared_ptr<FuncMemory>
create_plain_memory( uint32 addr_bits = 20);

virtual size_t memcpy_host_to_guest( Addr dst, const Byte* src, size_t size) = 0;
virtual size_t memcpy_guest_to_host( Byte* dst, Addr src, size_t size) const noexcept = 0;
virtual void duplicate_to( FuncMemory* target) const = 0;
virtual void duplicate_to( std::shared_ptr<FuncMemory> target) const = 0;
virtual std::string dump() const = 0;

size_t memcpy_host_to_guest_noexcept( Addr dst, const Byte* src, size_t size) noexcept;
Expand Down
8 changes: 4 additions & 4 deletions simulator/memory/plain_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class PlainMemory : public FuncMemory
std::string dump() const final;
size_t memcpy_host_to_guest( Addr dst, const Byte* src, size_t size) final;
size_t memcpy_guest_to_host( Byte* dst, Addr src, size_t size) const noexcept final;
void duplicate_to( FuncMemory* target) const final;
void duplicate_to( std::shared_ptr<FuncMemory> target) const final;
private:
std::vector<Byte> arena;
};

std::unique_ptr<FuncMemory>
std::shared_ptr<FuncMemory>
FuncMemory::create_plain_memory( uint32 addr_bits)
{
return std::make_unique<PlainMemory>( addr_bits);
return std::make_shared<PlainMemory>( addr_bits);
}

PlainMemory::PlainMemory( uint32 addr_bits) try
Expand All @@ -39,7 +39,7 @@ catch (const std::bad_alloc&)
throw FuncMemoryBadMapping("Too many address guest address bits");
}

void PlainMemory::duplicate_to( FuncMemory* target) const
void PlainMemory::duplicate_to( std::shared_ptr<FuncMemory> target) const
{
target->memcpy_host_to_guest( 0, arena.data(), arena.size());
}
Expand Down
6 changes: 3 additions & 3 deletions simulator/memory/t/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ TEST_CASE( "Func_memory: Duplicate")
auto mem2 = FuncMemory::create_hierarchied_memory( 48, 15, 10);

ElfLoader( valid_elf_file, -0x400000).load_to( mem1.get());
mem1->duplicate_to( mem2.get());
mem1->duplicate_to( mem2);
test_coherency( mem1.get(), mem2.get());
}

Expand All @@ -286,7 +286,7 @@ TEST_CASE( "Func_memory: Plain Memory")
auto mem2 = FuncMemory::create_plain_memory( 24);

ElfLoader( valid_elf_file, -0x400000).load_to( mem1.get());
mem1->duplicate_to( mem2.get());
mem1->duplicate_to( mem2);
test_coherency( mem1.get(), mem2.get());
}

Expand All @@ -296,6 +296,6 @@ TEST_CASE( "Func_memory: Duplicate Plain Memory")
auto mem2 = FuncMemory::create_hierarchied_memory();

ElfLoader( valid_elf_file, -0x400000).load_to( mem1.get());
mem1->duplicate_to( mem2.get());
mem1->duplicate_to( mem2);
test_coherency( mem1.get(), mem2.get());
}
2 changes: 1 addition & 1 deletion simulator/modules/core/perf_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PerfSim<ISA>::PerfSim(bool log) :
}

template <typename ISA>
void PerfSim<ISA>::set_memory( FuncMemory* m)
void PerfSim<ISA>::set_memory( std::shared_ptr<FuncMemory> m)
{
memory = m;
fetch.set_memory( m);
Expand Down
4 changes: 2 additions & 2 deletions simulator/modules/core/perf_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PerfSim : public CycleAccurateSimulator
~PerfSim() override { destroy_ports(); }
Trap run( uint64 instrs_to_run) final;
void set_target( const Target& target) final;
void set_memory( FuncMemory* memory) final;
void set_memory( std::shared_ptr<FuncMemory> memory) final;
void clock() final;
void halt() final { force_halt = true; }
void init_checker() final { writeback.init_checker( *memory); }
Expand Down Expand Up @@ -63,7 +63,7 @@ class PerfSim : public CycleAccurateSimulator

/* simulator units */
RF<ISA> rf;
FuncMemory* memory = nullptr;
std::shared_ptr<FuncMemory> memory;

Fetch<ISA> fetch;
Decode<ISA> decode;
Expand Down
8 changes: 4 additions & 4 deletions simulator/modules/core/t/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ TEST_CASE( "PerfSim: create empty memory and get lost")
{
auto m = FuncMemory::create_hierarchied_memory();
PerfSim<MIPS32> sim( false);
sim.set_memory( m.get());
sim.set_memory( m);
CHECK_THROWS_AS( sim.run_no_limit(), Deadlock);
}

// TODO: remove that class, use true FuncSim interfaces instead
template <typename ISA>
struct PerfSimAndMemory : PerfSim<ISA>
{
std::unique_ptr<FuncMemory> mem;
std::shared_ptr<FuncMemory> mem;

PerfSimAndMemory(bool log) : PerfSim<ISA>(log), mem( FuncMemory::create_hierarchied_memory())
{
this->set_memory( mem.get());
this->set_memory( mem);
}

void init_trace( const std::string& tr) {
Expand Down Expand Up @@ -106,7 +106,7 @@ TEST_CASE( "Perf_Sim: Run_SMC_Trace_WithoutChecker")
{
PerfSim<MIPS32> sim( false);
auto mem = FuncMemory::create_hierarchied_memory();
sim.set_memory( mem.get());
sim.set_memory( mem);
ElfLoader elf( smc_code);
elf.load_to( mem.get());
sim.set_pc( elf.get_startPC());
Expand Down
2 changes: 1 addition & 1 deletion simulator/modules/fetch/fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Fetch : public Log
public:
explicit Fetch( bool log);
void clock( Cycle cycle);
void set_memory( FuncMemory* mem) { memory.set_memory( mem); }
void set_memory( std::shared_ptr<FuncMemory> mem) { memory.set_memory( mem); }

private:
InstrMemoryCached<FuncInstr> memory;
Expand Down
4 changes: 2 additions & 2 deletions simulator/modules/mem/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Mem : public Log
using InstructionOutput = std::pair< RegisterUInt, RegisterUInt>;

private:
FuncMemory* memory = nullptr;
std::shared_ptr<FuncMemory> memory;

std::unique_ptr<WritePort<Instr>> wp_datapath = nullptr;
std::unique_ptr<ReadPort<Instr>> rp_datapath = nullptr;
Expand All @@ -41,7 +41,7 @@ class Mem : public Log
public:
explicit Mem( bool log);
void clock( Cycle cycle);
void set_memory( FuncMemory* mem) { memory = mem; }
void set_memory( std::shared_ptr<FuncMemory> mem) { memory = mem; }
};


Expand Down
6 changes: 3 additions & 3 deletions simulator/modules/writeback/writeback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Writeback<ISA>::Writeback(bool log) : Log( log)
template <typename ISA>
void Writeback<ISA>::Checker::init( const FuncMemory& outer_mem)
{
sim = std::make_unique<FuncSim<ISA>>();
sim = std::make_shared<FuncSim<ISA>>();
memory = FuncMemory::create_hierarchied_memory();
outer_mem.duplicate_to( memory.get());
sim->set_memory( memory.get());
outer_mem.duplicate_to( memory);
sim->set_memory( memory);
active = true;
}

Expand Down
4 changes: 2 additions & 2 deletions simulator/modules/writeback/writeback.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Writeback : public Log
Cycle last_writeback_cycle = 0_cl;

class Checker {
std::unique_ptr<FuncSim<ISA>> sim;
std::unique_ptr<FuncMemory> memory;
std::shared_ptr<FuncSim<ISA>> sim;
std::shared_ptr<FuncMemory> memory;
bool active = false;
public:
void check( const FuncInstr& instr);
Expand Down
6 changes: 3 additions & 3 deletions simulator/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SimulatorFactory {
}
};

std::unique_ptr<Simulator>
std::shared_ptr<Simulator>
Simulator::create_simulator( const std::string& isa, bool functional_only, bool log)
{
if ( functional_only)
Expand All @@ -110,13 +110,13 @@ Simulator::create_simulator( const std::string& isa, bool functional_only, bool
return CycleAccurateSimulator::create_simulator( isa, log);
}

std::unique_ptr<Simulator>
std::shared_ptr<Simulator>
Simulator::create_configured_simulator()
{
return create_simulator( config::isa, config::functional_only, config::disassembly_on);
}

std::unique_ptr<CycleAccurateSimulator>
std::shared_ptr<CycleAccurateSimulator>
CycleAccurateSimulator::create_simulator( const std::string& isa, bool log)
{
return SimulatorFactory::get_instance().get_perfsim( isa, log);
Expand Down