code: Use std::span where appropriate (#6658)

* code: Use std::span when possible

* code: Prefix memcpy and memcmp with std::
This commit is contained in:
GPUCode 2023-07-07 01:52:40 +03:00 committed by GitHub
parent 4ccd9f24fb
commit cf9bb90ae3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
106 changed files with 362 additions and 329 deletions

View file

@ -164,7 +164,7 @@ void ExtraHID::OnDisconnect() {
timing.UnscheduleEvent(hid_polling_callback_id, 0);
}
void ExtraHID::HandleConfigureHIDPollingRequest(const std::vector<u8>& request) {
void ExtraHID::HandleConfigureHIDPollingRequest(std::span<const u8> request) {
if (request.size() != 3) {
LOG_ERROR(Service_IR, "Wrong request size ({}): {}", request.size(),
fmt::format("{:02x}", fmt::join(request, " ")));
@ -177,7 +177,7 @@ void ExtraHID::HandleConfigureHIDPollingRequest(const std::vector<u8>& request)
timing.ScheduleEvent(msToCycles(hid_period), hid_polling_callback_id);
}
void ExtraHID::HandleReadCalibrationDataRequest(const std::vector<u8>& request_buf) {
void ExtraHID::HandleReadCalibrationDataRequest(std::span<const u8> request_buf) {
struct ReadCalibrationDataRequest {
RequestID request_id;
u8 expected_response_time;
@ -213,7 +213,7 @@ void ExtraHID::HandleReadCalibrationDataRequest(const std::vector<u8>& request_b
Send(response);
}
void ExtraHID::OnReceive(const std::vector<u8>& data) {
void ExtraHID::OnReceive(std::span<const u8> data) {
switch (static_cast<RequestID>(data[0])) {
case RequestID::ConfigureHIDPolling:
HandleConfigureHIDPollingRequest(data);

View file

@ -6,6 +6,7 @@
#include <array>
#include <atomic>
#include <span>
#include <boost/serialization/array.hpp>
#include "common/bit_field.h"
#include "common/swap.h"
@ -47,15 +48,15 @@ public:
void OnConnect() override;
void OnDisconnect() override;
void OnReceive(const std::vector<u8>& data) override;
void OnReceive(std::span<const u8> data) override;
/// Requests input devices reload from current settings. Called when the input settings change.
void RequestInputDevicesReload();
private:
void SendHIDStatus();
void HandleConfigureHIDPollingRequest(const std::vector<u8>& request);
void HandleReadCalibrationDataRequest(const std::vector<u8>& request);
void HandleConfigureHIDPollingRequest(std::span<const u8> request);
void HandleReadCalibrationDataRequest(std::span<const u8> request);
void LoadInputDevices();
Core::Timing& timing;

View file

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <memory>
#include <vector>
#include <boost/crc.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/shared_ptr.hpp>
@ -98,9 +99,10 @@ public:
* @params packet The data of the packet to put.
* @returns whether the operation is successful.
*/
bool Put(const std::vector<u8>& packet) {
if (info.packet_count == max_packet_count)
bool Put(std::span<const u8> packet) {
if (info.packet_count == max_packet_count) {
return false;
}
u32 write_offset;
@ -182,12 +184,12 @@ private:
}
void SetPacketInfo(u32 index, const PacketInfo& packet_info) {
memcpy(GetPacketInfoPointer(index), &packet_info, sizeof(PacketInfo));
std::memcpy(GetPacketInfoPointer(index), &packet_info, sizeof(PacketInfo));
}
PacketInfo GetPacketInfo(u32 index) {
PacketInfo packet_info;
memcpy(&packet_info, GetPacketInfoPointer(index), sizeof(PacketInfo));
std::memcpy(&packet_info, GetPacketInfoPointer(index), sizeof(PacketInfo));
return packet_info;
}
@ -198,7 +200,7 @@ private:
void UpdateBufferInfo() {
if (info_offset) {
memcpy(shared_memory->GetPointer(info_offset), &info, sizeof(info));
std::memcpy(shared_memory->GetPointer(info_offset), &info, sizeof(info));
}
}
@ -225,7 +227,7 @@ private:
};
/// Wraps the payload into packet and puts it to the receive buffer
void IR_USER::PutToReceive(const std::vector<u8>& payload) {
void IR_USER::PutToReceive(std::span<const u8> payload) {
LOG_TRACE(Service_IR, "called, data={}", fmt::format("{:02x}", fmt::join(payload, " ")));
std::size_t size = payload.size();
@ -464,8 +466,8 @@ IR_USER::IR_USER(Core::System& system) : ServiceFramework("ir:USER", 1) {
send_event = system.Kernel().CreateEvent(ResetType::OneShot, "IR:SendEvent");
receive_event = system.Kernel().CreateEvent(ResetType::OneShot, "IR:ReceiveEvent");
extra_hid = std::make_unique<ExtraHID>(
[this](const std::vector<u8>& data) { PutToReceive(data); }, system.CoreTiming());
extra_hid = std::make_unique<ExtraHID>([this](std::span<const u8> data) { PutToReceive(data); },
system.CoreTiming());
}
IR_USER::~IR_USER() {
@ -481,7 +483,7 @@ void IR_USER::ReloadInputDevices() {
IRDevice::IRDevice(SendFunc send_func_) : send_func(send_func_) {}
IRDevice::~IRDevice() = default;
void IRDevice::Send(const std::vector<u8>& data) {
void IRDevice::Send(std::span<const u8> data) {
send_func(data);
}

View file

@ -6,7 +6,7 @@
#include <functional>
#include <memory>
#include <vector>
#include <span>
#include "core/hle/service/service.h"
namespace Kernel {
@ -26,7 +26,7 @@ public:
* A function object that implements the method to send data to the 3DS, which takes a vector of
* data to send.
*/
using SendFunc = std::function<void(const std::vector<u8>& data)>;
using SendFunc = std::function<void(std::span<const u8> data)>;
explicit IRDevice(SendFunc send_func);
virtual ~IRDevice();
@ -38,11 +38,11 @@ public:
virtual void OnDisconnect() = 0;
/// Called when data is received from the 3DS. This is invoked by the ir:USER send function.
virtual void OnReceive(const std::vector<u8>& data) = 0;
virtual void OnReceive(std::span<const u8> data) = 0;
protected:
/// Sends data to the 3DS. The actual sending method is specified in the constructor
void Send(const std::vector<u8>& data);
void Send(std::span<const u8> data);
private:
// NOTE: This value is *not* serialized because it's always passed in the constructor
@ -161,7 +161,7 @@ private:
*/
void ReleaseReceivedData(Kernel::HLERequestContext& ctx);
void PutToReceive(const std::vector<u8>& payload);
void PutToReceive(std::span<const u8> payload);
std::shared_ptr<Kernel::Event> conn_status_event, send_event, receive_event;
std::shared_ptr<Kernel::SharedMemory> shared_memory;