service: Make use of buffer element count helpers

merge-requests/60/head
Lioncash 2022-11-23 13:25:14 +07:00
parent 59335f6796
commit 97f273e94e
12 changed files with 41 additions and 47 deletions

@ -122,10 +122,10 @@ private:
} }
void GetReleasedAudioInBuffer(Kernel::HLERequestContext& ctx) { void GetReleasedAudioInBuffer(Kernel::HLERequestContext& ctx) {
auto write_buffer_size = ctx.GetWriteBufferSize() / sizeof(u64); const auto write_buffer_size = ctx.GetWriteBufferNumElements<u64>();
std::vector<u64> released_buffers(write_buffer_size, 0); std::vector<u64> released_buffers(write_buffer_size);
auto count = impl->GetReleasedBuffers(released_buffers); const auto count = impl->GetReleasedBuffers(released_buffers);
[[maybe_unused]] std::string tags{}; [[maybe_unused]] std::string tags{};
for (u32 i = 0; i < count; i++) { for (u32 i = 0; i < count; i++) {
@ -228,7 +228,7 @@ void AudInU::ListAudioIns(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_Audio, "called"); LOG_DEBUG(Service_Audio, "called");
const auto write_count = const auto write_count =
static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(AudioDevice::AudioDeviceName)); static_cast<u32>(ctx.GetWriteBufferNumElements<AudioDevice::AudioDeviceName>());
std::vector<AudioDevice::AudioDeviceName> device_names{}; std::vector<AudioDevice::AudioDeviceName> device_names{};
u32 out_count{0}; u32 out_count{0};
@ -248,7 +248,7 @@ void AudInU::ListAudioInsAutoFiltered(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_Audio, "called"); LOG_DEBUG(Service_Audio, "called");
const auto write_count = const auto write_count =
static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(AudioDevice::AudioDeviceName)); static_cast<u32>(ctx.GetWriteBufferNumElements<AudioDevice::AudioDeviceName>());
std::vector<AudioDevice::AudioDeviceName> device_names{}; std::vector<AudioDevice::AudioDeviceName> device_names{};
u32 out_count{0}; u32 out_count{0};

@ -129,16 +129,16 @@ private:
} }
void GetReleasedAudioOutBuffers(Kernel::HLERequestContext& ctx) { void GetReleasedAudioOutBuffers(Kernel::HLERequestContext& ctx) {
auto write_buffer_size = ctx.GetWriteBufferSize() / sizeof(u64); const auto write_buffer_size = ctx.GetWriteBufferNumElements<u64>();
std::vector<u64> released_buffers(write_buffer_size, 0); std::vector<u64> released_buffers(write_buffer_size);
auto count = impl->GetReleasedBuffers(released_buffers); const auto count = impl->GetReleasedBuffers(released_buffers);
[[maybe_unused]] std::string tags{}; [[maybe_unused]] std::string tags{};
for (u32 i = 0; i < count; i++) { for (u32 i = 0; i < count; i++) {
tags += fmt::format("{:08X}, ", released_buffers[i]); tags += fmt::format("{:08X}, ", released_buffers[i]);
} }
[[maybe_unused]] auto sessionid{impl->GetSystem().GetSessionId()}; [[maybe_unused]] const auto sessionid{impl->GetSystem().GetSessionId()};
LOG_TRACE(Service_Audio, "called. Session {} released {} buffers: {}", sessionid, count, LOG_TRACE(Service_Audio, "called. Session {} released {} buffers: {}", sessionid, count,
tags); tags);
@ -244,7 +244,7 @@ void AudOutU::ListAudioOuts(Kernel::HLERequestContext& ctx) {
std::scoped_lock l{impl->mutex}; std::scoped_lock l{impl->mutex};
const auto write_count = const auto write_count =
static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(AudioDevice::AudioDeviceName)); static_cast<u32>(ctx.GetWriteBufferNumElements<AudioDevice::AudioDeviceName>());
std::vector<AudioDevice::AudioDeviceName> device_names{}; std::vector<AudioDevice::AudioDeviceName> device_names{};
if (write_count > 0) { if (write_count > 0) {
device_names.emplace_back("DeviceOut"); device_names.emplace_back("DeviceOut");

@ -274,7 +274,7 @@ public:
private: private:
void ListAudioDeviceName(Kernel::HLERequestContext& ctx) { void ListAudioDeviceName(Kernel::HLERequestContext& ctx) {
const size_t in_count = ctx.GetWriteBufferSize() / sizeof(AudioDevice::AudioDeviceName); const size_t in_count = ctx.GetWriteBufferNumElements<AudioDevice::AudioDeviceName>();
std::vector<AudioDevice::AudioDeviceName> out_names{}; std::vector<AudioDevice::AudioDeviceName> out_names{};
@ -335,7 +335,7 @@ private:
} }
void GetActiveAudioDeviceName(Kernel::HLERequestContext& ctx) { void GetActiveAudioDeviceName(Kernel::HLERequestContext& ctx) {
const auto write_size = ctx.GetWriteBufferSize() / sizeof(char); const auto write_size = ctx.GetWriteBufferSize();
std::string out_name{"AudioTvOutput"}; std::string out_name{"AudioTvOutput"};
LOG_DEBUG(Service_Audio, "(STUBBED) called. Name={}", out_name); LOG_DEBUG(Service_Audio, "(STUBBED) called. Name={}", out_name);
@ -387,7 +387,7 @@ private:
} }
void ListAudioOutputDeviceName(Kernel::HLERequestContext& ctx) { void ListAudioOutputDeviceName(Kernel::HLERequestContext& ctx) {
const size_t in_count = ctx.GetWriteBufferSize() / sizeof(AudioDevice::AudioDeviceName); const size_t in_count = ctx.GetWriteBufferNumElements<AudioDevice::AudioDeviceName>();
std::vector<AudioDevice::AudioDeviceName> out_names{}; std::vector<AudioDevice::AudioDeviceName> out_names{};

@ -68,7 +68,7 @@ private:
ExtraBehavior extra_behavior) { ExtraBehavior extra_behavior) {
u32 consumed = 0; u32 consumed = 0;
u32 sample_count = 0; u32 sample_count = 0;
std::vector<opus_int16> samples(ctx.GetWriteBufferSize() / sizeof(opus_int16)); std::vector<opus_int16> samples(ctx.GetWriteBufferNumElements<opus_int16>());
if (extra_behavior == ExtraBehavior::ResetContext) { if (extra_behavior == ExtraBehavior::ResetContext) {
ResetDecoderContext(); ResetDecoderContext();

@ -443,7 +443,7 @@ private:
} }
void Read(Kernel::HLERequestContext& ctx) { void Read(Kernel::HLERequestContext& ctx) {
auto write_size = ctx.GetWriteBufferSize() / sizeof(DeliveryCacheDirectoryEntry); auto write_size = ctx.GetWriteBufferNumElements<DeliveryCacheDirectoryEntry>();
LOG_DEBUG(Service_BCAT, "called, write_size={:016X}", write_size); LOG_DEBUG(Service_BCAT, "called, write_size={:016X}", write_size);
@ -533,7 +533,7 @@ private:
} }
void EnumerateDeliveryCacheDirectory(Kernel::HLERequestContext& ctx) { void EnumerateDeliveryCacheDirectory(Kernel::HLERequestContext& ctx) {
auto size = ctx.GetWriteBufferSize() / sizeof(DirectoryName); auto size = ctx.GetWriteBufferNumElements<DirectoryName>();
LOG_DEBUG(Service_BCAT, "called, size={:016X}", size); LOG_DEBUG(Service_BCAT, "called, size={:016X}", size);

@ -192,12 +192,10 @@ private:
} }
void ListCommonTicketRightsIds(Kernel::HLERequestContext& ctx) { void ListCommonTicketRightsIds(Kernel::HLERequestContext& ctx) {
u32 out_entries; size_t out_entries = 0;
if (keys.GetCommonTickets().empty()) if (!keys.GetCommonTickets().empty()) {
out_entries = 0; out_entries = ctx.GetWriteBufferNumElements<u128>();
else }
out_entries = static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(u128));
LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries); LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries);
keys.PopulateTickets(); keys.PopulateTickets();
@ -206,20 +204,19 @@ private:
std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids), std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids),
[](const auto& pair) { return pair.first; }); [](const auto& pair) { return pair.first; });
out_entries = static_cast<u32>(std::min<std::size_t>(ids.size(), out_entries)); out_entries = std::min(ids.size(), out_entries);
ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128)); ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128));
IPC::ResponseBuilder rb{ctx, 3}; IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);
rb.Push<u32>(out_entries); rb.Push<u32>(static_cast<u32>(out_entries));
} }
void ListPersonalizedTicketRightsIds(Kernel::HLERequestContext& ctx) { void ListPersonalizedTicketRightsIds(Kernel::HLERequestContext& ctx) {
u32 out_entries; size_t out_entries = 0;
if (keys.GetPersonalizedTickets().empty()) if (!keys.GetPersonalizedTickets().empty()) {
out_entries = 0; out_entries = ctx.GetWriteBufferNumElements<u128>();
else }
out_entries = static_cast<u32>(ctx.GetWriteBufferSize() / sizeof(u128));
LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries); LOG_DEBUG(Service_ETicket, "called, entries={:016X}", out_entries);
@ -229,12 +226,12 @@ private:
std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids), std::transform(tickets.begin(), tickets.end(), std::back_inserter(ids),
[](const auto& pair) { return pair.first; }); [](const auto& pair) { return pair.first; });
out_entries = static_cast<u32>(std::min<std::size_t>(ids.size(), out_entries)); out_entries = std::min(ids.size(), out_entries);
ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128)); ctx.WriteBuffer(ids.data(), out_entries * sizeof(u128));
IPC::ResponseBuilder rb{ctx, 3}; IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess); rb.Push(ResultSuccess);
rb.Push<u32>(out_entries); rb.Push<u32>(static_cast<u32>(out_entries));
} }
void GetCommonTicketSize(Kernel::HLERequestContext& ctx) { void GetCommonTicketSize(Kernel::HLERequestContext& ctx) {

@ -277,7 +277,7 @@ private:
LOG_DEBUG(Service_FS, "called."); LOG_DEBUG(Service_FS, "called.");
// Calculate how many entries we can fit in the output buffer // Calculate how many entries we can fit in the output buffer
const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(FileSys::Entry); const u64 count_entries = ctx.GetWriteBufferNumElements<FileSys::Entry>();
// Cap at total number of entries. // Cap at total number of entries.
const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index); const u64 actual_entries = std::min(count_entries, entries.size() - next_entry_index);
@ -543,7 +543,7 @@ public:
LOG_DEBUG(Service_FS, "called"); LOG_DEBUG(Service_FS, "called");
// Calculate how many entries we can fit in the output buffer // Calculate how many entries we can fit in the output buffer
const u64 count_entries = ctx.GetWriteBufferSize() / sizeof(SaveDataInfo); const u64 count_entries = ctx.GetWriteBufferNumElements<SaveDataInfo>();
// Cap at total number of entries. // Cap at total number of entries.
const u64 actual_entries = std::min(count_entries, info.size() - next_entry_index); const u64 actual_entries = std::min(count_entries, info.size() - next_entry_index);

@ -292,7 +292,7 @@ public:
void GetNetworkInfoLatestUpdate(Kernel::HLERequestContext& ctx) { void GetNetworkInfoLatestUpdate(Kernel::HLERequestContext& ctx) {
const std::size_t network_buffer_size = ctx.GetWriteBufferSize(0); const std::size_t network_buffer_size = ctx.GetWriteBufferSize(0);
const std::size_t node_buffer_count = ctx.GetWriteBufferSize(1) / sizeof(NodeLatestUpdate); const std::size_t node_buffer_count = ctx.GetWriteBufferNumElements<NodeLatestUpdate>(1);
if (node_buffer_count == 0 || network_buffer_size != sizeof(NetworkInfo)) { if (node_buffer_count == 0 || network_buffer_size != sizeof(NetworkInfo)) {
LOG_ERROR(Service_LDN, "Invalid buffer, size = {}, count = {}", network_buffer_size, LOG_ERROR(Service_LDN, "Invalid buffer, size = {}, count = {}", network_buffer_size,
@ -333,7 +333,7 @@ public:
const auto channel{rp.PopEnum<WifiChannel>()}; const auto channel{rp.PopEnum<WifiChannel>()};
const auto scan_filter{rp.PopRaw<ScanFilter>()}; const auto scan_filter{rp.PopRaw<ScanFilter>()};
const std::size_t network_info_size = ctx.GetWriteBufferSize() / sizeof(NetworkInfo); const std::size_t network_info_size = ctx.GetWriteBufferNumElements<NetworkInfo>();
if (network_info_size == 0) { if (network_info_size == 0) {
LOG_ERROR(Service_LDN, "Invalid buffer size {}", network_info_size); LOG_ERROR(Service_LDN, "Invalid buffer size {}", network_info_size);

@ -118,7 +118,7 @@ void IUser::ListDevices(Kernel::HLERequestContext& ctx) {
} }
std::vector<u64> nfp_devices; std::vector<u64> nfp_devices;
const std::size_t max_allowed_devices = ctx.GetWriteBufferSize() / sizeof(u64); const std::size_t max_allowed_devices = ctx.GetWriteBufferNumElements<u64>();
for (auto& device : devices) { for (auto& device : devices) {
if (nfp_devices.size() >= max_allowed_devices) { if (nfp_devices.size() >= max_allowed_devices) {

@ -104,9 +104,9 @@ void IUser::ListDevices(Kernel::HLERequestContext& ctx) {
} }
std::vector<u64> nfp_devices; std::vector<u64> nfp_devices;
const std::size_t max_allowed_devices = ctx.GetWriteBufferSize() / sizeof(u64); const std::size_t max_allowed_devices = ctx.GetWriteBufferNumElements<u64>();
for (auto& device : devices) { for (const auto& device : devices) {
if (nfp_devices.size() >= max_allowed_devices) { if (nfp_devices.size() >= max_allowed_devices) {
continue; continue;
} }
@ -115,7 +115,7 @@ void IUser::ListDevices(Kernel::HLERequestContext& ctx) {
} }
} }
if (nfp_devices.size() == 0) { if (nfp_devices.empty()) {
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(DeviceNotFound); rb.Push(DeviceNotFound);
return; return;

@ -279,13 +279,10 @@ void IPlatformServiceManager::GetSharedFontInOrderOfPriority(Kernel::HLERequestC
font_sizes.push_back(region.size); font_sizes.push_back(region.size);
} }
// Resize buffers if game requests smaller size output. // Resize buffers if game requests smaller size output
font_codes.resize( font_codes.resize(std::min(font_codes.size(), ctx.GetWriteBufferNumElements<u32>(0)));
std::min<std::size_t>(font_codes.size(), ctx.GetWriteBufferSize(0) / sizeof(u32))); font_offsets.resize(std::min(font_offsets.size(), ctx.GetWriteBufferNumElements<u32>(1)));
font_offsets.resize( font_sizes.resize(std::min(font_sizes.size(), ctx.GetWriteBufferNumElements<u32>(2)));
std::min<std::size_t>(font_offsets.size(), ctx.GetWriteBufferSize(1) / sizeof(u32)));
font_sizes.resize(
std::min<std::size_t>(font_sizes.size(), ctx.GetWriteBufferSize(2) / sizeof(u32)));
ctx.WriteBuffer(font_codes, 0); ctx.WriteBuffer(font_codes, 0);
ctx.WriteBuffer(font_offsets, 1); ctx.WriteBuffer(font_offsets, 1);

@ -83,7 +83,7 @@ void PushResponseLanguageCode(Kernel::HLERequestContext& ctx, std::size_t num_la
} }
void GetAvailableLanguageCodesImpl(Kernel::HLERequestContext& ctx, std::size_t max_entries) { void GetAvailableLanguageCodesImpl(Kernel::HLERequestContext& ctx, std::size_t max_entries) {
const std::size_t requested_amount = ctx.GetWriteBufferSize() / sizeof(LanguageCode); const std::size_t requested_amount = ctx.GetWriteBufferNumElements<LanguageCode>();
const std::size_t max_amount = std::min(requested_amount, max_entries); const std::size_t max_amount = std::min(requested_amount, max_entries);
const std::size_t copy_amount = std::min(available_language_codes.size(), max_amount); const std::size_t copy_amount = std::min(available_language_codes.size(), max_amount);
const std::size_t copy_size = copy_amount * sizeof(LanguageCode); const std::size_t copy_size = copy_amount * sizeof(LanguageCode);