cellMic: add nullptr check for data in cell_mic_read

Also rename S to Size for readability
This commit is contained in:
Megamouse 2024-03-26 12:20:06 +01:00
parent e05239f3d9
commit cf5a66a204
2 changed files with 28 additions and 26 deletions

View File

@ -1144,8 +1144,6 @@ error_code cellMicRemoveNotifyEventQueue(u64 key)
error_code cell_mic_read(s32 dev_num, vm::ptr<void> data, s32 max_bytes, /*CellMicSignalType*/u32 type) error_code cell_mic_read(s32 dev_num, vm::ptr<void> data, s32 max_bytes, /*CellMicSignalType*/u32 type)
{ {
// TODO: CELL_MICIN_ERROR_PARAM
auto& mic_thr = g_fxo->get<mic_thread>(); auto& mic_thr = g_fxo->get<mic_thread>();
const std::lock_guard lock(mic_thr.mutex); const std::lock_guard lock(mic_thr.mutex);
if (!mic_thr.init) if (!mic_thr.init)
@ -1159,16 +1157,19 @@ error_code cell_mic_read(s32 dev_num, vm::ptr<void> data, s32 max_bytes, /*CellM
if (!device.is_opened() || !(device.get_signal_types() & type)) if (!device.is_opened() || !(device.get_signal_types() & type))
return CELL_MICIN_ERROR_NOT_OPEN; return CELL_MICIN_ERROR_NOT_OPEN;
if (!data)
return not_an_error(0);
switch (type) switch (type)
{ {
case CELLMIC_SIGTYPE_DSP: return not_an_error(device.read_dsp(vm::_ptr<u8>(data.addr()), max_bytes)); case CELLMIC_SIGTYPE_DSP: return not_an_error(device.read_dsp(vm::_ptr<u8>(data.addr()), max_bytes));
case CELLMIC_SIGTYPE_AUX: return CELL_OK; // TODO case CELLMIC_SIGTYPE_AUX: return not_an_error(0); // TODO
case CELLMIC_SIGTYPE_RAW: return not_an_error(device.read_raw(vm::_ptr<u8>(data.addr()), max_bytes)); case CELLMIC_SIGTYPE_RAW: return not_an_error(device.read_raw(vm::_ptr<u8>(data.addr()), max_bytes));
default: default:
fmt::throw_exception("Invalid CELLMIC_SIGTYPE %d", type); fmt::throw_exception("Invalid CELLMIC_SIGTYPE %d", type);
} }
return CELL_OK; return not_an_error(0);
} }
error_code cellMicReadRaw(s32 dev_num, vm::ptr<void> data, s32 max_bytes) error_code cellMicReadRaw(s32 dev_num, vm::ptr<void> data, s32 max_bytes)

View File

@ -157,9 +157,8 @@ struct CellMicInputStream
struct CellMicInputDefinition struct CellMicInputDefinition
{ {
// TODO: Data types be_t<u32> uiDevId;
volatile u32 uiDevId; CellMicInputStream data;
CellMicInputStream data;
CellMicInputFormatI aux_format; CellMicInputFormatI aux_format;
CellMicInputFormatI raw_format; CellMicInputFormatI raw_format;
CellMicInputFormatI sig_format; CellMicInputFormatI sig_format;
@ -182,13 +181,13 @@ struct CellMicStatus
// --- End of cell definitions --- // --- End of cell definitions ---
template <usz S> template <usz Size>
class simple_ringbuf class simple_ringbuf
{ {
public: public:
simple_ringbuf() simple_ringbuf()
{ {
m_container.resize(S); m_container.resize(Size);
} }
bool has_data() const bool has_data() const
@ -200,19 +199,19 @@ public:
{ {
ensure(buf); ensure(buf);
u32 to_read = size > m_used ? m_used : size; const u32 to_read = size > m_used ? m_used : size;
if (!to_read) if (!to_read)
return 0; return 0;
u8* data = m_container.data(); u8* data = m_container.data();
u32 new_tail = m_tail + to_read; const u32 new_tail = m_tail + to_read;
if (new_tail >= S) if (new_tail >= Size)
{ {
u32 first_chunk_size = S - m_tail; const u32 first_chunk_size = Size - m_tail;
std::memcpy(buf, data + m_tail, first_chunk_size); std::memcpy(buf, data + m_tail, first_chunk_size);
std::memcpy(buf + first_chunk_size, data, to_read - first_chunk_size); std::memcpy(buf + first_chunk_size, data, to_read - first_chunk_size);
m_tail = (new_tail - S); m_tail = (new_tail - Size);
} }
else else
{ {
@ -227,30 +226,32 @@ public:
void write_bytes(const u8* buf, const u32 size) void write_bytes(const u8* buf, const u32 size)
{ {
ensure(size <= S); ensure(size <= Size);
if (u32 over_size = m_used + size; over_size > S) const u32 over_size = m_used + size;
if (over_size > Size)
{ {
m_tail += (over_size - S); m_tail += (over_size - Size);
if (m_tail > S) if (m_tail > Size)
m_tail -= S; m_tail -= Size;
m_used = S; m_used = Size;
} }
else else
{ {
m_used = over_size; m_used = over_size;
} }
u8* data = m_container.data(); u8* data = m_container.data();
u32 new_head = m_head + size; const u32 new_head = m_head + size;
if (new_head >= S) if (new_head >= Size)
{ {
u32 first_chunk_size = S - m_head; const u32 first_chunk_size = Size - m_head;
std::memcpy(data + m_head, buf, first_chunk_size); std::memcpy(data + m_head, buf, first_chunk_size);
std::memcpy(data, buf + first_chunk_size, size - first_chunk_size); std::memcpy(data, buf + first_chunk_size, size - first_chunk_size);
m_head = (new_head - S); m_head = (new_head - Size);
} }
else else
{ {