Skip to content

Commit

Permalink
Use span instead of vector
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Sep 7, 2023
1 parent 71e61a0 commit 87da328
Show file tree
Hide file tree
Showing 31 changed files with 162 additions and 210 deletions.
6 changes: 4 additions & 2 deletions cpp/controllers/abstract_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "controller_manager.h"
#include <unordered_set>
#include <unordered_map>
#include <span>
#include <vector>
#include <memory>

Expand Down Expand Up @@ -77,8 +78,9 @@ class AbstractController : public PhaseHandler, public enable_shared_from_this<A
void IncrementNext() { ++ctrl.next; }
int GetMessage() const { return ctrl.message; }
void SetMessage(int m) { ctrl.message = m; }
// TODO Consider using std::span instead of std::vector&
vector<int>& GetCmd() { return ctrl.cmd; }
span <const int> GetCmd() { return ctrl.cmd; }
// TODO Try to make this method protected. It is currently public for usage by the unit tests.
void SetCmdByte(int index, int value) { ctrl.cmd[index] = value; }
int GetCmd(int index) const { return ctrl.cmd[index]; }
bool IsByteTransfer() const { return is_byte_transfer; }
void SetByteTransfer(bool);
Expand Down
4 changes: 2 additions & 2 deletions cpp/controllers/scsi_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void ScsiController::Command()
// Command data transfer
AllocateCmd(command_byte_count);
for (int i = 0; i < command_byte_count; i++) {
GetCmd()[i] = GetBuffer()[i];
SetCmdByte(i, GetBuffer()[i]);
}

SetLength(0);
Expand Down Expand Up @@ -938,7 +938,7 @@ void ScsiController::ProcessCommand()
stringstream s;
s << "CDB=$" << setfill('0') << setw(2) << hex;
for (uint32_t i = 0; i < len; i++) {
GetCmd()[i] = GetBuffer()[i];
SetCmdByte(i, GetBuffer()[i]);
s << GetCmd(i);
}
logger.Trace(s.str());
Expand Down
4 changes: 2 additions & 2 deletions cpp/devices/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ bool Disk::Eject(bool force)
return status;
}

int Disk::ModeSense6(const vector<int>& cdb, vector<uint8_t>& buf) const
int Disk::ModeSense6(span<const int> cdb, vector<uint8_t>& buf) const
{
// Get length, clear buffer
const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(cdb[4])));
Expand Down Expand Up @@ -315,7 +315,7 @@ int Disk::ModeSense6(const vector<int>& cdb, vector<uint8_t>& buf) const
return size;
}

int Disk::ModeSense10(const vector<int>& cdb, vector<uint8_t>& buf) const
int Disk::ModeSense10(span<const int> cdb, vector<uint8_t>& buf) const
{
// Get length, clear buffer
const auto length = static_cast<int>(min(buf.size(), static_cast<size_t>(GetInt16(cdb, 7))));
Expand Down
5 changes: 3 additions & 2 deletions cpp/devices/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "interfaces/scsi_block_commands.h"
#include "storage_device.h"
#include <string>
#include <span>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
Expand Down Expand Up @@ -92,8 +93,8 @@ class Disk : public StorageDevice, private ScsiBlockCommands
void ValidateBlockAddress(access_mode) const;
tuple<bool, uint64_t, uint32_t> CheckAndGetStartAndCount(access_mode) const;

int ModeSense6(const vector<int>&, vector<uint8_t>&) const override;
int ModeSense10(const vector<int>&, vector<uint8_t>&) const override;
int ModeSense6(span<const int>, vector<uint8_t>&) const override;
int ModeSense10(span<const int>, vector<uint8_t>&) const override;

static inline const unordered_map<uint32_t, uint32_t> shift_counts =
{ { 512, 9 }, { 1024, 10 }, { 2048, 11 }, { 4096, 12 } };
Expand Down
4 changes: 2 additions & 2 deletions cpp/devices/host_services.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void HostServices::StartStopUnit() const
EnterStatusPhase();
}

