From: Gabe Black Date: Wed, 21 Oct 2020 04:11:53 +0000 (-0700) Subject: sim: Generalize the arrayParamOut and arrayParamIn functions. X-Git-Tag: develop-gem5-snapshot~585 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=edccff8f23e2d5e5f04cb548c296b399ff06c6f8;p=gem5.git sim: Generalize the arrayParamOut and arrayParamIn functions. These had been written specifically for the vector, list, set, and C style array types. This change reworks them to share an implementation, and to work with more general types. The arrayParamOut method requires std::begin() and std::end() to accept that type, and the arrayParamIn method requires either insert or push_back, or the type to be an array. Also fix up a couple of files which accidentally depended on includes in the serialize headers which are no longer necessary. Change-Id: I6ec4fe3bb900603bbb4e35c4efa620c249942452 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/36277 Reviewed-by: Andreas Sandberg Maintainer: Andreas Sandberg Tested-by: kokoro --- diff --git a/src/sim/fd_array.hh b/src/sim/fd_array.hh index 5b9265d37..529556647 100644 --- a/src/sim/fd_array.hh +++ b/src/sim/fd_array.hh @@ -35,6 +35,7 @@ #define __FD_ARRAY_HH__ #include +#include #include #include diff --git a/src/sim/serialize.cc b/src/sim/serialize.cc index 5aa933d2e..e502d9aa7 100644 --- a/src/sim/serialize.cc +++ b/src/sim/serialize.cc @@ -49,6 +49,7 @@ #include #include #include +#include #include #include diff --git a/src/sim/serialize.hh b/src/sim/serialize.hh index 6c06eeb25..22c40aac0 100644 --- a/src/sim/serialize.hh +++ b/src/sim/serialize.hh @@ -48,10 +48,9 @@ #include #include -#include -#include +#include #include -#include +#include #include #include "base/bitunion.hh" @@ -524,18 +523,18 @@ paramIn(CheckpointIn &cp, const std::string &name, T ¶m) /** * @ingroup api_serialize */ -template +template void arrayParamOut(CheckpointOut &os, const std::string &name, - const std::vector ¶m) + InputIterator start, InputIterator end) { - typename std::vector::size_type size = param.size(); os << name << "="; - if (size > 0) - showParam(os, param[0]); - for (typename std::vector::size_type i = 1; i < size; ++i) { + auto it = start; + if (it != end) + showParam(os, *it++); + while (it != end) { os << " "; - showParam(os, param[i]); + showParam(os, *it++); } os << "\n"; } @@ -544,45 +543,14 @@ arrayParamOut(CheckpointOut &os, const std::string &name, * @ingroup api_serialize */ template -void +decltype(std::begin(std::declval()), + std::end(std::declval()), void()) arrayParamOut(CheckpointOut &os, const std::string &name, - const std::list ¶m) + const T ¶m) { - typename std::list::const_iterator it = param.begin(); - - os << name << "="; - if (param.size() > 0) - showParam(os, *it); - it++; - while (it != param.end()) { - os << " "; - showParam(os, *it); - it++; - } - os << "\n"; + arrayParamOut(os, name, std::begin(param), std::end(param)); } -/** - * @ingroup api_serialize - */ -template -void -arrayParamOut(CheckpointOut &os, const std::string &name, - const std::set ¶m) -{ - typename std::set::const_iterator it = param.begin(); - - os << name << "="; - if (param.size() > 0) - showParam(os, *it); - it++; - while (it != param.end()) { - os << " "; - showParam(os, *it); - it++; - } - os << "\n"; -} /** * @ingroup api_serialize @@ -592,14 +560,7 @@ void arrayParamOut(CheckpointOut &os, const std::string &name, const T *param, unsigned size) { - os << name << "="; - if (size > 0) - showParam(os, param[0]); - for (unsigned i = 1; i < size; ++i) { - os << " "; - showParam(os, param[i]); - } - os << "\n"; + arrayParamOut(os, name, param, param + size); } /** @@ -613,48 +574,28 @@ arrayParamOut(CheckpointOut &os, const std::string &name, * * @ingroup api_serialize */ -template + +template void arrayParamIn(CheckpointIn &cp, const std::string &name, - T *param, unsigned size) + InsertIterator inserter, ssize_t fixed_size=-1) { - const std::string §ion(Serializable::currentSection()); + const std::string §ion = Serializable::currentSection(); std::string str; - if (!cp.find(section, name, str)) { - fatal("Can't unserialize '%s:%s'\n", section, name); - } - - // code below stolen from VectorParam::parse(). - // it would be nice to unify these somehow... + fatal_if(!cp.find(section, name, str), + "Can't unserialize '%s:%s'.", section, name); std::vector tokens; - tokenize(tokens, str, ' '); - // Need this if we were doing a vector - // value.resize(tokens.size()); - - fatal_if(tokens.size() != size, + fatal_if(fixed_size >= 0 && tokens.size() != fixed_size, "Array size mismatch on %s:%s (Got %u, expected %u)'\n", - section, name, tokens.size(), size); - - for (std::vector::size_type i = 0; i < tokens.size(); i++) { - // need to parse into local variable to handle vector, - // for which operator[] returns a special reference class - // that's not the same as 'bool&', (since it's a packed - // vector) - T scalar_value; - if (!parseParam(tokens[i], scalar_value)) { - std::string err("could not parse \""); - - err += str; - err += "\""; - - fatal(err); - } + section, name, tokens.size(), fixed_size); - // assign parsed value to vector - param[i] = scalar_value; + for (const auto &token: tokens) { + T value; + fatal_if(!parseParam(token, value), "Could not parse \"%s\".", str); + *inserter = value; } } @@ -662,78 +603,25 @@ arrayParamIn(CheckpointIn &cp, const std::string &name, * @ingroup api_serialize */ template -void -arrayParamIn(CheckpointIn &cp, const std::string &name, std::vector ¶m) +decltype(std::declval().insert(std::declval()), + void()) +arrayParamIn(CheckpointIn &cp, const std::string &name, T ¶m) { - const std::string §ion(Serializable::currentSection()); - std::string str; - if (!cp.find(section, name, str)) { - fatal("Can't unserialize '%s:%s'\n", section, name); - } - - // code below stolen from VectorParam::parse(). - // it would be nice to unify these somehow... - - std::vector tokens; - - tokenize(tokens, str, ' '); - - // Need this if we were doing a vector - // value.resize(tokens.size()); - - param.resize(tokens.size()); - - for (std::vector::size_type i = 0; i < tokens.size(); i++) { - // need to parse into local variable to handle vector, - // for which operator[] returns a special reference class - // that's not the same as 'bool&', (since it's a packed - // vector) - T scalar_value; - if (!parseParam(tokens[i], scalar_value)) { - std::string err("could not parse \""); - - err += str; - err += "\""; - - fatal(err); - } - - // assign parsed value to vector - param[i] = scalar_value; - } + param.clear(); + arrayParamIn( + cp, name, std::inserter(param, param.begin())); } /** * @ingroup api_serialize */ template -void -arrayParamIn(CheckpointIn &cp, const std::string &name, std::list ¶m) +decltype(std::declval().push_back(std::declval()), + void()) +arrayParamIn(CheckpointIn &cp, const std::string &name, T ¶m) { - const std::string §ion(Serializable::currentSection()); - std::string str; - if (!cp.find(section, name, str)) { - fatal("Can't unserialize '%s:%s'\n", section, name); - } param.clear(); - - std::vector tokens; - tokenize(tokens, str, ' '); - - for (std::vector::size_type i = 0; i < tokens.size(); i++) { - T scalar_value; - if (!parseParam(tokens[i], scalar_value)) { - std::string err("could not parse \""); - - err += str; - err += "\""; - - fatal(err); - } - - // assign parsed value to vector - param.push_back(scalar_value); - } + arrayParamIn(cp, name, std::back_inserter(param)); } /** @@ -741,32 +629,16 @@ arrayParamIn(CheckpointIn &cp, const std::string &name, std::list ¶m) */ template void -arrayParamIn(CheckpointIn &cp, const std::string &name, std::set ¶m) +arrayParamIn(CheckpointIn &cp, const std::string &name, + T *param, unsigned size) { - const std::string §ion(Serializable::currentSection()); - std::string str; - if (!cp.find(section, name, str)) { - fatal("Can't unserialize '%s:%s'\n", section, name); - } - param.clear(); - - std::vector tokens; - tokenize(tokens, str, ' '); + struct ArrayInserter + { + T *data; + T &operator *() { return *data++; } + } insert_it{param}; - for (std::vector::size_type i = 0; i < tokens.size(); i++) { - T scalar_value; - if (!parseParam(tokens[i], scalar_value)) { - std::string err("could not parse \""); - - err += str; - err += "\""; - - fatal(err); - } - - // assign parsed value to vector - param.insert(scalar_value); - } + arrayParamIn(cp, name, insert_it, size); } void diff --git a/src/sim/system.hh b/src/sim/system.hh index 4b4461686..4954af68f 100644 --- a/src/sim/system.hh +++ b/src/sim/system.hh @@ -42,6 +42,7 @@ #ifndef __SYSTEM_HH__ #define __SYSTEM_HH__ +#include #include #include #include