From 9634200c1041eca1c7ac0ce25d79bb8d961530f6 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 6 Feb 2019 20:03:57 +0100 Subject: Resolve some memory leaks found by Valgrind. Signed-off-by: Pavel Pisa --- qtmips_gui/cacheview.cpp | 9 +++++---- qtmips_gui/memoryview.cpp | 1 + qtmips_machine/cache.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++--- qtmips_machine/cache.h | 1 + qtmips_machine/core.cpp | 3 ++- qtmips_machine/memory.cpp | 10 ++++++++-- 6 files changed, 63 insertions(+), 10 deletions(-) diff --git a/qtmips_gui/cacheview.cpp b/qtmips_gui/cacheview.cpp index 24b8d7b..d279083 100644 --- a/qtmips_gui/cacheview.cpp +++ b/qtmips_gui/cacheview.cpp @@ -210,11 +210,12 @@ CacheViewBlock::CacheViewBlock(const machine::Cache *cache, unsigned block , boo } CacheViewBlock::~CacheViewBlock() { - delete validity; - delete dirty; - delete tag; + delete[] validity; + if (dirty != nullptr) + delete[] dirty; + delete[] tag; for (unsigned y = 0; y < rows; y++) - delete data[y]; + delete[] data[y]; delete data; } diff --git a/qtmips_gui/memoryview.cpp b/qtmips_gui/memoryview.cpp index 9bc0b0c..0457633 100644 --- a/qtmips_gui/memoryview.cpp +++ b/qtmips_gui/memoryview.cpp @@ -47,6 +47,7 @@ MemoryView::MemoryView(QWidget *parent, std::uint32_t addr0) : QWidget(parent) { memory = nullptr; addr_0 = addr0; + change_counter = 0; layout = new QVBoxLayout(this); diff --git a/qtmips_machine/cache.cpp b/qtmips_machine/cache.cpp index e358986..6a00c27 100644 --- a/qtmips_machine/cache.cpp +++ b/qtmips_machine/cache.cpp @@ -48,6 +48,10 @@ Cache::Cache(MemoryAccess *m, const MachineConfigCache *cc, unsigned memory_acc hit_write = 0; miss_read = 0; miss_write = 0; + dt = nullptr; + replc.lfu = nullptr; + replc.lru = nullptr; + // Skip any other initialization if cache is disabled if (!cc->enabled()) return; @@ -58,6 +62,7 @@ Cache::Cache(MemoryAccess *m, const MachineConfigCache *cc, unsigned memory_acc dt[i] = new cache_data[cc->sets()]; for (unsigned y = 0; y < cc->sets(); y++) { dt[i][y].valid = false; + dt[i][y].dirty = false; dt[i][y].data = new std::uint32_t[cc->blocks()]; } } @@ -65,18 +70,56 @@ Cache::Cache(MemoryAccess *m, const MachineConfigCache *cc, unsigned memory_acc switch (cnf.replacement_policy()) { case MachineConfigCache::RP_LFU: replc.lfu = new unsigned *[cnf.sets()]; - for (unsigned row = 0; row < cnf.sets(); row++) + for (unsigned row = 0; row < cnf.sets(); row++) { replc.lfu[row] = new unsigned[cnf.associativity()]; + for (int i = 0; i < cnf.associativity(); i++) + replc.lfu[row][i] = 0; + } break; case MachineConfigCache::RP_LRU: replc.lru = new time_t*[cnf.sets()]; - for (unsigned row = 0; row < cnf.sets(); row++) + for (unsigned row = 0; row < cnf.sets(); row++) { replc.lru[row] = new time_t[cnf.associativity()]; + for (int i = 0; i < cnf.associativity(); i++) + replc.lru[row][i] = 0; + } default: break; } } +Cache::~Cache(){ + if (dt != nullptr) { + for (unsigned i = 0; i < cnf.associativity(); i++) { + if (dt[i]) { + for (unsigned y = 0; y < cnf.sets(); y++) + if (dt[i][y].data != nullptr) + delete[] dt[i][y].data; + delete[] dt[i]; + } + } + delete[] dt; + } + switch (cnf.replacement_policy()) { + case MachineConfigCache::RP_LFU: + if (replc.lfu == nullptr) + break; + for (unsigned row = 0; row < cnf.sets(); row++) + delete[] replc.lfu[row]; + delete [] replc.lfu; + break; + case MachineConfigCache::RP_LRU: + if (replc.lru == nullptr) + break; + for (unsigned row = 0; row < cnf.sets(); row++) + delete[] replc.lru[row]; + delete[] replc.lru; + default: + break; + } +} + + bool Cache::wword(std::uint32_t address, std::uint32_t value) { bool changed; @@ -222,7 +265,7 @@ bool Cache::access(std::uint32_t address, std::uint32_t *data, bool write, std:: struct cache_data &cd = dt[indx][row]; // Verify if we are not replacing - if (cd.tag != tag && cd.valid) + if (cd.valid && cd.tag != tag) kick(indx, row); // Update statistics and otherwise read from memory diff --git a/qtmips_machine/cache.h b/qtmips_machine/cache.h index bb089fe..d4df317 100644 --- a/qtmips_machine/cache.h +++ b/qtmips_machine/cache.h @@ -47,6 +47,7 @@ class Cache : public MemoryAccess { Q_OBJECT public: Cache(MemoryAccess *m, const MachineConfigCache *c, unsigned memory_access_penalty_r = 1, unsigned memory_access_penalty_w = 1); + ~Cache(); bool wword(std::uint32_t address, std::uint32_t value); std::uint32_t rword(std::uint32_t address) const; diff --git a/qtmips_machine/core.cpp b/qtmips_machine/core.cpp index 3118d01..fb3db5c 100644 --- a/qtmips_machine/core.cpp +++ b/qtmips_machine/core.cpp @@ -233,7 +233,7 @@ void Core::writeback(const struct dtMemory &dt) { } void Core::handle_pc(const struct dtDecode &dt) { - bool branch; + bool branch = false; emit instruction_program_counter(dt.inst); if (dt.jump) { @@ -289,6 +289,7 @@ void Core::dtDecodeInit(struct dtDecode &dt) { dt.forward_m_d_rs = false; dt.forward_m_d_rt = false; dt.aluop = ALU_OP_SLL; + dt.memctl = AC_NONE; dt.val_rs = 0; dt.val_rt = 0; dt.rwrite = 0; diff --git a/qtmips_machine/memory.cpp b/qtmips_machine/memory.cpp index 338835d..5aa65ee 100644 --- a/qtmips_machine/memory.cpp +++ b/qtmips_machine/memory.cpp @@ -133,7 +133,7 @@ MemorySection::MemorySection(const MemorySection &ms) : MemorySection(ms.length( } MemorySection::~MemorySection() { - delete this->dt; + delete[] this->dt; } bool MemorySection::wword(std::uint32_t offset, std::uint32_t value) { @@ -205,14 +205,18 @@ Memory::Memory() { Memory::Memory(const Memory &m) { this->mt_root = copy_section_tree(m.get_memorytree_root(), 0); + change_counter = 0; + write_counter = 0; } Memory::~Memory() { free_section_tree(this->mt_root, 0); + delete[] this->mt_root; } void Memory::reset() { free_section_tree(this->mt_root, 0); + delete[] this->mt_root; this->mt_root = allocate_section_tree(); } @@ -283,8 +287,10 @@ union machine::MemoryTree *Memory::allocate_section_tree() { void Memory::free_section_tree(union machine::MemoryTree *mt, size_t depth) { if (depth < (MEMORY_TREE_DEPTH - 1)) { // Following level is memory tree for (int i = 0; i < MEMORY_TREE_ROW_SIZE; i++) { - if (mt[i].mt != nullptr) + if (mt[i].mt != nullptr) { free_section_tree(mt[i].mt, depth + 1); + delete[] mt[i].mt; + } } } else { // Following level is memory section for (int i = 0; i < MEMORY_TREE_ROW_SIZE; i++) { -- cgit v1.2.3