From bb596f55e38d2f420a4ebf9e0dcfcf407ffbd41d Mon Sep 17 00:00:00 2001 From: "Daniel R. Carvalho" Date: Wed, 2 Dec 2020 16:51:09 +0100 Subject: [PATCH] base-stats: Use smart pointer for info's storageParams Previously the storage params were not being deallocated. Make sure this happens by managing it with smart pointers. As a side effect, encapsulate this variable to facilitate future changes. Change-Id: I4c2496d08241f155793ed35e3463512d9ea06f83 Signed-off-by: Daniel R. Carvalho Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38178 Tested-by: kokoro Reviewed-by: Hoa Nguyen Reviewed-by: Jason Lowe-Power Maintainer: Jason Lowe-Power --- src/base/statistics.cc | 2 +- src/base/statistics.hh | 34 +++++++++++++++++----------------- src/base/stats/info.cc | 15 ++++++++++++++- src/base/stats/info.hh | 14 ++++++++++++-- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/base/statistics.cc b/src/base/statistics.cc index c4782f617b..39e76b6dac 100644 --- a/src/base/statistics.cc +++ b/src/base/statistics.cc @@ -99,7 +99,7 @@ InfoAccess::setInfo(Group *parent, Info *info) void InfoAccess::setParams(const StorageParams *params) { - info()->storageParams = params; + info()->setStorageParams(params); } void diff --git a/src/base/statistics.hh b/src/base/statistics.hh index 1885954d9a..18f52cb580 100644 --- a/src/base/statistics.hh +++ b/src/base/statistics.hh @@ -440,7 +440,7 @@ class DataWrapVec : public DataWrap size_t size = self.size(); for (off_type i = 0; i < size; ++i) - self.data(i)->prepare(info->storageParams); + self.data(i)->prepare(info->getStorageParams()); } void @@ -451,7 +451,7 @@ class DataWrapVec : public DataWrap size_t size = self.size(); for (off_type i = 0; i < size; ++i) - self.data(i)->reset(info->storageParams); + self.data(i)->reset(info->getStorageParams()); } }; @@ -551,7 +551,7 @@ class ScalarBase : public DataWrap void doInit() { - new (storage) Storage(this->info()->storageParams); + new (storage) Storage(this->info()->getStorageParams()); this->setInit(); } @@ -624,8 +624,8 @@ class ScalarBase : public DataWrap bool zero() const { return result() == 0.0; } - void reset() { data()->reset(this->info()->storageParams); } - void prepare() { data()->prepare(this->info()->storageParams); } + void reset() { data()->reset(this->info()->getStorageParams()); } + void prepare() { data()->prepare(this->info()->getStorageParams()); } }; class ProxyInfo : public ScalarInfo @@ -952,7 +952,7 @@ class VectorBase : public DataWrapVec fatal_if(s <= 0, "Storage size must be positive"); fatal_if(check(), "Stat has already been initialized"); - storage.resize(s, new Storage(this->info()->storageParams)); + storage.resize(s, new Storage(this->info()->getStorageParams())); this->setInit(); } @@ -1178,7 +1178,7 @@ class Vector2dBase : public DataWrapVec2d info->x = _x; info->y = _y; - storage.resize(x * y, new Storage(info->storageParams)); + storage.resize(x * y, new Storage(info->getStorageParams())); this->setInit(); return self; @@ -1225,7 +1225,7 @@ class Vector2dBase : public DataWrapVec2d size_type size = this->size(); for (off_type i = 0; i < size; ++i) - data(i)->prepare(info->storageParams); + data(i)->prepare(info->getStorageParams()); info->cvec.resize(size); for (off_type i = 0; i < size; ++i) @@ -1241,7 +1241,7 @@ class Vector2dBase : public DataWrapVec2d Info *info = this->info(); size_type size = this->size(); for (off_type i = 0; i < size; ++i) - data(i)->reset(info->storageParams); + data(i)->reset(info->getStorageParams()); } bool @@ -1297,7 +1297,7 @@ class DistBase : public DataWrap void doInit() { - new (storage) Storage(this->info()->storageParams); + new (storage) Storage(this->info()->getStorageParams()); this->setInit(); } @@ -1333,7 +1333,7 @@ class DistBase : public DataWrap prepare() { Info *info = this->info(); - data()->prepare(info->storageParams, info->data); + data()->prepare(info->getStorageParams(), info->data); } /** @@ -1342,7 +1342,7 @@ class DistBase : public DataWrap void reset() { - data()->reset(this->info()->storageParams); + data()->reset(this->info()->getStorageParams()); } /** @@ -1387,7 +1387,7 @@ class VectorDistBase : public DataWrapVec fatal_if(s <= 0, "Storage size must be positive"); fatal_if(check(), "Stat has already been initialized"); - storage.resize(s, new Storage(this->info()->storageParams)); + storage.resize(s, new Storage(this->info()->getStorageParams())); this->setInit(); } @@ -1434,7 +1434,7 @@ class VectorDistBase : public DataWrapVec size_type size = this->size(); info->data.resize(size); for (off_type i = 0; i < size; ++i) - data(i)->prepare(info->storageParams, info->data[i]); + data(i)->prepare(info->getStorageParams(), info->data[i]); } bool @@ -2431,7 +2431,7 @@ class SparseHistBase : public DataWrap void doInit() { - new (storage) Storage(this->info()->storageParams); + new (storage) Storage(this->info()->getStorageParams()); this->setInit(); } @@ -2467,7 +2467,7 @@ class SparseHistBase : public DataWrap prepare() { Info *info = this->info(); - data()->prepare(info->storageParams, info->data); + data()->prepare(info->getStorageParams(), info->data); } /** @@ -2476,7 +2476,7 @@ class SparseHistBase : public DataWrap void reset() { - data()->reset(this->info()->storageParams); + data()->reset(this->info()->getStorageParams()); } }; diff --git a/src/base/stats/info.cc b/src/base/stats/info.cc index edc4290d15..c40b55918e 100644 --- a/src/base/stats/info.cc +++ b/src/base/stats/info.cc @@ -46,6 +46,7 @@ #include "base/cprintf.hh" #include "base/debug.hh" #include "base/logging.hh" +#include "base/stats/storage.hh" #include "base/str.hh" namespace gem5 @@ -69,7 +70,7 @@ nameMap() } Info::Info() - : flags(none), precision(-1), prereq(0), storageParams(NULL) + : flags(none), precision(-1), prereq(0), storageParams() { id = id_count++; if (debug_break_id >= 0 and debug_break_id == id) @@ -80,6 +81,18 @@ Info::~Info() { } +StorageParams const* +Info::getStorageParams() const +{ + return storageParams.get(); +} + +void +Info::setStorageParams(const StorageParams *const params) +{ + return storageParams.reset(params); +} + bool validateStatName(const std::string &name) { diff --git a/src/base/stats/info.hh b/src/base/stats/info.hh index 70e2e2c05e..9a5e2e77ab 100644 --- a/src/base/stats/info.hh +++ b/src/base/stats/info.hh @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -100,8 +101,8 @@ class Info static int id_count; int id; - public: - const StorageParams *storageParams; + private: + std::unique_ptr storageParams; public: Info(); @@ -118,6 +119,15 @@ class Info void setSeparator(std::string _sep) { separatorString = _sep;} + /** + * Getter for the storage params. These parameters should only be modified + * using the respective setter. + * @sa setStorageParams + */ + StorageParams const* getStorageParams() const; + /** Setter for the storage params. */ + void setStorageParams(const StorageParams *const params); + /** * Check that this stat has been set up properly and is ready for * use