libstrat: convert to experimental new (super-accurate) sf allocation semantics

This commit is contained in:
Michael Scire
2021-01-17 07:55:32 -08:00
committed by SciresM
parent 8314d015f3
commit f06de12bea
149 changed files with 2852 additions and 1746 deletions

View File

@@ -23,21 +23,19 @@ namespace ams::i2c {
constinit int g_initialize_count = 0;
constinit os::SdkMutex g_i2c_mutex;
std::shared_ptr<sf::IManager> g_i2c_manager;
ams::sf::SharedPointer<sf::IManager> g_i2c_manager;
constinit int g_i2c_count = 0;
constinit os::SdkMutex g_i2c_pcv_mutex;
std::shared_ptr<sf::IManager> g_i2c_pcv_manager;
ams::sf::SharedPointer<sf::IManager> g_i2c_pcv_manager;
constinit int g_i2c_pcv_count = 0;
using InternalSession = std::shared_ptr<i2c::sf::ISession>;
InternalSession &GetInterface(const I2cSession &session) {
i2c::sf::ISession *GetInterface(const I2cSession &session) {
AMS_ASSERT(session._session != nullptr);
return *static_cast<InternalSession *>(session._session);
return static_cast<i2c::sf::ISession *>(session._session);
}
std::shared_ptr<sf::IManager> GetManager(DeviceCode device_code) {
ams::sf::SharedPointer<sf::IManager> GetManager(DeviceCode device_code) {
if (IsPowerBusDeviceCode(device_code)) {
return g_i2c_pcv_manager;
} else {
@@ -47,7 +45,7 @@ namespace ams::i2c {
}
void InitializeWith(std::shared_ptr<i2c::sf::IManager> &&sp, std::shared_ptr<i2c::sf::IManager> &&sp_pcv) {
void InitializeWith(ams::sf::SharedPointer<i2c::sf::IManager> sp, ams::sf::SharedPointer<i2c::sf::IManager> sp_pcv) {
std::scoped_lock lk(g_init_mutex);
AMS_ABORT_UNLESS(g_initialize_count == 0);
@@ -86,7 +84,7 @@ namespace ams::i2c {
AMS_ASSERT(g_i2c_count > 0);
if (g_i2c_count > 0) {
if ((--g_i2c_count) == 0) {
g_i2c_manager.reset();
g_i2c_manager = nullptr;
}
}
}
@@ -95,7 +93,7 @@ namespace ams::i2c {
AMS_ASSERT(g_i2c_pcv_count > 0);
if (g_i2c_pcv_count > 0) {
if ((--g_i2c_pcv_count) == 0) {
g_i2c_pcv_manager.reset();
g_i2c_manager = nullptr;
}
}
}
@@ -103,36 +101,29 @@ namespace ams::i2c {
}
Result OpenSession(I2cSession *out, DeviceCode device_code) {
/* Allocate the session. */
InternalSession *internal_session = new (std::nothrow) InternalSession;
AMS_ABORT_UNLESS(internal_session != nullptr);
auto session_guard = SCOPE_GUARD { delete internal_session; };
/* Get manager for the device. */
auto manager = GetManager(device_code);
/* Get the session. */
ams::sf::SharedPointer<i2c::sf::ISession> session;
{
ams::sf::cmif::ServiceObjectHolder object_holder;
if (hos::GetVersion() >= hos::Version_6_0_0) {
R_TRY(manager->OpenSession2(std::addressof(object_holder), device_code));
R_TRY(manager->OpenSession2(std::addressof(session), device_code));
} else {
R_TRY(manager->OpenSession(std::addressof(object_holder), ConvertToI2cDevice(device_code)));
R_TRY(manager->OpenSession(std::addressof(session), ConvertToI2cDevice(device_code)));
}
*internal_session = object_holder.GetServiceObject<sf::ISession>();
}
/* Set output. */
out->_session = internal_session;
out->_session = session.Detach();
/* We succeeded. */
session_guard.Cancel();
return ResultSuccess();
}
void CloseSession(I2cSession &session) {
/* Close the session. */
delete std::addressof(GetInterface(session));
ams::sf::ReleaseSharedObject(GetInterface(session));
session._session = nullptr;
}

View File

@@ -20,27 +20,17 @@ namespace ams::i2c::server {
namespace {
ManagerImpl g_manager_impl;
ManagerImpl g_pcv_manager_impl;
std::shared_ptr<i2c::sf::IManager> GetManagerServiceObject() {
static std::shared_ptr<i2c::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<i2c::sf::IManager>(g_manager_impl);
return s_sp;
}
std::shared_ptr<i2c::sf::IManager> GetManagerServiceObjectPowerBus() {
static std::shared_ptr<i2c::sf::IManager> s_sp = ams::sf::GetSharedPointerTo<i2c::sf::IManager>(g_pcv_manager_impl);
return s_sp;
}
ams::sf::UnmanagedServiceObject<i2c::sf::IManager, i2c::server::ManagerImpl> g_manager_impl;
ams::sf::UnmanagedServiceObject<i2c::sf::IManager, i2c::server::ManagerImpl> g_pcv_manager_impl;
}
std::shared_ptr<i2c::sf::IManager> GetServiceObject() {
return GetManagerServiceObject();
ams::sf::SharedPointer<i2c::sf::IManager> GetServiceObject() {
return g_manager_impl.GetShared();
}
std::shared_ptr<i2c::sf::IManager> GetServiceObjectPowerBus() {
return GetManagerServiceObjectPowerBus();
ams::sf::SharedPointer<i2c::sf::IManager> GetServiceObjectPowerBus() {
return g_pcv_manager_impl.GetShared();
}
}

View File

@@ -18,21 +18,21 @@
namespace ams::i2c::server {
ManagerImpl::ManagerImpl() : session_memory_resource(), allocator(std::addressof(session_memory_resource)) {
ManagerImpl::ManagerImpl() {
this->heap_handle = lmem::CreateExpHeap(this->heap_buffer, sizeof(this->heap_buffer), lmem::CreateOption_None);
this->session_memory_resource.Attach(this->heap_handle);
this->allocator.Attach(this->heap_handle);
}
ManagerImpl::~ManagerImpl() {
lmem::DestroyExpHeap(this->heap_handle);
}
Result ManagerImpl::OpenSessionForDev(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode) {
Result ManagerImpl::OpenSessionForDev(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode) {
/* TODO */
AMS_ABORT();
}
Result ManagerImpl::OpenSession(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, i2c::I2cDevice device) {
Result ManagerImpl::OpenSession(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, i2c::I2cDevice device) {
return this->OpenSession2(out, ConvertToDeviceCode(device));
}
@@ -46,15 +46,15 @@ namespace ams::i2c::server {
AMS_ABORT();
}
Result ManagerImpl::OpenSession2(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, DeviceCode device_code) {
Result ManagerImpl::OpenSession2(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, DeviceCode device_code) {
/* Allocate a session. */
auto session = ams::sf::AllocateShared<i2c::sf::ISession, SessionImpl>(this->allocator, this);
auto session = Factory::CreateSharedEmplaced<i2c::sf::ISession, SessionImpl>(std::addressof(this->allocator), this);
/* Open the session. */
R_TRY(session->GetImpl().OpenSession(device_code));
R_TRY(session.GetImpl().OpenSession(device_code));
/* We succeeded. */
out.SetValue(std::move(session));
*out = std::move(session);
return ResultSuccess();
}

View File

@@ -20,10 +20,12 @@
namespace ams::i2c::server {
class ManagerImpl {
private:
using Allocator = ams::sf::ExpHeapAllocator;
using Factory = ams::sf::ObjectFactory<Allocator::Policy>;
private:
lmem::HeapHandle heap_handle;
ams::sf::ExpHeapMemoryResource session_memory_resource;
typename ams::sf::ServiceObjectAllocator<i2c::sf::ISession, SessionImpl> allocator;
Allocator allocator;
u8 heap_buffer[4_KB];
public:
ManagerImpl();
@@ -31,11 +33,11 @@ namespace ams::i2c::server {
~ManagerImpl();
public:
/* Actual commands. */
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode);
Result OpenSession(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, i2c::I2cDevice device);
Result OpenSessionForDev(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, s32 bus_idx, u16 slave_address, i2c::AddressingMode addressing_mode, i2c::SpeedMode speed_mode);
Result OpenSession(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, i2c::I2cDevice device);
Result HasDevice(ams::sf::Out<bool> out, i2c::I2cDevice device);
Result HasDeviceForDev(ams::sf::Out<bool> out, i2c::I2cDevice device);
Result OpenSession2(ams::sf::Out<std::shared_ptr<i2c::sf::ISession>> out, DeviceCode device_code);
Result OpenSession2(ams::sf::Out<ams::sf::SharedPointer<i2c::sf::ISession>> out, DeviceCode device_code);
};
static_assert(i2c::sf::IsIManager<ManagerImpl>);