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

Conversation

trexxet
Copy link
Contributor

@trexxet trexxet commented Oct 24, 2018

#662
Circular dependencies are resolved by use of weak_ptr's.

@codecov
Copy link

codecov bot commented Oct 24, 2018

Codecov Report

Merging #673 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #673      +/-   ##
==========================================
+ Coverage   94.05%   94.05%   +<.01%     
==========================================
  Files          50       50              
  Lines        1665     1666       +1     
==========================================
+ Hits         1566     1567       +1     
  Misses         99       99
Impacted Files Coverage Δ
simulator/simulator.h 100% <ø> (ø) ⬆️
simulator/modules/core/perf_sim.h 87.5% <ø> (ø) ⬆️
simulator/memory/memory.h 97.5% <ø> (ø) ⬆️
simulator/memory/elf/elf_loader.h 100% <ø> (ø) ⬆️
simulator/modules/writeback/writeback.h 100% <ø> (ø) ⬆️
simulator/func_sim/func_sim.h 100% <ø> (ø) ⬆️
simulator/modules/fetch/fetch.h 100% <100%> (ø) ⬆️
simulator/memory/hierarchied_memory.cpp 100% <100%> (ø) ⬆️
simulator/func_sim/instr_memory.h 93.33% <100%> (ø) ⬆️
simulator/modules/writeback/writeback.cpp 100% <100%> (ø) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3228c2e...243b856. Read the comment docs.

@@ -9,10 +9,11 @@
#include <elfio/elfio.hpp>
#include <memory/memory.h>

static void load_elf_section( FuncMemory* memory, const ELFIO::section& section, AddrDiff offset)
static void load_elf_section( FuncMemory& memory, const ELFIO::section& section, AddrDiff offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not use non-const lvalue references as parameters, as it is not clear whether object is mutated.

@pavelkryukov
Copy link
Member

Could you please describe where the circular dependencies occur in current code (w/o Kernel)?

{
// 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());
memory.memcpy_host_to_guest( section.get_address() + offset,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOLINTNEXTLINE is no longer effective, as reinterpret_cast is moved to the next line.
I think we need a separate statement to define a const Byte* pointer.

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

void ElfLoader::load_to( FuncMemory* memory) const
void ElfLoader::load_to( std::shared_ptr<FuncMemory> memory) const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guideline would be to use a raw pointer here, as we are not going to own Memory inside ElfLoader.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we may use const std::shared_ptr<FuncMemory>&. But reference to pointer is an awkward pattern...

@trexxet
Copy link
Contributor Author

trexxet commented Oct 25, 2018

where the circular dependencies occur in current code (w/o Kernel)?

As FuncMemory doesn't refer to Simulator, there are no current dependencies.
However, Simulator would have a pointer to Kernel, and MARS Kernel (not the base Kernel) would have a pointer to Simulator. But, FuncMemory seems not to refer to any of the same-level entities.
I think it's worth replacing weak_ptr<FuncMemory> with shared_ptr<FuncMemory>.

@pavelkryukov
Copy link
Member

I think it's worth replacing weak_ptr with shared_ptr.

Yes, that's what I would propose.

@@ -14,10 +14,10 @@
template<typename Instr>
class InstrMemory
{
FuncMemory* mem = nullptr;
std::weak_ptr<FuncMemory> mem;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's one is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault.

@@ -21,7 +21,7 @@ class Mem : public Log
using InstructionOutput = std::pair< RegisterUInt, RegisterUInt>;

private:
FuncMemory* memory = nullptr;
std::weak_ptr<FuncMemory> memory;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@pavelkryukov pavelkryukov merged commit b72ce57 into MIPT-ILab:master Oct 25, 2018
@trexxet trexxet deleted the sim_funcmem_shared_ptrs branch October 26, 2018 21:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants