rpcsx/rpcs3/Emu/ARMv7/PSVObjectList.h

129 lines
2.9 KiB
C
Raw Normal View History

2015-01-26 01:38:50 +03:00
#pragma once
2015-01-26 09:13:11 +03:00
union psv_uid_t
2015-01-26 01:38:50 +03:00
{
// true UID format is partially unknown
s32 uid;
struct
{
u32 oddness : 1; // always 1 for UIDs (to not mess it up with addresses)
u32 number : 15; // ID from 0 to 2^15-1
u32 type : 15; // UID class (psv_object_class_t)
u32 sign : 1; // UIDs are positive, error codes are negative
};
2015-01-26 09:13:11 +03:00
static psv_uid_t make(s32 uid)
2015-01-26 01:38:50 +03:00
{
2015-01-26 09:13:11 +03:00
psv_uid_t result;
2015-01-26 01:38:50 +03:00
result.uid = uid;
return result;
}
};
template<typename T, u32 type>
class psv_object_list_t // Class for managing object data
{
std::array<std::shared_ptr<T>, 0x8000> m_data;
std::atomic<u32> m_hint; // guessing next free position
2015-01-26 15:55:26 +03:00
std::mutex m_mutex; // TODO: remove it when shared_ptr atomic ops are fully available
2015-01-26 01:38:50 +03:00
public:
psv_object_list_t() : m_hint(0) {}
psv_object_list_t(const psv_object_list_t&) = delete;
psv_object_list_t(psv_object_list_t&&) = delete;
psv_object_list_t& operator =(const psv_object_list_t&) = delete;
psv_object_list_t& operator =(psv_object_list_t&&) = delete;
2015-01-26 01:38:50 +03:00
public:
static const u32 uid_class = type;
// check if UID is potentially valid (will return true if the object doesn't exist)
bool check(s32 uid)
{
2015-01-26 09:13:11 +03:00
const psv_uid_t id = psv_uid_t::make(uid);
2015-01-26 01:38:50 +03:00
// check sign bit, uid class and ensure that value is odd
return !id.sign && id.type == uid_class && id.oddness == 1;
}
// share object with UID specified (will return empty pointer if the object doesn't exist or the UID is invalid)
std::shared_ptr<T> find(s32 uid)
{
if (!check(uid))
{
return nullptr;
}
2015-01-26 09:13:11 +03:00
return m_data[psv_uid_t::make(uid).number];
2015-01-26 01:38:50 +03:00
}
std::shared_ptr<T> operator [](s32 uid)
{
return find(uid);
}
// generate UID for newly created object (will return zero if the limit exceeded)
s32 add(std::shared_ptr<T>& data)
{
2015-01-26 15:55:26 +03:00
std::lock_guard<std::mutex> lock(m_mutex);
for (u32 i = 0, j = m_hint % m_data.size(); i < m_data.size(); i++, j = (j + 1) % m_data.size())
2015-01-26 01:38:50 +03:00
{
// find an empty position and copy the pointer
if (!m_data[j])
2015-01-26 01:38:50 +03:00
{
m_data[j] = data;
m_hint = j + 1; // guess next position
2015-01-26 09:13:11 +03:00
psv_uid_t id = psv_uid_t::make(1); // odd number
2015-01-26 01:38:50 +03:00
id.type = uid_class; // set type
id.number = j; // set position
data->on_init(id.uid); // save UID
return id.uid; // return UID
2015-01-26 01:38:50 +03:00
}
}
return 0;
}
// remove object with UID specified and share it for the last time (will return empty pointer if the object doesn't exists or the UID is invalid)
std::shared_ptr<T> remove(s32 uid)
{
if (!check(uid))
{
return nullptr;
}
const u32 pos = psv_uid_t::make(uid).number;
2015-01-26 15:55:26 +03:00
std::lock_guard<std::mutex> lock(m_mutex);
std::shared_ptr<T> old_ptr = nullptr;
m_data[pos].swap(old_ptr);
m_hint = pos;
2015-01-26 15:55:26 +03:00
return old_ptr;
2015-01-26 01:38:50 +03:00
}
// remove all objects
void clear()
{
2015-01-26 15:55:26 +03:00
std::lock_guard<std::mutex> lock(m_mutex);
for (auto& object : m_data)
2015-01-26 01:38:50 +03:00
{
if (object)
{
object->on_stop();
}
object = nullptr;
2015-01-26 01:38:50 +03:00
}
m_hint = 0;
2015-01-26 01:38:50 +03:00
}
};
2015-01-26 15:55:26 +03:00
void clear_all_psv_objects();