Skip to content

Commit

Permalink
disassembler: Make the disassembler a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Feb 7, 2017
1 parent c16d010 commit f1c8ffa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/include/disassembler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ namespace kcov
uint64_t offset) = 0;


static IDisassembler *create();
static IDisassembler &getInstance();
};
}
10 changes: 5 additions & 5 deletions src/parsers/bfd-disassembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ class BfdDisassembler : public IDisassembler
SectionCache_t m_cache;
};

IDisassembler *IDisassembler::create()
IDisassembler &IDisassembler::getInstance()
{
static bool g_bfdInited;
static BfdDisassembler *g_p;

if (!g_bfdInited) {
if (!g_p) {
bfd_init();
g_bfdInited = true;
g_p = new BfdDisassembler();
}

return new BfdDisassembler();
return *g_p;
}
9 changes: 7 additions & 2 deletions src/parsers/dummy-disassembler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class DummyDisassembler : public IDisassembler
}
};

IDisassembler *IDisassembler::create()
IDisassembler &IDisassembler::getInstance()
{
return new DummyDisassembler();
static DummyDisassembler *g_p;

if (!g_p)
g_p = new DummyDisassembler();

return *g_p;
}
11 changes: 5 additions & 6 deletions src/parsers/elf-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ typedef std::vector<Segment> SegmentList_t;
class ElfInstance : public IFileParser, IFileParser::ILineListener
{
public:
ElfInstance()
ElfInstance() :
m_addressVerifier(IDisassembler::getInstance())
{
m_elf = NULL;
m_addressVerifier = IDisassembler::create();
m_filename = "";
m_checksum = 0;
m_elfIs32Bit = true;
Expand All @@ -142,7 +142,6 @@ class ElfInstance : public IFileParser, IFileParser::ILineListener

virtual ~ElfInstance()
{
delete m_addressVerifier;
}

uint64_t getChecksum()
Expand Down Expand Up @@ -451,7 +450,7 @@ class ElfInstance : public IFileParser, IFileParser::ILineListener
return false;
}

m_addressVerifier->setup(fileData, EI_NIDENT);
m_addressVerifier.setup(fileData, EI_NIDENT);

if (elf_getshdrstrndx(m_elf, &shstrndx) < 0) {
error("elf_getshstrndx failed on %s\n", m_filename.c_str());
Expand Down Expand Up @@ -647,7 +646,7 @@ class ElfInstance : public IFileParser, IFileParser::ILineListener
if (m_verifyAddresses) {
uint64_t offset = addr - it->getBase();

out = m_addressVerifier->verify(it->getData(),it->getSize(), offset);
out = m_addressVerifier.verify(it->getData(),it->getSize(), offset);

if (!out) {
kcov_debug(ELF_MSG, "kcov: Address 0x%llx is not at an instruction boundary, skipping\n",
Expand Down Expand Up @@ -808,7 +807,7 @@ class ElfInstance : public IFileParser, IFileParser::ILineListener
SegmentList_t m_executableSegments;
FileList_t m_gcnoFiles;

IDisassembler *m_addressVerifier;
IDisassembler &m_addressVerifier;
bool m_verifyAddresses;
struct Elf *m_elf;
bool m_elfIs32Bit;
Expand Down

0 comments on commit f1c8ffa

Please sign in to comment.