More usage of smart pointers throughout core and version info.

develop
Stoyan Gaydarov 2018-07-08 15:50:14 -07:00
parent 699f864110
commit 6f90273bb6
10 changed files with 56 additions and 53 deletions

@ -1532,10 +1532,7 @@ Core::Core() :
{ {
// init the console. This must be always the first step! // init the console. This must be always the first step!
plug_mgr = 0; plug_mgr = 0;
vif = 0;
p = 0;
errorstate = false; errorstate = false;
vinfo = 0;
started = false; started = false;
memset(&(s_mods), 0, sizeof(s_mods)); memset(&(s_mods), 0, sizeof(s_mods));
@ -1614,24 +1611,24 @@ bool Core::Init()
#else #else
const char * path = "hack\\symbols.xml"; const char * path = "hack\\symbols.xml";
#endif #endif
vif = new DFHack::VersionInfoFactory(); auto local_vif = dts::make_unique<DFHack::VersionInfoFactory>();
cerr << "Identifying DF version.\n"; cerr << "Identifying DF version.\n";
try try
{ {
vif->loadFile(path); local_vif->loadFile(path);
} }
catch(Error::All & err) catch(Error::All & err)
{ {
std::stringstream out; std::stringstream out;
out << "Error while reading symbols.xml:\n"; out << "Error while reading symbols.xml:\n";
out << err.what() << std::endl; out << err.what() << std::endl;
delete vif;
vif = NULL;
errorstate = true; errorstate = true;
fatal(out.str()); fatal(out.str());
return false; return false;
} }
std::unique_ptr<DFHack::Process> local_p(new DFHack::Process(vif)); vif = std::move(local_vif);
auto local_p = dts::make_unique<DFHack::Process>(*vif);
local_p->ValidateDescriptionOS();
vinfo = local_p->getDescriptor(); vinfo = local_p->getDescriptor();
if(!vinfo || !local_p->isIdentified()) if(!vinfo || !local_p->isIdentified())
@ -1670,18 +1667,6 @@ bool Core::Init()
cerr << "Version: " << vinfo->getVersion() << endl; cerr << "Version: " << vinfo->getVersion() << endl;
p = std::move(local_p); p = std::move(local_p);
#if defined(_WIN32)
const OSType expected = OS_WINDOWS;
#elif defined(_DARWIN)
const OSType expected = OS_APPLE;
#else
const OSType expected = OS_LINUX;
#endif
if (expected != vinfo->getOS()) {
cerr << "OS mismatch; resetting to " << int(expected) << endl;
vinfo->setOS(expected);
}
// Init global object pointers // Init global object pointers
df::global::InitGlobals(); df::global::InitGlobals();

@ -17,7 +17,7 @@ namespace {
} }
#define INIT_GLOBAL_FUNCTION_PREFIX \ #define INIT_GLOBAL_FUNCTION_PREFIX \
DFHack::VersionInfo *global_table_ = DFHack::Core::getInstance().vinfo; \ DFHack::VersionInfo *global_table_ = DFHack::Core::getInstance().vinfo.get(); \
void * tmp_; void * tmp_;
#define INIT_GLOBAL_FUNCTION_ITEM(type,name) \ #define INIT_GLOBAL_FUNCTION_ITEM(type,name) \

@ -42,14 +42,13 @@ using namespace std;
#include <md5wrapper.h> #include <md5wrapper.h>
#include "MemAccess.h" #include "MemAccess.h"
#include "Memory.h" #include "Memory.h"
#include "MiscUtils.h"
#include "VersionInfoFactory.h" #include "VersionInfoFactory.h"
#include "VersionInfo.h" #include "VersionInfo.h"
#include "Error.h" #include "Error.h"
#include <string.h> #include <string.h>
using namespace DFHack; using namespace DFHack;
Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe(0) Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0)
{ {
int target_result; int target_result;
@ -67,10 +66,10 @@ Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe
// get hash of the running DF process // get hash of the running DF process
my_md5 = md5.getHashFromFile(real_path, length, (char *) first_kb); my_md5 = md5.getHashFromFile(real_path, length, (char *) first_kb);
// create linux process, add it to the vector // create linux process, add it to the vector
const VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); auto vinfo = known_versions.getVersionInfoByMD5(my_md5);
if(vinfo) if(vinfo)
{ {
my_descriptor = dts::make_unique<VersionInfo>(*vinfo); my_descriptor = std::make_shared<VersionInfo>(*vinfo);
identified = true; identified = true;
} }
else else

@ -40,14 +40,13 @@ using namespace std;
#include <md5wrapper.h> #include <md5wrapper.h>
#include "MemAccess.h" #include "MemAccess.h"
#include "Memory.h" #include "Memory.h"
#include "MiscUtils.h"
#include "VersionInfoFactory.h" #include "VersionInfoFactory.h"
#include "VersionInfo.h" #include "VersionInfo.h"
#include "Error.h" #include "Error.h"
#include <string.h> #include <string.h>
using namespace DFHack; using namespace DFHack;
Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe(0) Process::Process(const VersionInfoFactory& known_versions) : identified(false), my_pe(0)
{ {
const char * dir_name = "/proc/self/"; const char * dir_name = "/proc/self/";
const char * exe_link_name = "/proc/self/exe"; const char * exe_link_name = "/proc/self/exe";
@ -71,10 +70,10 @@ Process::Process(VersionInfoFactory * known_versions) : identified(false), my_pe
// get hash of the running DF process // get hash of the running DF process
my_md5 = md5.getHashFromFile(self_exe_name, length, (char *) first_kb); my_md5 = md5.getHashFromFile(self_exe_name, length, (char *) first_kb);
// create linux process, add it to the vector // create linux process, add it to the vector
const VersionInfo * vinfo = known_versions->getVersionInfoByMD5(my_md5); auto vinfo = known_versions.getVersionInfoByMD5(my_md5);
if(vinfo) if(vinfo)
{ {
my_descriptor = dts::make_unique<VersionInfo>(*vinfo); my_descriptor = std::make_shared<VersionInfo>(*vinfo);
identified = true; identified = true;
} }
else else

@ -44,7 +44,6 @@ using namespace std;
#include "Error.h" #include "Error.h"
#include "MemAccess.h" #include "MemAccess.h"
#include "Memory.h" #include "Memory.h"
#include "MiscUtils.h"
using namespace DFHack; using namespace DFHack;
namespace DFHack namespace DFHack
{ {
@ -63,7 +62,7 @@ namespace DFHack
char * base; char * base;
}; };
} }
Process::Process(VersionInfoFactory * factory) : identified(false) Process::Process(const VersionInfoFactory& factory) : identified(false)
{ {
HMODULE hmod = NULL; HMODULE hmod = NULL;
DWORD needed; DWORD needed;
@ -96,12 +95,12 @@ Process::Process(VersionInfoFactory * factory) : identified(false)
return; return;
} }
my_pe = d->pe_header.FileHeader.TimeDateStamp; my_pe = d->pe_header.FileHeader.TimeDateStamp;
const VersionInfo* vinfo = factory->getVersionInfoByPETimestamp(my_pe); auto vinfo = factory.getVersionInfoByPETimestamp(my_pe);
if(vinfo) if(vinfo)
{ {
identified = true; identified = true;
// give the process a data model and memory layout fixed for the base of first module // give the process a data model and memory layout fixed for the base of first module
my_descriptor = dts::make_unique<VersionInfo>(*vinfo); my_descriptor = std::make_shared<VersionInfo>(*vinfo);
my_descriptor->rebaseTo(getBase()); my_descriptor->rebaseTo(getBase());
} }
else else

@ -56,24 +56,24 @@ void VersionInfoFactory::clear()
error = false; error = false;
} }
const VersionInfo * VersionInfoFactory::getVersionInfoByMD5(string hash) std::shared_ptr<const VersionInfo> VersionInfoFactory::getVersionInfoByMD5(string hash) const
{ {
for(size_t i = 0; i < versions.size();i++) for (const auto& version : versions)
{ {
if(versions[i]->hasMD5(hash)) if(version->hasMD5(hash))
return versions[i].get(); return version;
} }
return 0; return nullptr;
} }
const VersionInfo * VersionInfoFactory::getVersionInfoByPETimestamp(uintptr_t timestamp) std::shared_ptr<const VersionInfo> VersionInfoFactory::getVersionInfoByPETimestamp(uintptr_t timestamp) const
{ {
for(size_t i = 0; i < versions.size();i++) for (const auto& version : versions)
{ {
if(versions[i]->hasPE(timestamp)) if(version->hasPE(timestamp))
return versions[i].get(); return version;
} }
return 0; return nullptr;
} }
void VersionInfoFactory::ParseVersion (TiXmlElement* entry, VersionInfo* mem) void VersionInfoFactory::ParseVersion (TiXmlElement* entry, VersionInfo* mem)

