From b863b55594c6a55c8b444a39d62406a1566cf358 Mon Sep 17 00:00:00 2001 From: stephena Date: Mon, 10 Nov 2014 16:13:28 +0000 Subject: [PATCH] Some more optimizations and small code cleanups. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3066 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- src/cheat/CheatManager.cxx | 26 ++++++++++++-------------- src/cheat/CheatManager.hxx | 3 +-- src/common/PNGLibrary.cxx | 6 ++---- src/debugger/CartDebug.cxx | 14 +++++++------- src/debugger/DebuggerParser.cxx | 18 +++++++++--------- src/emucore/EventHandler.cxx | 5 ++--- src/emucore/System.cxx | 9 +-------- src/emucore/System.hxx | 8 ++++---- 8 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/cheat/CheatManager.cxx b/src/cheat/CheatManager.cxx index e6abb36ad..3f9536954 100644 --- a/src/cheat/CheatManager.cxx +++ b/src/cheat/CheatManager.cxx @@ -128,13 +128,11 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatManager::addOneShot(const string& name, const string& code) { - Cheat* cheat = createCheat(name, code); - if(!cheat) - return; + unique_ptr cheat(createCheat(name, code)); // Evaluate this cheat once, and then immediately delete it - cheat->evaluate(); - delete cheat; + if(cheat) + cheat->evaluate(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -218,14 +216,14 @@ void CheatManager::parse(const string& cheats) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void CheatManager::enable(const string& code, bool enable) { - for(unsigned int i = 0; i < myCheatList.size(); i++) + for(const auto& cheat: myCheatList) { - if(myCheatList[i]->code() == code) + if(cheat->code() == code) { if(enable) - myCheatList[i]->enable(); + cheat->enable(); else - myCheatList[i]->disable(); + cheat->disable(); break; } } @@ -327,7 +325,7 @@ void CheatManager::saveCheats(const string& md5sum) // Only update the list if absolutely necessary if(changed) { - CheatCodeMap::iterator iter = myCheatMap.find(md5sum); + auto iter = myCheatMap.find(md5sum); // Erase old entry and add a new one only if it's changed if(iter != myCheatMap.end()) @@ -350,16 +348,16 @@ void CheatManager::clear() // the following loop myPerFrameList.clear(); - for(unsigned int i = 0; i < myCheatList.size(); i++) - delete myCheatList[i]; + for(auto cheat: myCheatList) + delete cheat; myCheatList.clear(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool CheatManager::isValidCode(const string& code) const { - for(unsigned int i = 0; i < code.size(); i++) - if(!isxdigit(code[i])) + for(auto c: code) + if(!isxdigit(c)) return false; uInt32 length = (uInt32)code.length(); diff --git a/src/cheat/CheatManager.hxx b/src/cheat/CheatManager.hxx index 0b393c901..ee7d9d537 100644 --- a/src/cheat/CheatManager.hxx +++ b/src/cheat/CheatManager.hxx @@ -28,7 +28,6 @@ class OSystem; #include "bspf.hxx" typedef vector CheatList; -typedef map CheatCodeMap; /** This class provides an interface for performing all cheat operations @@ -156,7 +155,7 @@ class CheatManager CheatList myCheatList; CheatList myPerFrameList; - CheatCodeMap myCheatMap; + map myCheatMap; string myCheatFile; // This is set each time a new cheat/ROM is loaded, for later diff --git a/src/common/PNGLibrary.cxx b/src/common/PNGLibrary.cxx index 4f2844c7b..09330be58 100644 --- a/src/common/PNGLibrary.cxx +++ b/src/common/PNGLibrary.cxx @@ -326,15 +326,13 @@ void PNGLibrary::writeComments(png_structp png_ptr, png_infop info_ptr, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void PNGLibrary::png_read_data(png_structp ctx, png_bytep area, png_size_t size) { - ifstream* stream = (ifstream *) png_get_io_ptr(ctx); - stream->read((char *)area, size); + ((ifstream *) png_get_io_ptr(ctx))->read((char *)area, size); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void PNGLibrary::png_write_data(png_structp ctx, png_bytep area, png_size_t size) { - ofstream* stream = (ofstream *) png_get_io_ptr(ctx); - stream->write((const char *)area, size); + ((ofstream *) png_get_io_ptr(ctx))->write((const char *)area, size); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/debugger/CartDebug.cxx b/src/debugger/CartDebug.cxx index 29f461e77..3da2ffc26 100644 --- a/src/debugger/CartDebug.cxx +++ b/src/debugger/CartDebug.cxx @@ -268,7 +268,7 @@ bool CartDebug::disassemble(bool force) if(bankChanged || !pcfound) { AddressList::const_iterator i; - for(i = addresses.begin(); i != addresses.end(); ++i) + for(auto i = addresses.begin(); i != addresses.end(); ++i) { if(PC < *i) { @@ -537,11 +537,11 @@ bool CartDebug::addLabel(const string& label, uInt16 address) bool CartDebug::removeLabel(const string& label) { // Only user-defined labels can be removed - LabelToAddr::iterator iter = myUserAddresses.find(label); + const auto& iter = myUserAddresses.find(label); if(iter != myUserAddresses.end()) { // Erase the address assigned to the label - AddrToLabel::iterator iter2 = myUserLabels.find(iter->second); + const auto& iter2 = myUserLabels.find(iter->second); if(iter2 != myUserLabels.end()) myUserLabels.erase(iter2); @@ -612,8 +612,8 @@ bool CartDebug::getLabel(ostream& buf, uInt16 addr, bool isRead, int places) con { // RAM can use user-defined labels; otherwise we default to // standard mnemonics - AddrToLabel::const_iterator iter; - if((iter = myUserLabels.find(addr)) != myUserLabels.end()) + auto iter = myUserLabels.find(addr); + if(iter != myUserLabels.end()) { buf << iter->second; } @@ -634,8 +634,8 @@ bool CartDebug::getLabel(ostream& buf, uInt16 addr, bool isRead, int places) con case ADDR_ROM: { // These addresses can never be in the system labels list - AddrToLabel::const_iterator iter; - if((iter = myUserLabels.find(addr)) != myUserLabels.end()) + const auto& iter = myUserLabels.find(addr); + if(iter != myUserLabels.end()) { buf << iter->second; return true; diff --git a/src/debugger/DebuggerParser.cxx b/src/debugger/DebuggerParser.cxx index fe51257b8..0e54335ec 100644 --- a/src/debugger/DebuggerParser.cxx +++ b/src/debugger/DebuggerParser.cxx @@ -403,15 +403,15 @@ bool DebuggerParser::getArgs(const string& command, string& verb) } */ - for(int i = 0; i < argCount; i++) { - int err = YaccParser::parse(argStrings[i].c_str()); - if(err) { - args.push_back(-1); - } else { - Expression* e = YaccParser::getResult(); - args.push_back( e->evaluate() ); - delete e; + for(int i = 0; i < argCount; i++) + { + if(!YaccParser::parse(argStrings[i].c_str())) + { + unique_ptr expr(YaccParser::getResult()); + args.push_back(expr->evaluate()); } + else + args.push_back(-1); } return true; @@ -422,7 +422,7 @@ bool DebuggerParser::validateArgs(int cmd) { // cerr << "entering validateArgs(" << cmd << ")" << endl; bool required = commands[cmd].parmsRequired; - parameters *p = commands[cmd].parms; + parameters* p = commands[cmd].parms; if(argCount == 0) { diff --git a/src/emucore/EventHandler.cxx b/src/emucore/EventHandler.cxx index 99eda2c02..23f6fc8ba 100644 --- a/src/emucore/EventHandler.cxx +++ b/src/emucore/EventHandler.cxx @@ -220,9 +220,8 @@ void EventHandler::poll(uInt64 time) #endif { #ifdef CHEATCODE_SUPPORT - const CheatList& cheats = myOSystem.cheat().perFrame(); - for(uInt32 i = 0; i < cheats.size(); i++) - cheats[i]->evaluate(); + for(auto& cheat: myOSystem.cheat().perFrame()) + cheat->evaluate(); #endif // Handle continuous snapshots diff --git a/src/emucore/System.cxx b/src/emucore/System.cxx index 57c0fe3b0..eb2c489cb 100644 --- a/src/emucore/System.cxx +++ b/src/emucore/System.cxx @@ -43,10 +43,6 @@ System::System(const OSystem& osystem, M6502& m6502, M6532& m6532, // Re-initialize random generator randGenerator().initSeed(); - // Allocate page table and dirty list - myPageAccessTable = new PageAccess[NUM_PAGES]; - myPageIsDirtyTable = new bool[NUM_PAGES]; - // Initialize page access table PageAccess access(&myNullDevice, System::PA_READ); for(int page = 0; page < NUM_PAGES; ++page) @@ -62,9 +58,6 @@ System::System(const OSystem& osystem, M6502& m6502, M6532& m6532, // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - System::~System() { - // Free my page access table and dirty list - delete[] myPageAccessTable; - delete[] myPageIsDirtyTable; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -218,7 +211,7 @@ void System::poke(uInt16 addr, uInt8 value) uInt8 System::getAccessFlags(uInt16 addr) const { #ifdef DEBUGGER_SUPPORT - PageAccess& access = myPageAccessTable[(addr & ADDRESS_MASK) >> PAGE_SHIFT]; + const PageAccess& access = myPageAccessTable[(addr & ADDRESS_MASK) >> PAGE_SHIFT]; if(access.codeAccessBase) return *(access.codeAccessBase + (addr & PAGE_MASK)); diff --git a/src/emucore/System.hxx b/src/emucore/System.hxx index f30104bca..d8468809f 100644 --- a/src/emucore/System.hxx +++ b/src/emucore/System.hxx @@ -396,11 +396,11 @@ class System : public Serializable // Null device to use for page which are not installed NullDevice myNullDevice; - // Pointer to a dynamically allocated array of PageAccess structures - PageAccess* myPageAccessTable; + // The list of PageAccess structures + PageAccess myPageAccessTable[NUM_PAGES]; - // Pointer to a dynamically allocated array for dirty pages - bool* myPageIsDirtyTable; + // The list of dirty pages + bool myPageIsDirtyTable[NUM_PAGES]; // The current state of the Data Bus uInt8 myDataBusState;