diff --git a/src/include/disassembler.hh b/src/include/disassembler.hh index cc46970c..e8be98f8 100644 --- a/src/include/disassembler.hh +++ b/src/include/disassembler.hh @@ -26,6 +26,6 @@ namespace kcov uint64_t offset) = 0; - static IDisassembler *create(); + static IDisassembler &getInstance(); }; } diff --git a/src/parsers/bfd-disassembler.cc b/src/parsers/bfd-disassembler.cc index 596c0c42..611815d1 100644 --- a/src/parsers/bfd-disassembler.cc +++ b/src/parsers/bfd-disassembler.cc @@ -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; } diff --git a/src/parsers/dummy-disassembler.cc b/src/parsers/dummy-disassembler.cc index 9ade3aed..312011e4 100644 --- a/src/parsers/dummy-disassembler.cc +++ b/src/parsers/dummy-disassembler.cc @@ -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; } diff --git a/src/parsers/elf-parser.cc b/src/parsers/elf-parser.cc index b77fcf1d..3b83d395 100644 --- a/src/parsers/elf-parser.cc +++ b/src/parsers/elf-parser.cc @@ -121,10 +121,10 @@ typedef std::vector 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; @@ -142,7 +142,6 @@ class ElfInstance : public IFileParser, IFileParser::ILineListener virtual ~ElfInstance() { - delete m_addressVerifier; } uint64_t getChecksum() @@ -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()); @@ -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", @@ -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;