@ -192,7 +192,7 @@ namespace DFHack
DFHack::Console &getConsole() { return con; } DFHack::Console &getConsole() { return con; }
std::unique_ptr<DFHack::Process> p; std::unique_ptr<DFHack::Process> p;
DFHack::VersionInfo * vinfo; std::shared_ptr<DFHack::VersionInfo> vinfo;
DFHack::Windows::df_window * screen_window; DFHack::Windows::df_window * screen_window;
static void print(const char *format, ...) Wformat(printf,1,2); static void print(const char *format, ...) Wformat(printf,1,2);
@ -235,7 +235,7 @@ namespace DFHack
struct Cond; struct Cond;
// FIXME: shouldn't be kept around like this // FIXME: shouldn't be kept around like this
DFHack::VersionInfoFactory * vif; std::unique_ptr<DFHack::VersionInfoFactory> vif;
// Module storage // Module storage
struct struct
{ {

@ -35,9 +35,10 @@ distribution.
#include <map> #include <map>
#include <memory> #include <memory>
#include "VersionInfo.h"
namespace DFHack namespace DFHack
{ {
struct VersionInfo;
class Process; class Process;
//class Window; //class Window;
class DFVector; class DFVector;
@ -79,7 +80,7 @@ namespace DFHack
{ {
public: public:
/// this is the single most important destructor ever. ~px /// this is the single most important destructor ever. ~px
Process(VersionInfoFactory * known_versions); Process(const VersionInfoFactory& known_versions);
~Process(); ~Process();
/// read a 8-byte integer /// read a 8-byte integer
uint64_t readQuad(const void * address) uint64_t readQuad(const void * address)
@ -247,10 +248,15 @@ namespace DFHack
void getMemRanges(std::vector<t_memrange> & ranges ); void getMemRanges(std::vector<t_memrange> & ranges );
/// get the symbol table extension of this process /// get the symbol table extension of this process
VersionInfo *getDescriptor() std::shared_ptr<DFHack::VersionInfo> getDescriptor()
{ {
return my_descriptor.get(); return my_descriptor;
};
void ValidateDescriptionOS() {
my_descriptor->ValidateOS();
}; };
uintptr_t getBase(); uintptr_t getBase();
/// get the DF Process ID /// get the DF Process ID
int getPID(); int getPID();
@ -292,7 +298,7 @@ namespace DFHack
std::string getMD5() { return my_md5; } std::string getMD5() { return my_md5; }
private: private:
std::unique_ptr<VersionInfo> my_descriptor; std::shared_ptr<VersionInfo> my_descriptor;
PlatformSpecific *d; PlatformSpecific *d;
bool identified; bool identified;
uint32_t my_pid; uint32_t my_pid;

@ -26,6 +26,7 @@ distribution.
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <iostream>
#include <map> #include <map>
#include <sys/types.h> #include <sys/types.h>
#include <vector> #include <vector>
@ -169,5 +170,19 @@ namespace DFHack
{ {
return OS; return OS;
}; };
void ValidateOS() {
#if defined(_WIN32)
const OSType expected = OS_WINDOWS;
#elif defined(_DARWIN)
const OSType expected = OS_APPLE;
#else
const OSType expected = OS_LINUX;
#endif
if (expected != getOS()) {
std::cerr << "OS mismatch; resetting to " << int(expected) << std::endl;
setOS(expected);
}
}
}; };
} }

@ -41,12 +41,12 @@ namespace DFHack
~VersionInfoFactory(); ~VersionInfoFactory();
bool loadFile( std::string path_to_xml); bool loadFile( std::string path_to_xml);
bool isInErrorState() const {return error;}; bool isInErrorState() const {return error;};
const VersionInfo * getVersionInfoByMD5(std::string md5string); std::shared_ptr<const VersionInfo> getVersionInfoByMD5(std::string md5string) const;
const VersionInfo * getVersionInfoByPETimestamp(uintptr_t timestamp); std::shared_ptr<const VersionInfo> getVersionInfoByPETimestamp(uintptr_t timestamp) const;
std::vector<std::shared_ptr<const VersionInfo>> versions;
// trash existing list // trash existing list
void clear(); void clear();
private: private:
std::vector<std::shared_ptr<const VersionInfo>> versions;
void ParseVersion (TiXmlElement* version, VersionInfo* mem); void ParseVersion (TiXmlElement* version, VersionInfo* mem);
bool error; bool error;
}; };