systemc: Implement sc_gen_unique_name.

The Accellera implementation statically allocates the buffer it uses to
build the unique names and only allocates the name generator if it's
going to be used for a particular module. I assume that's to avoid
allocating a large buffer if it's not going to be used.

In this implementation, I use an std::string which manages its own
memory and so shouldn't need to be selectively allocated. I also use a
string stream to construct the name instead of sprintf.

Change-Id: If92c68586a85b5d27c067a75a6e9ebbf00d8c785
Reviewed-on: https://gem5-review.googlesource.com/12066
Reviewed-by: Gabe Black <gabeblack@google.com>
Maintainer: Gabe Black <gabeblack@google.com>
This commit is contained in:
Gabe Black
2018-08-07 01:23:01 -07:00
parent e07c2d3c6e
commit c18695fa7d
2 changed files with 30 additions and 3 deletions

View File

@@ -31,7 +31,10 @@
#define __SYSTEMC_CORE_MODULE_HH__
#include <cassert>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include "systemc/core/object.hh"
#include "systemc/ext/core/sc_module.hh"
@@ -39,6 +42,23 @@
namespace sc_gem5
{
class UniqueNameGen
{
private:
std::map<std::string, int> counts;
std::string buf;
public:
const char *
gen(std::string seed)
{
std::ostringstream os;
os << seed << "_" << counts[seed]++;
buf = os.str();
return buf.c_str();
}
};
class Module
{
private:
@@ -46,6 +66,8 @@ class Module
sc_core::sc_module *_sc_mod;
Object *_obj;
UniqueNameGen nameGen;
public:
Module(const char *name);
@@ -77,6 +99,8 @@ class Module
}
void pop();
const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
};
Module *currentModule();

View File

@@ -64,6 +64,8 @@ newCThreadProcess(const char *name, ProcessFuncWrapper *func)
return p;
}
UniqueNameGen nameGen;
} // namespace sc_gem5
namespace sc_core
@@ -643,10 +645,11 @@ at_negedge(const sc_signal_in_if<sc_dt::sc_logic> &)
}
const char *
sc_gen_unique_name(const char *)
sc_gen_unique_name(const char *seed)
{
warn("%s not implemented.\n", __PRETTY_FUNCTION__);
return "";
::sc_gem5::Module *mod = ::sc_gem5::currentModule();
return mod ? mod->uniqueName(seed) :
::sc_gem5::nameGen.gen(seed);
}
bool