int HostServices::ModeSense6(const vector<int>& cdb, vector<uint8_t>& buf) const
int HostServices::ModeSense6(span<const int> cdb, vector<uint8_t>& buf) const
{
// Block descriptors cannot be returned
if (!(cdb[1] & 0x08)) {
Expand All @@ -95,7 +95,7 @@ int HostServices::ModeSense6(const vector<int>& cdb, vector<uint8_t>& buf) const
return size;
}

int HostServices::ModeSense10(const vector<int>& cdb, vector<uint8_t>& buf) const
int HostServices::ModeSense10(span<const int> cdb, vector<uint8_t>& buf) const
{
// Block descriptors cannot be returned
if (!(cdb[1] & 0x08)) {
Expand Down
7 changes: 4 additions & 3 deletions cpp/devices/host_services.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
// Copyright (C) 2022-2023 Uwe Seimet
//
// Host Services with realtime clock and shutdown support
//
Expand All @@ -12,6 +12,7 @@
#pragma once

#include "mode_page_device.h"
#include <span>
#include <vector>
#include <map>

Expand Down Expand Up @@ -48,8 +49,8 @@ class HostServices: public ModePageDevice
};

void StartStopUnit() const;
int ModeSense6(const vector<int>&, vector<uint8_t>&) const override;
int ModeSense10(const vector<int>&, vector<uint8_t>&) const override;
int ModeSense6(span<const int>, vector<uint8_t>&) const override;
int ModeSense10(span<const int>, vector<uint8_t>&) const override;

void AddRealtimeClockPage(map<int, vector<byte>>&, bool) const;
};
3 changes: 2 additions & 1 deletion cpp/devices/interfaces/byte_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma once

#include <span>
#include <vector>

using namespace std;
Expand All @@ -23,5 +24,5 @@ class ByteWriter
ByteWriter() = default;
virtual ~ByteWriter() = default;

virtual bool WriteBytes(const vector<int>&, vector<uint8_t>&) = 0;
virtual bool WriteBytes(span<const int>, vector<uint8_t>&) = 0;
};
6 changes: 3 additions & 3 deletions cpp/devices/mode_page_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
// Copyright (C) 2022-2023 Uwe Seimet
//
// A basic device with mode page support, to be used for subclassing
//
Expand Down Expand Up @@ -32,7 +32,7 @@ bool ModePageDevice::Init(const unordered_map<string, string>& params)
return true;
}

