From 9d86d3ca9bba6805f96b81eb18cbf2b9f0a5ec42 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Mon, 3 Nov 2014 08:24:53 -0800 Subject: [PATCH] Use std::unordered_multimaps in a few places. --- Common/ChunkFile.h | 73 +++++++++++++++++++++++---- Core/HLE/sceKernelMutex.cpp | 13 ++++- Core/MIPS/JitCommon/JitBlockCache.cpp | 16 +++--- Core/MIPS/JitCommon/JitBlockCache.h | 13 ++++- 4 files changed, 91 insertions(+), 24 deletions(-) diff --git a/Common/ChunkFile.h b/Common/ChunkFile.h index fa8efce3b..f347892f2 100644 --- a/Common/ChunkFile.h +++ b/Common/ChunkFile.h @@ -28,6 +28,15 @@ // - Serialization code for anything complex has to be manually written. #include +#ifdef IOS +#include +namespace std { + using std::tr1::unordered_map; + using std::tr1::unordered_multimap; +} +#else +#include +#endif #include #include #include @@ -178,7 +187,29 @@ public: } template - void DoMap(std::map &x, T &default_val) + void Do(std::unordered_map &x) + { + if (mode == MODE_READ) + { + for (auto it = x.begin(), end = x.end(); it != end; ++it) + { + if (it->second != NULL) + delete it->second; + } + } + T *dv = NULL; + DoMap(x, dv); + } + + template + void Do(std::unordered_map &x) + { + T dv = T(); + DoMap(x, dv); + } + + template + void DoMap(M &x, typename M::mapped_type &default_val) { unsigned int number = (unsigned int)x.size(); Do(number); @@ -188,9 +219,9 @@ public: x.clear(); while (number > 0) { - K first = K(); + typename M::key_type first = typename M::key_type(); Do(first); - T second = default_val; + typename M::mapped_type second = default_val; Do(second); x[first] = second; --number; @@ -201,10 +232,10 @@ public: case MODE_MEASURE: case MODE_VERIFY: { - typename std::map::iterator itr = x.begin(); + typename M::iterator itr = x.begin(); while (number > 0) { - K first = itr->first; + typename M::key_type first = itr->first; Do(first); Do(itr->second); --number; @@ -238,7 +269,29 @@ public: } template - void DoMultimap(std::multimap &x, T &default_val) + void Do(std::unordered_multimap &x) + { + if (mode == MODE_READ) + { + for (auto it = x.begin(), end = x.end(); it != end; ++it) + { + if (it->second != NULL) + delete it->second; + } + } + T *dv = NULL; + DoMultimap(x, dv); + } + + template + void Do(std::unordered_multimap &x) + { + T dv = T(); + DoMultimap(x, dv); + } + + template + void DoMultimap(M &x, typename M::mapped_type &default_val) { unsigned int number = (unsigned int)x.size(); Do(number); @@ -248,11 +301,11 @@ public: x.clear(); while (number > 0) { - K first = K(); + typename M::key_type first = typename M::key_type(); Do(first); - T second = default_val; + typename M::mapped_type second = default_val; Do(second); - x.insert(std::make_pair(first, second)); + x.emplace(first, second); --number; } } @@ -261,7 +314,7 @@ public: case MODE_MEASURE: case MODE_VERIFY: { - typename std::multimap::iterator itr = x.begin(); + typename M::iterator itr = x.begin(); while (number > 0) { Do(itr->first); diff --git a/Core/HLE/sceKernelMutex.cpp b/Core/HLE/sceKernelMutex.cpp index 77a2c857d..6beee42a7 100644 --- a/Core/HLE/sceKernelMutex.cpp +++ b/Core/HLE/sceKernelMutex.cpp @@ -17,6 +17,15 @@ #include #include +#ifdef IOS +#include +namespace std { + using std::tr1::unordered_map; + using std::tr1::unordered_multimap; +} +#else +#include +#endif #include "Common/ChunkFile.h" #include "Core/MemMap.h" #include "Core/HLE/HLE.h" @@ -155,7 +164,7 @@ struct LwMutex : public KernelObject static int mutexWaitTimer = -1; static int lwMutexWaitTimer = -1; // Thread -> Mutex locks for thread end. -typedef std::multimap MutexMap; +typedef std::unordered_multimap MutexMap; static MutexMap mutexHeldLocks; void __KernelMutexBeginCallback(SceUID threadID, SceUID prevCallbackId); @@ -204,7 +213,7 @@ void __KernelMutexShutdown() void __KernelMutexAcquireLock(Mutex *mutex, int count, SceUID thread) { #if defined(_DEBUG) - std::pair locked = mutexHeldLocks.equal_range(thread); + auto locked = mutexHeldLocks.equal_range(thread); for (MutexMap::iterator iter = locked.first; iter != locked.second; ++iter) _dbg_assert_msg_(SCEKERNEL, (*iter).second != mutex->GetUID(), "Thread %d / mutex %d wasn't removed from mutexHeldLocks properly.", thread, mutex->GetUID()); #endif diff --git a/Core/MIPS/JitCommon/JitBlockCache.cpp b/Core/MIPS/JitCommon/JitBlockCache.cpp index b1b23d5c3..e4458cfbf 100644 --- a/Core/MIPS/JitCommon/JitBlockCache.cpp +++ b/Core/MIPS/JitCommon/JitBlockCache.cpp @@ -200,7 +200,7 @@ void JitBlockCache::ProxyBlock(u32 rootAddress, u32 startAddress, u32 size, cons // Make binary searches and stuff work ok b.normalEntry = codePtr; b.checkedEntry = codePtr; - proxyBlockMap_.insert(std::make_pair(startAddress, num_blocks_)); + proxyBlockMap_.emplace(startAddress, num_blocks_); AddBlockMap(num_blocks_); num_blocks_++; //commit the current block @@ -253,7 +253,7 @@ void JitBlockCache::FinalizeBlock(int block_num, bool block_link) { if (block_link) { for (int i = 0; i < MAX_JIT_BLOCK_EXITS; i++) { if (b.exitAddress[i] != INVALID_EXIT) { - links_to_.insert(std::pair(b.exitAddress[i], block_num)); + links_to_.emplace(b.exitAddress[i], block_num); latestExit = std::max(latestExit, b.exitAddress[i]); } } @@ -439,29 +439,25 @@ void JitBlockCache::LinkBlockExits(int i) { } void JitBlockCache::LinkBlock(int i) { - using namespace std; LinkBlockExits(i); JitBlock &b = blocks_[i]; - pair::iterator, multimap::iterator> ppp; // equal_range(b) returns pair representing the range // of element with key b - ppp = links_to_.equal_range(b.originalAddress); + auto ppp = links_to_.equal_range(b.originalAddress); if (ppp.first == ppp.second) return; - for (multimap::iterator iter = ppp.first; iter != ppp.second; ++iter) { + for (auto iter = ppp.first; iter != ppp.second; ++iter) { // PanicAlert("Linking block %i to block %i", iter->second, i); LinkBlockExits(iter->second); } } void JitBlockCache::UnlinkBlock(int i) { - using namespace std; JitBlock &b = blocks_[i]; - pair::iterator, multimap::iterator> ppp; - ppp = links_to_.equal_range(b.originalAddress); + auto ppp = links_to_.equal_range(b.originalAddress); if (ppp.first == ppp.second) return; - for (multimap::iterator iter = ppp.first; iter != ppp.second; ++iter) { + for (auto iter = ppp.first; iter != ppp.second; ++iter) { JitBlock &sourceBlock = blocks_[iter->second]; for (int e = 0; e < MAX_JIT_BLOCK_EXITS; e++) { if (sourceBlock.exitAddress[e] == b.originalAddress) diff --git a/Core/MIPS/JitCommon/JitBlockCache.h b/Core/MIPS/JitCommon/JitBlockCache.h index a25fb75b7..a0e201dd6 100644 --- a/Core/MIPS/JitCommon/JitBlockCache.h +++ b/Core/MIPS/JitCommon/JitBlockCache.h @@ -18,6 +18,15 @@ #pragma once #include +#ifdef IOS +#include +namespace std { + using std::tr1::unordered_map; + using std::tr1::unordered_multimap; +} +#else +#include +#endif #include #include @@ -160,10 +169,10 @@ private: MIPSState *mips_; CodeBlock *codeBlock_; JitBlock *blocks_; - std::multimap proxyBlockMap_; + std::unordered_multimap proxyBlockMap_; int num_blocks_; - std::multimap links_to_; + std::unordered_multimap links_to_; std::map, u32> block_map_; // (end_addr, start_addr) -> number enum {