Use std::unordered_multimaps in a few places.

This commit is contained in:
Unknown W. Brackets 2014-11-03 08:24:53 -08:00
parent 67a7205bdd
commit 9d86d3ca9b
4 changed files with 91 additions and 24 deletions

View File

@ -28,6 +28,15 @@
// - Serialization code for anything complex has to be manually written.
#include <map>
#ifdef IOS
#include <tr1/unordered_map>
namespace std {
using std::tr1::unordered_map;
using std::tr1::unordered_multimap;
}
#else
#include <unordered_map>
#endif
#include <deque>
#include <list>
#include <set>
@ -178,7 +187,29 @@ public:
}
template<class K, class T>
void DoMap(std::map<K, T> &x, T &default_val)
void Do(std::unordered_map<K, T *> &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<class K, class T>
void Do(std::unordered_map<K, T> &x)
{
T dv = T();
DoMap(x, dv);
}
template<class M>
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<K, T>::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<class K, class T>
void DoMultimap(std::multimap<K, T> &x, T &default_val)
void Do(std::unordered_multimap<K, T *> &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<class K, class T>
void Do(std::unordered_multimap<K, T> &x)
{
T dv = T();
DoMultimap(x, dv);
}
template<class M>
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<K, T>::iterator itr = x.begin();
typename M::iterator itr = x.begin();
while (number > 0)
{
Do(itr->first);

View File

@ -17,6 +17,15 @@
#include <algorithm>
#include <map>
#ifdef IOS
#include <tr1/unordered_map>
namespace std {
using std::tr1::unordered_map;
using std::tr1::unordered_multimap;
}
#else
#include <unordered_map>
#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<SceUID, SceUID> MutexMap;
typedef std::unordered_multimap<SceUID, SceUID> 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<MutexMap::iterator, MutexMap::iterator> 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

View File

@ -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<u32, int>(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<multimap<u32, int>::iterator, multimap<u32, int>::iterator> ppp;
// equal_range(b) returns pair<iterator,iterator> 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<u32, int>::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<multimap<u32, int>::iterator, multimap<u32, int>::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<u32, int>::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)

View File

@ -18,6 +18,15 @@
#pragma once
#include <map>
#ifdef IOS
#include <tr1/unordered_map>
namespace std {
using std::tr1::unordered_map;
using std::tr1::unordered_multimap;
}
#else
#include <unordered_map>
#endif
#include <vector>
#include <string>
@ -160,10 +169,10 @@ private:
MIPSState *mips_;
CodeBlock *codeBlock_;
JitBlock *blocks_;
std::multimap<u32, int> proxyBlockMap_;
std::unordered_multimap<u32, int> proxyBlockMap_;
int num_blocks_;
std::multimap<u32, int> links_to_;
std::unordered_multimap<u32, int> links_to_;
std::map<std::pair<u32,u32>, u32> block_map_; // (end_addr, start_addr) -> number
enum {