ENH: memory management for exprResultGlobals via objectRegistry

- replaces previous code that used an autoPtr to hold a singleton.

  In some circumstances this deletion would conflict with clearing
  the objectRegistry - leading to error messages on exit.

  Now store directly on the registry (similar to a MeshObject)
This commit is contained in:
Mark Olesen 2020-12-17 18:33:03 +01:00
parent 66865b9fbc
commit 8afed765be
8 changed files with 90 additions and 92 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -41,7 +41,7 @@ namespace Foam
{
namespace expressions
{
defineTypeNameAndDebug(exprResultDelayed, 0);
defineTypeName(exprResultDelayed);
addToRunTimeSelectionTable
(

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -106,12 +106,12 @@ protected:
public:
//- Runtime type information
TypeName("exprResultDelayed");
TypeNameNoDebug("exprResultDelayed");
// Constructors
//- Construct null
//- Default construct
exprResultDelayed();
//- Copy construct

View File

@ -36,16 +36,12 @@ namespace Foam
namespace expressions
{
defineTypeNameAndDebug(exprResultGlobals, 0);
defineTypeName(exprResultGlobals);
} // End namespace expressions
} // End namespace Foam
Foam::autoPtr<Foam::expressions::exprResultGlobals>
Foam::expressions::exprResultGlobals::singleton_;
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::expressions::exprResultGlobals::exprResultGlobals
@ -57,32 +53,28 @@ Foam::expressions::exprResultGlobals::exprResultGlobals
(
IOobject
(
"exprResultGlobals",
obr.time().timeName(),
"expressions",
exprResultGlobals::typeName,
obr.time().timeName(), // instance
"expressions", // local
obr.time(),
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE
IOobject::AUTO_WRITE,
true // register
)
),
variables_(),
timeIndex_(obr.time().timeIndex())
{
if (headerOk())
{
readData
(
readStream("exprResultGlobals", true)
readStream(exprResultGlobals::typeName, true)
);
}
}
Foam::expressions::exprResultGlobals::Table::Table()
:
HashPtrTable<exprResult>()
{}
Foam::expressions::exprResultGlobals::Table::Table(const Table& tbl)
:
HashPtrTable<exprResult>(tbl.capacity())
@ -120,6 +112,52 @@ void Foam::expressions::exprResultGlobals::reset()
}
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
Foam::expressions::exprResultGlobals&
Foam::expressions::exprResultGlobals::New
(
const objectRegistry& obr
)
{
typedef expressions::exprResultGlobals Type;
auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
if (!ptr)
{
ptr = new Type(obr);
ptr->store();
}
else if (ptr->timeIndex_ != obr.time().timeIndex())
{
// If time changes, reset variables
ptr->timeIndex_ = obr.time().timeIndex();
ptr->reset();
}
return *ptr;
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
bool Foam::expressions::exprResultGlobals::Delete(const objectRegistry& obr)
{
typedef expressions::exprResultGlobals Type;
auto* ptr = obr.time().getObjectPtr<Type>(Type::typeName);
if (ptr)
{
return obr.time().checkOut(ptr);
}
return false;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::expressions::exprResultGlobals::writeData(Ostream& os) const
@ -148,29 +186,6 @@ bool Foam::expressions::exprResultGlobals::readData(Istream& is)
}
Foam::expressions::exprResultGlobals&
Foam::expressions::exprResultGlobals::New
(
const objectRegistry& obr
)
{
if (!singleton_)
{
singleton_.reset(new exprResultGlobals(obr));
}
if (singleton_->timeIndex_ != obr.time().timeIndex())
{
// Time changes, reset variables
singleton_->timeIndex_ = obr.time().timeIndex();
singleton_->reset();
}
return *singleton_;
}
Foam::expressions::exprResultGlobals::Table&
Foam::expressions::exprResultGlobals::getNamespace(const word& name)
{
@ -187,11 +202,11 @@ Foam::expressions::exprResultGlobals::get
{
for (const word& scopeName : scopes)
{
const auto tableIter = variables_.find(scopeName);
const auto tableIter = variables_.cfind(scopeName);
if (tableIter.found())
{
const auto resultIter = (*tableIter).find(name);
const auto resultIter = (*tableIter).cfind(name);
if (resultIter.found())
{
@ -239,26 +254,6 @@ Foam::expressions::exprResultGlobals::addValue
}
Foam::expressions::exprResult&
Foam::expressions::exprResultGlobals::addValue
(
const word& name,
const word& scope,
autoPtr<exprResult>& value,
const bool overwrite
)
{
Table& tbl = getOrCreateScope(scope);
if (overwrite || !tbl.found(name))
{
tbl.set(name, value);
}
return *tbl[name];
}
Foam::expressions::exprResult&
Foam::expressions::exprResultGlobals::addValue
(
@ -272,7 +267,7 @@ Foam::expressions::exprResultGlobals::addValue
if (overwrite || !tbl.found(name))
{
tbl.set(name, value);
tbl.set(name, std::move(value));
}
return *tbl[name];

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,7 +28,12 @@ Class
Foam::expressions::exprResultGlobals
Description
A globally available registry of expression results
A globally available registry of expression results.
These are currently registered on Time (may change in the future).
Note
The storage mechanism is similar to a MeshObject, but always stores
on Time and flushes stale values according to the time index.
SourceFiles
exprResultGlobals.C
@ -69,10 +74,17 @@ public:
{
public:
Table();
//- Default construct
Table() = default;
//- Copy (clone) construct
Table(const Table& tbl);
//- Move construct
Table(Table&& tbl);
Table(Istream& is);
//- Read construct from stream
explicit Table(Istream& is);
};
@ -86,13 +98,10 @@ private:
//- The currently (or previously) used time-index
label timeIndex_;
//- Only one instance of this repository
static autoPtr<exprResultGlobals> singleton_;
// Private Member Functions
//- Construct
//- Construct on Time for registry
explicit exprResultGlobals(const objectRegistry& obr);
//- Reset all variables
@ -108,14 +117,17 @@ private:
public:
//- Runtime type information
TypeName("exprResultGlobals");
TypeNameNoDebug("exprResultGlobals");
// Constructors
// Selectors
//- Get the singleton
//- Static constructor for singleton
static exprResultGlobals& New(const objectRegistry& obr);
//- Static destructor for singleton
static bool Delete(const objectRegistry& obr);
//- Destructor
virtual ~exprResultGlobals() = default;
@ -143,15 +155,6 @@ public:
const bool overwrite = true
);
//- Add named result to specified scope
exprResult& addValue
(
const word& name,
const word& scope,
autoPtr<exprResult>& value,
const bool overwrite = true
);
//- Add named result to specified scope
exprResult& addValue
(

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,7 +35,7 @@ namespace Foam
{
namespace expressions
{
defineTypeNameAndDebug(exprResultStored, 0);
defineTypeName(exprResultStored);
addToRunTimeSelectionTable
(

View File

@ -84,7 +84,7 @@ protected:
public:
//- Runtime type information
TypeName("exprResultStored");
TypeNameNoDebug("exprResultStored");
// Constructors

View File

@ -36,7 +36,7 @@ namespace Foam
namespace expressions
{
defineTypeNameAndDebug(exprResultStoredStack,0);
defineTypeName(exprResultStoredStack);
addToRunTimeSelectionTable
(

View File

@ -66,7 +66,7 @@ protected:
public:
//- Runtime type information
TypeName("exprResultStoredStack");
TypeNameNoDebug("exprResultStoredStack");
// Constructors