2010-05-23 15:07:20 -06:00
|
|
|
/*
|
|
|
|
www.sourceforge.net/projects/dfhack
|
|
|
|
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2010-05-29 21:13:59 -06:00
|
|
|
#include "DFMemInfoManager.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFProcess.h"
|
|
|
|
#include "dfhack/DFProcessEnumerator.h"
|
|
|
|
#include "dfhack/DFError.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFContext.h"
|
|
|
|
#include "dfhack/DFContextManager.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
|
|
|
#include <shms.h>
|
|
|
|
#include <mod-core.h>
|
|
|
|
#include <mod-maps.h>
|
|
|
|
#include <mod-creature40d.h>
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "private/ContextShared.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
namespace DFHack
|
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
class ContextManager::Private
|
2010-05-23 15:07:20 -06:00
|
|
|
{
|
|
|
|
public:
|
2010-05-29 13:34:36 -06:00
|
|
|
Private(){};
|
|
|
|
~Private(){};
|
2010-05-23 15:07:20 -06:00
|
|
|
string xml; // path to xml
|
|
|
|
vector <Context *> contexts;
|
|
|
|
ProcessEnumerator * pEnum;
|
|
|
|
};
|
|
|
|
}
|
2010-05-29 13:34:36 -06:00
|
|
|
class DFHack::BadContexts::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private(){};
|
|
|
|
vector <Context *> bad;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BadContexts::BadContexts():d(new Private()){}
|
|
|
|
|
|
|
|
BadContexts::~BadContexts()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BadContexts::Contains(Process* p)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < d->bad.size(); i++)
|
|
|
|
{
|
|
|
|
if((d->bad[i])->getProcess() == p)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BadContexts::Contains(Context* c)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < d->bad.size(); i++)
|
|
|
|
{
|
|
|
|
if(d->bad[i] == c)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t BadContexts::size()
|
|
|
|
{
|
|
|
|
return d->bad.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BadContexts::clear()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < d->bad.size(); i++)
|
|
|
|
{
|
|
|
|
// delete both Process and Context!
|
|
|
|
// process has to be deleted after context, because Context does some
|
|
|
|
// cleanup on delete (detach, etc.)
|
|
|
|
Process * to_kill = d->bad[i]->getProcess();
|
|
|
|
delete d->bad[i];
|
|
|
|
delete to_kill;
|
|
|
|
}
|
|
|
|
d->bad.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BadContexts::push_back(Context* c)
|
|
|
|
{
|
|
|
|
if(c)
|
|
|
|
d->bad.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
Context * BadContexts::operator[](uint32_t index)
|
|
|
|
{
|
|
|
|
if(index < d->bad.size())
|
|
|
|
return d->bad[index];
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-23 15:07:20 -06:00
|
|
|
|
2010-05-29 13:34:36 -06:00
|
|
|
ContextManager::ContextManager (const string path_to_xml) : d (new Private())
|
2010-05-23 15:07:20 -06:00
|
|
|
{
|
|
|
|
d->xml = QUOT (MEMXML_DATA_PATH);
|
|
|
|
d->xml += "/";
|
|
|
|
d->xml += path_to_xml;
|
2010-05-29 13:34:36 -06:00
|
|
|
d->pEnum = new ProcessEnumerator(d->xml);
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ContextManager::~ContextManager()
|
|
|
|
{
|
|
|
|
purge();
|
2010-05-29 14:46:23 -06:00
|
|
|
// process enumerator has to be destroyed after we detach from processes
|
|
|
|
// because it tracks and destroys them
|
|
|
|
if(d->pEnum)
|
|
|
|
{
|
|
|
|
delete d->pEnum;
|
|
|
|
d->pEnum = 0;
|
|
|
|
}
|
2010-05-23 15:07:20 -06:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2010-05-29 13:34:36 -06:00
|
|
|
uint32_t ContextManager::Refresh( BadContexts* bad_contexts )
|
2010-05-23 15:07:20 -06:00
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
// handle expired processes, remove stale Contexts
|
2010-05-23 15:07:20 -06:00
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
BadProcesses expired;
|
|
|
|
// get new list od living and expired Process objects
|
|
|
|
d->pEnum->Refresh(&expired);
|
|
|
|
|
|
|
|
// scan expired, kill contexts if necessary
|
|
|
|
vector <Context*>::iterator it = d->contexts.begin();;
|
|
|
|
while(it != d->contexts.end())
|
|
|
|
{
|
|
|
|
Process * test = (*it)->getProcess();
|
|
|
|
if(expired.Contains(test))
|
|
|
|
{
|
|
|
|
// ok. we have an expired context here.
|
|
|
|
if(!bad_contexts)
|
|
|
|
{
|
|
|
|
// with nowhere to put the context, we have to destroy it
|
|
|
|
delete *it;
|
|
|
|
// stop tracking it and advance the iterator
|
|
|
|
it = d->contexts.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we stuff the context into bad_contexts
|
|
|
|
bad_contexts->push_back(*it);
|
|
|
|
// stop tracking it and advance the iterator
|
|
|
|
it = d->contexts.erase(it);
|
|
|
|
// remove process from the 'expired' container, it is tracked by bad_contexts now
|
|
|
|
// (which is responsible for freeing it).
|
|
|
|
expired.excise(test);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else it++; // not expired, just advance to next one
|
|
|
|
}
|
|
|
|
// no expired contexts are in the d->contexts vector now
|
|
|
|
// all processes remaining in 'expired' are now destroyed along with it
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
|
|
|
int numProcesses = d->pEnum->size();
|
2010-05-29 13:34:36 -06:00
|
|
|
int numContexts = d->contexts.size();
|
|
|
|
vector <Context *> newContexts;
|
|
|
|
// enumerate valid processes
|
2010-05-23 15:07:20 -06:00
|
|
|
for(int i = 0; i < numProcesses; i++)
|
|
|
|
{
|
2010-05-29 13:34:36 -06:00
|
|
|
Process * test = d->pEnum->operator[](i);
|
|
|
|
bool exists = false;
|
|
|
|
// scan context vector for this process
|
|
|
|
for(int j = 0; j < numContexts; j++)
|
|
|
|
{
|
|
|
|
if((d->contexts[j])->getProcess() == test)
|
|
|
|
{
|
|
|
|
// already have that one, skip
|
|
|
|
exists = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!exists)
|
|
|
|
{
|
|
|
|
// new process needs a new context
|
|
|
|
Context * c = new Context(d->pEnum->operator[](i));
|
|
|
|
newContexts.push_back(c);
|
|
|
|
}
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
2010-05-29 13:34:36 -06:00
|
|
|
d->contexts.insert(d->contexts.end(), newContexts.begin(), newContexts.end());
|
2010-05-24 17:49:13 -06:00
|
|
|
return d->contexts.size();
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
2010-05-29 13:34:36 -06:00
|
|
|
|
2010-05-23 15:07:20 -06:00
|
|
|
uint32_t ContextManager::size()
|
|
|
|
{
|
|
|
|
return d->contexts.size();
|
|
|
|
}
|
2010-05-29 14:46:23 -06:00
|
|
|
|
2010-05-23 15:07:20 -06:00
|
|
|
Context * ContextManager::operator[](uint32_t index)
|
|
|
|
{
|
|
|
|
if (index < d->contexts.size())
|
|
|
|
return d->contexts[index];
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-29 14:46:23 -06:00
|
|
|
|
2010-05-23 15:07:20 -06:00
|
|
|
Context * ContextManager::getSingleContext()
|
|
|
|
{
|
|
|
|
if(!d->contexts.size())
|
|
|
|
{
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
for(int i = 0; i < d->contexts.size();i++)
|
|
|
|
{
|
|
|
|
if(d->contexts[i]->isValid())
|
|
|
|
{
|
|
|
|
return d->contexts[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw DFHack::Error::NoProcess();
|
|
|
|
}
|
2010-05-29 14:46:23 -06:00
|
|
|
|
2010-05-23 15:07:20 -06:00
|
|
|
void ContextManager::purge(void)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < d->contexts.size();i++)
|
|
|
|
delete d->contexts[i];
|
|
|
|
d->contexts.clear();
|
2010-05-29 14:46:23 -06:00
|
|
|
d->pEnum->purge();
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|