int ModePageDevice::AddModePages(const vector<int>& cdb, vector<uint8_t>& buf, int offset, int length, int max_size) const
int ModePageDevice::AddModePages(span<const int> cdb, vector<uint8_t>& buf, int offset, int length, int max_size) const
{
const int max_length = length - offset;
if (max_length < 0) {
Expand Down Expand Up @@ -114,7 +114,7 @@ void ModePageDevice::ModeSense10() const
EnterDataInPhase();
}

void ModePageDevice::ModeSelect(scsi_command, const vector<int>&, const vector<uint8_t>&, int) const
void ModePageDevice::ModeSelect(scsi_command, span<const int>, span<const uint8_t>, int) const
{
// There is no default implementation of MDOE SELECT
throw scsi_exception(sense_key::illegal_request, asc::invalid_command_operation_code);
Expand Down
9 changes: 5 additions & 4 deletions cpp/devices/mode_page_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "primary_device.h"
#include <string>
#include <span>
#include <vector>
#include <map>

Expand All @@ -22,13 +23,13 @@ class ModePageDevice : public PrimaryDevice

bool Init(const unordered_map<string, string>&) override;

virtual void ModeSelect(scsi_defs::scsi_command, const vector<int>&, const vector<uint8_t>&, int) const;
virtual void ModeSelect(scsi_defs::scsi_command, span<const int>, span<const uint8_t>, int) const;

protected:

bool SupportsSaveParameters() const { return supports_save_parameters; }
void SupportsSaveParameters(bool b) { supports_save_parameters = b; }
int AddModePages(const vector<int>&, vector<uint8_t>&, int, int, int) const;
int AddModePages(span<const int>, vector<uint8_t>&, int, int, int) const;
virtual void SetUpModePages(map<int, vector<byte>>&, int, bool) const = 0;
virtual void AddVendorPage(map<int, vector<byte>>&, int, bool) const {
// Nothing to add by default
Expand All @@ -38,8 +39,8 @@ class ModePageDevice : public PrimaryDevice

bool supports_save_parameters = false;

virtual int ModeSense6(const vector<int>&, vector<uint8_t>&) const = 0;
virtual int ModeSense10(const vector<int>&, vector<uint8_t>&) const = 0;
virtual int ModeSense6(span<const int>, vector<uint8_t>&) const = 0;
virtual int ModeSense10(span<const int>, vector<uint8_t>&) const = 0;

void ModeSense6() const;
void ModeSense10() const;
Expand Down
4 changes: 2 additions & 2 deletions cpp/devices/scsi_command_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

using namespace scsi_defs;

void scsi_command_util::ModeSelect(const DeviceLogger& logger, scsi_command cmd, const vector<int>& cdb,
const vector<uint8_t>& buf, int length, int sector_size)
void scsi_command_util::ModeSelect(const DeviceLogger& logger, scsi_command cmd, span<const int> cdb,
span<const uint8_t> buf, int length, int sector_size)
{
assert(cmd == scsi_command::eCmdModeSelect6 || cmd == scsi_command::eCmdModeSelect10);
assert(length >= 0);
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/scsi_command_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DeviceLogger;

namespace scsi_command_util
{
void ModeSelect(const DeviceLogger&, scsi_defs::scsi_command, const vector<int>&, const vector<uint8_t>&, int, int);
void ModeSelect(const DeviceLogger&, scsi_defs::scsi_command, span<const int>, span<const uint8_t>, int, int);
void EnrichFormatPage(map<int, vector<byte>>&, bool, int);
void AddAppleVendorModePage(map<int, vector<byte>>&, bool);

Expand Down
6 changes: 3 additions & 3 deletions cpp/devices/scsi_daynaport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ vector<uint8_t> SCSIDaynaPort::InquiryInternal() const
// - The SCSI/Link apparently has about 6KB buffer space for packets.
//
//---------------------------------------------------------------------------
int SCSIDaynaPort::Read(const vector<int>& cdb, vector<uint8_t>& buf, uint64_t)
int SCSIDaynaPort::Read(span<const int> cdb, vector<uint8_t>& buf, uint64_t)
{
int rx_packet_size = 0;
const auto response = (scsi_resp_read_t*)buf.data();
Expand Down Expand Up @@ -249,7 +249,7 @@ int SCSIDaynaPort::Read(const vector<int>& cdb, vector<uint8_t>& buf, uint64_t)
// XX XX ... is the actual packet
//
//---------------------------------------------------------------------------
bool SCSIDaynaPort::WriteBytes(const vector<int>& cdb, vector<uint8_t>& buf)
bool SCSIDaynaPort::WriteBytes(span<const int> cdb, vector<uint8_t>& buf)
{
const int data_format = cdb[5];
int data_length = GetInt16(cdb, 3);
Expand Down Expand Up @@ -291,7 +291,7 @@ bool SCSIDaynaPort::WriteBytes(const vector<int>& cdb, vector<uint8_t>& buf)
// - long #3: frames lost
//
//---------------------------------------------------------------------------
int SCSIDaynaPort::RetrieveStats(const vector<int>& cdb, vector<uint8_t>& buf) const
int SCSIDaynaPort::RetrieveStats(span<const int> cdb, vector<uint8_t>& buf) const
{
memcpy(buf.data(), &m_scsi_link_stats, sizeof(m_scsi_link_stats));

Expand Down
7 changes: 4 additions & 3 deletions cpp/devices/scsi_daynaport.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "primary_device.h"
#include "ctapdriver.h"
#include <string>
#include <span>
#include <unordered_map>
#include <array>

Expand All @@ -52,10 +53,10 @@ class SCSIDaynaPort : public PrimaryDevice, public ByteWriter

// Commands
vector<uint8_t> InquiryInternal() const override;
int Read(const vector<int>&, vector<uint8_t>&, uint64_t);
bool WriteBytes(const vector<int>&, vector<uint8_t>&) override;
int Read(span <const int>, vector<uint8_t>&, uint64_t);
bool WriteBytes(span<const int>, vector<uint8_t>&) override;

int RetrieveStats(const vector<int>&, vector<uint8_t>&) const;
int RetrieveStats(span<const int>, vector<uint8_t>&) const;

void TestUnitReady() override;
void Read6();
Expand Down
8 changes: 4 additions & 4 deletions cpp/devices/scsi_host_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void SCSIBR::TestUnitReady()
EnterStatusPhase();
}

int SCSIBR::GetMessage10(const vector<int>& cdb, vector<uint8_t>& buf)
int SCSIBR::GetMessage10(span<const int> cdb, vector<uint8_t>& buf)
{
// Type
const int type = cdb[2];
Expand Down Expand Up @@ -186,7 +186,7 @@ int SCSIBR::GetMessage10(const vector<int>& cdb, vector<uint8_t>& buf)
return 0;
}

bool SCSIBR::WriteBytes(const vector<int>& cdb, vector<uint8_t>& buf)
bool SCSIBR::WriteBytes(span<const int> cdb, vector<uint8_t>& buf)
{
// Type
const int type = cdb[2];
Expand Down Expand Up @@ -291,7 +291,7 @@ int SCSIBR::GetMacAddr(vector<uint8_t>& mac) const
return static_cast<int>(mac_addr.size());
}

void SCSIBR::SetMacAddr(const vector<uint8_t>& mac)
void SCSIBR::SetMacAddr(span<const uint8_t> mac)
{
memcpy(mac_addr.data(), mac.data(), mac_addr.size());
}
Expand Down Expand Up @@ -338,7 +338,7 @@ void SCSIBR::GetPacketBuf(vector<uint8_t>& buf, int index)
packet_enable = false;
}

void SCSIBR::SendPacket(const vector<uint8_t>& buf, int len)
void SCSIBR::SendPacket(span<const uint8_t> buf, int len)
{
tap.Send(buf.data(), len);
}
Expand Down
11 changes: 6 additions & 5 deletions cpp/devices/scsi_host_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ctapdriver.h"
#include "cfilesystem.h"
#include <string>
#include <span>
#include <array>

using namespace std;
Expand All @@ -39,19 +40,19 @@ class SCSIBR : public PrimaryDevice, public ByteWriter

// Commands
vector<uint8_t> InquiryInternal() const override;
int GetMessage10(const vector<int>&, vector<uint8_t>&);
bool WriteBytes(const vector<int>&, vector<uint8_t>&) override;
int GetMessage10(span<const int>, vector<uint8_t>&);
bool WriteBytes(span<const int>, vector<uint8_t>&) override;
void TestUnitReady() override;
void GetMessage10();
void SendMessage10() const;

private:

int GetMacAddr(vector<uint8_t>&) const; // Get MAC address
void SetMacAddr(const vector<uint8_t>&); // Set MAC address
void SetMacAddr(span<const uint8_t>); // Set MAC address
void ReceivePacket(); // Receive a packet
void GetPacketBuf(vector<uint8_t>&, int); // Get a packet
void SendPacket(const vector<uint8_t>&, int); // Send a packet
void GetPacketBuf(vector<uint8_t>&, int); // Get a packet
void SendPacket(span<const uint8_t>, int); // Send a packet

CTapDriver tap; // TAP driver
bool m_bTapEnable = false; // TAP valid flag
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/scsicd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int SCSICD::Read(vector<uint8_t>& buf, uint64_t block)
return Disk::Read(buf, block);
}

int SCSICD::ReadTocInternal(const vector<int>& cdb, vector<uint8_t>& buf)
int SCSICD::ReadTocInternal(span <const int> cdb, vector<uint8_t>& buf)
{
CheckReady();

Expand Down
5 changes: 4 additions & 1 deletion cpp/devices/scsicd.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "cd_track.h"
#include "disk.h"
#include "interfaces/scsi_mmc_commands.h"
#include <span>
#include <vector>
#include <map>

class SCSICD : public Disk, private ScsiMmcCommands
{
Expand All @@ -39,7 +42,7 @@ class SCSICD : public Disk, private ScsiMmcCommands

private:

int ReadTocInternal(const vector<int>&, vector<uint8_t>&);
int ReadTocInternal(span<const int>, vector<uint8_t>&);

void AddCDROMPage(map<int, vector<byte>>&, bool) const;
void AddCDDAPage(map<int, vector<byte>>&, bool) const;
Expand Down
2 changes: 1 addition & 1 deletion cpp/devices/scsihd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ vector<uint8_t> SCSIHD::InquiryInternal() const
return HandleInquiry(device_type::direct_access, scsi_level, IsRemovable());
}

void SCSIHD::ModeSelect(scsi_command cmd, const vector<int>& cdb, const vector<uint8_t>& buf, int length) const
void SCSIHD::ModeSelect(scsi_command cmd, span<const int> cdb, span<const uint8_t> buf, int length) const
{
scsi_command_util::ModeSelect(GetLogger(), cmd, cdb, buf, length, 1 << GetSectorSizeShiftCount());
}
Expand Down
5 changes: 3 additions & 2 deletions cpp/devices/scsihd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
// Copyright (C) 2014-2020 GIMONS
// Copyright (C) 2022 Uwe Seimet
// Copyright (C) 2022-2023 Uwe Seimet
// Copyright (C) akuker
//
// Licensed under the BSD 3-Clause License.
Expand All @@ -18,6 +18,7 @@
#include "shared/scsi.h"
#include "disk.h"
#include <string>
#include <span>
#include <vector>
#include <map>

Expand All @@ -36,7 +37,7 @@ class SCSIHD : public Disk

// Commands
vector<uint8_t> InquiryInternal() const override;
void ModeSelect(scsi_defs::scsi_command, const vector<int>&, const vector<uint8_t>&, int) const override;
void ModeSelect(scsi_defs::scsi_command, span<const int>, span<const uint8_t>, int) const override;

void AddFormatPage(map<int, vector<byte>>&, bool) const override;
void AddVendorPage(map<int, vector<byte>>&, int, bool) const override;
Expand Down
Loading

0 comments on commit 87da328

Please sign in to comment.