mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:17:44 +00:00
Kernel: Convert UserOrKernelBuffer callbacks to use AK::Bytes
This commit is contained in:
parent
f3baa5d8c9
commit
668c429900
11 changed files with 58 additions and 55 deletions
|
@ -58,10 +58,10 @@ Kernel::KResultOr<size_t> ConsoleDevice::write(FileDescription&, u64, const Kern
|
||||||
if (!size)
|
if (!size)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return data.read_buffered<256>(size, [&](u8 const* bytes, size_t bytes_count) {
|
return data.read_buffered<256>(size, [&](ReadonlyBytes readonly_bytes) {
|
||||||
for (size_t i = 0; i < bytes_count; i++)
|
for (const auto& byte : readonly_bytes)
|
||||||
put_char((char)bytes[i]);
|
put_char(byte);
|
||||||
return bytes_count;
|
return readonly_bytes.size();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -292,9 +292,9 @@ KResultOr<size_t> KeyboardDevice::read(FileDescription&, u64, UserOrKernelBuffer
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
auto result = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
|
auto result = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](Bytes bytes) {
|
||||||
memcpy(data, &event, sizeof(Event));
|
memcpy(bytes.data(), &event, sizeof(Event));
|
||||||
return data_bytes;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
return result.error();
|
return result.error();
|
||||||
|
|
|
@ -31,9 +31,9 @@ bool RandomDevice::can_read(const FileDescription&, size_t) const
|
||||||
|
|
||||||
KResultOr<size_t> RandomDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
KResultOr<size_t> RandomDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
|
||||||
{
|
{
|
||||||
return buffer.write_buffered<256>(size, [&](u8* data, size_t data_size) {
|
return buffer.write_buffered<256>(size, [&](Bytes bytes) {
|
||||||
get_good_random_bytes({ data, data_size });
|
get_good_random_bytes(bytes);
|
||||||
return data_size;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,10 +63,10 @@ KResultOr<size_t> SerialDevice::read(FileDescription&, u64, UserOrKernelBuffer&
|
||||||
if (!(get_line_status() & DataReady))
|
if (!(get_line_status() & DataReady))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return buffer.write_buffered<128>(size, [&](u8* data, size_t data_size) {
|
return buffer.write_buffered<128>(size, [&](Bytes bytes) {
|
||||||
for (size_t i = 0; i < data_size; i++)
|
for (auto& byte : bytes)
|
||||||
data[i] = m_base_addr.in<u8>();
|
byte = m_base_addr.in<u8>();
|
||||||
return data_size;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,10 +84,10 @@ KResultOr<size_t> SerialDevice::write(FileDescription& description, u64, const U
|
||||||
if (!can_write(description, size))
|
if (!can_write(description, size))
|
||||||
return EAGAIN;
|
return EAGAIN;
|
||||||
|
|
||||||
return buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) {
|
return buffer.read_buffered<128>(size, [&](ReadonlyBytes bytes) {
|
||||||
for (size_t i = 0; i < data_size; i++)
|
for (const auto& byte : bytes)
|
||||||
put_char(data[i]);
|
put_char(byte);
|
||||||
return data_size;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,23 +48,23 @@ KResultOr<size_t> InodeWatcher::read(FileDescription&, u64, UserOrKernelBuffer&
|
||||||
if (buffer_size < bytes_to_write)
|
if (buffer_size < bytes_to_write)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
||||||
auto result = buffer.write_buffered<MAXIMUM_EVENT_SIZE>(bytes_to_write, [&](u8* data, size_t data_bytes) {
|
auto result = buffer.write_buffered<MAXIMUM_EVENT_SIZE>(bytes_to_write, [&](Bytes bytes) {
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
|
|
||||||
memcpy(data + offset, &event.wd, sizeof(InodeWatcherEvent::watch_descriptor));
|
memcpy(bytes.offset(offset), &event.wd, sizeof(InodeWatcherEvent::watch_descriptor));
|
||||||
offset += sizeof(InodeWatcherEvent::watch_descriptor);
|
offset += sizeof(InodeWatcherEvent::watch_descriptor);
|
||||||
memcpy(data + offset, &event.type, sizeof(InodeWatcherEvent::type));
|
memcpy(bytes.offset(offset), &event.type, sizeof(InodeWatcherEvent::type));
|
||||||
offset += sizeof(InodeWatcherEvent::type);
|
offset += sizeof(InodeWatcherEvent::type);
|
||||||
|
|
||||||
if (!event.path.is_null()) {
|
if (!event.path.is_null()) {
|
||||||
memcpy(data + offset, &name_length, sizeof(InodeWatcherEvent::name_length));
|
memcpy(bytes.offset(offset), &name_length, sizeof(InodeWatcherEvent::name_length));
|
||||||
offset += sizeof(InodeWatcherEvent::name_length);
|
offset += sizeof(InodeWatcherEvent::name_length);
|
||||||
memcpy(data + offset, event.path.characters(), name_length);
|
memcpy(bytes.offset(offset), event.path.characters(), name_length);
|
||||||
} else {
|
} else {
|
||||||
memset(data + offset, 0, sizeof(InodeWatcherEvent::name_length));
|
memset(bytes.offset(offset), 0, sizeof(InodeWatcherEvent::name_length));
|
||||||
}
|
}
|
||||||
|
|
||||||
return data_bytes;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
evaluate_block_conditions();
|
evaluate_block_conditions();
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -478,10 +478,10 @@ bool IDEChannel::ata_do_read_sector()
|
||||||
dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector");
|
dbgln_if(PATA_DEBUG, "IDEChannel::ata_do_read_sector");
|
||||||
auto& request = *m_current_request;
|
auto& request = *m_current_request;
|
||||||
auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
auto out_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
||||||
auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](u8* buffer, size_t buffer_bytes) {
|
auto result = request.write_to_buffer_buffered<512>(out_buffer, 512, [&](Bytes bytes) {
|
||||||
for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
|
for (size_t i = 0; i < bytes.size(); i += sizeof(u16))
|
||||||
*(u16*)&buffer[i] = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
|
*(u16*)bytes.offset_pointer(i) = IO::in16(m_io_group.io_base().offset(ATA_REG_DATA).get());
|
||||||
return buffer_bytes;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
if (result.is_error()) {
|
if (result.is_error()) {
|
||||||
// TODO: Do we need to abort the PATA read if this wasn't the last block?
|
// TODO: Do we need to abort the PATA read if this wasn't the last block?
|
||||||
|
@ -520,10 +520,10 @@ void IDEChannel::ata_do_write_sector()
|
||||||
|
|
||||||
auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
auto in_buffer = request.buffer().offset(m_current_request_block_index * 512);
|
||||||
dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
|
dbgln_if(PATA_DEBUG, "IDEChannel: Writing 512 bytes (part {}) (status={:#02x})...", m_current_request_block_index, status);
|
||||||
auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](u8 const* buffer, size_t buffer_bytes) {
|
auto result = request.read_from_buffer_buffered<512>(in_buffer, 512, [&](ReadonlyBytes readonly_bytes) {
|
||||||
for (size_t i = 0; i < buffer_bytes; i += sizeof(u16))
|
for (size_t i = 0; i < readonly_bytes.size(); i += sizeof(u16))
|
||||||
IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)&buffer[i]);
|
IO::out16(m_io_group.io_base().offset(ATA_REG_DATA).get(), *(const u16*)readonly_bytes.offset(i));
|
||||||
return buffer_bytes;
|
return readonly_bytes.size();
|
||||||
});
|
});
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
complete_current_request(AsyncDeviceRequest::MemoryFault);
|
complete_current_request(AsyncDeviceRequest::MemoryFault);
|
||||||
|
|
|
@ -23,9 +23,9 @@ KResultOr<FlatPtr> Process::sys$getrandom(Userspace<void*> buffer, size_t buffer
|
||||||
auto data_buffer = UserOrKernelBuffer::for_user_buffer(buffer, buffer_size);
|
auto data_buffer = UserOrKernelBuffer::for_user_buffer(buffer, buffer_size);
|
||||||
if (!data_buffer.has_value())
|
if (!data_buffer.has_value())
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
auto result = data_buffer.value().write_buffered<1024>(buffer_size, [&](u8* buffer, size_t buffer_bytes) {
|
auto result = data_buffer.value().write_buffered<1024>(buffer_size, [&](Bytes bytes) {
|
||||||
get_good_random_bytes({ buffer, buffer_bytes });
|
get_good_random_bytes(bytes);
|
||||||
return buffer_bytes;
|
return bytes.size();
|
||||||
});
|
});
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
return result.error();
|
return result.error();
|
||||||
|
|
|
@ -67,10 +67,10 @@ void SlavePTY::echo(u8 ch)
|
||||||
|
|
||||||
void SlavePTY::on_master_write(const UserOrKernelBuffer& buffer, size_t size)
|
void SlavePTY::on_master_write(const UserOrKernelBuffer& buffer, size_t size)
|
||||||
{
|
{
|
||||||
auto result = buffer.read_buffered<128>(size, [&](u8 const* data, size_t data_size) {
|
auto result = buffer.read_buffered<128>(size, [&](ReadonlyBytes data) {
|
||||||
for (size_t i = 0; i < data_size; ++i)
|
for (const auto& byte : data)
|
||||||
emit(data[i], false);
|
emit(byte, false);
|
||||||
return data_size;
|
return data.size();
|
||||||
});
|
});
|
||||||
if (!result.is_error())
|
if (!result.is_error())
|
||||||
evaluate_block_conditions();
|
evaluate_block_conditions();
|
||||||
|
|
|
@ -52,9 +52,9 @@ KResultOr<size_t> TTY::read(FileDescription&, u64, UserOrKernelBuffer& buffer, s
|
||||||
size = m_input_buffer.size();
|
size = m_input_buffer.size();
|
||||||
|
|
||||||
bool need_evaluate_block_conditions = false;
|
bool need_evaluate_block_conditions = false;
|
||||||
auto result = buffer.write_buffered<512>(size, [&](u8* data, size_t data_size) {
|
auto result = buffer.write_buffered<512>(size, [&](Bytes data) {
|
||||||
size_t bytes_written = 0;
|
size_t bytes_written = 0;
|
||||||
for (; bytes_written < data_size; ++bytes_written) {
|
for (; bytes_written < data.size(); ++bytes_written) {
|
||||||
auto bit_index = m_input_buffer.head_index();
|
auto bit_index = m_input_buffer.head_index();
|
||||||
bool is_special_character = m_special_character_bitmask[bit_index / 8] & (1 << (bit_index % 8));
|
bool is_special_character = m_special_character_bitmask[bit_index / 8] & (1 << (bit_index % 8));
|
||||||
if (in_canonical_mode() && is_special_character) {
|
if (in_canonical_mode() && is_special_character) {
|
||||||
|
@ -88,11 +88,11 @@ KResultOr<size_t> TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr size_t num_chars = 256;
|
constexpr size_t num_chars = 256;
|
||||||
return buffer.read_buffered<num_chars>(size, [&](u8 const* data, size_t buffer_bytes) -> KResultOr<size_t> {
|
return buffer.read_buffered<num_chars>(size, [&](ReadonlyBytes bytes) -> KResultOr<size_t> {
|
||||||
u8 modified_data[num_chars * 2];
|
u8 modified_data[num_chars * 2];
|
||||||
size_t modified_data_size = 0;
|
size_t modified_data_size = 0;
|
||||||
for (size_t i = 0; i < buffer_bytes; ++i) {
|
for (const auto& byte : bytes) {
|
||||||
process_output(data[i], [&modified_data, &modified_data_size](u8 out_ch) {
|
process_output(byte, [&modified_data, &modified_data_size](u8 out_ch) {
|
||||||
modified_data[modified_data_size++] = out_ch;
|
modified_data[modified_data_size++] = out_ch;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -101,14 +101,14 @@ KResultOr<size_t> TTY::write(FileDescription&, u64, const UserOrKernelBuffer& bu
|
||||||
return bytes_written_or_error;
|
return bytes_written_or_error;
|
||||||
auto bytes_written = bytes_written_or_error.value();
|
auto bytes_written = bytes_written_or_error.value();
|
||||||
if (bytes_written == modified_data_size)
|
if (bytes_written == modified_data_size)
|
||||||
return buffer_bytes;
|
return bytes.size();
|
||||||
|
|
||||||
// Degenerate case where we converted some newlines and encountered a partial write
|
// Degenerate case where we converted some newlines and encountered a partial write
|
||||||
|
|
||||||
// Calculate where in the input buffer the last character would have been
|
// Calculate where in the input buffer the last character would have been
|
||||||
size_t pos_data = 0;
|
size_t pos_data = 0;
|
||||||
for (size_t pos_modified_data = 0; pos_modified_data < bytes_written; ++pos_data) {
|
for (size_t pos_modified_data = 0; pos_modified_data < bytes_written; ++pos_data) {
|
||||||
if (data[pos_data] == '\n')
|
if (bytes[pos_data] == '\n')
|
||||||
pos_modified_data += 2;
|
pos_modified_data += 2;
|
||||||
else
|
else
|
||||||
pos_modified_data += 1;
|
pos_modified_data += 1;
|
||||||
|
|
|
@ -260,10 +260,10 @@ void VirtualConsole::on_key_pressed(KeyEvent event)
|
||||||
KResultOr<size_t> VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, size_t size)
|
KResultOr<size_t> VirtualConsole::on_tty_write(const UserOrKernelBuffer& data, size_t size)
|
||||||
{
|
{
|
||||||
SpinlockLocker global_lock(ConsoleManagement::the().tty_write_lock());
|
SpinlockLocker global_lock(ConsoleManagement::the().tty_write_lock());
|
||||||
auto result = data.read_buffered<512>(size, [&](u8 const* buffer, size_t buffer_bytes) {
|
auto result = data.read_buffered<512>(size, [&](ReadonlyBytes buffer) {
|
||||||
for (size_t i = 0; i < buffer_bytes; ++i)
|
for (const auto& byte : buffer)
|
||||||
m_console_impl.on_input(buffer[i]);
|
m_console_impl.on_input(byte);
|
||||||
return buffer_bytes;
|
return buffer.size();
|
||||||
});
|
});
|
||||||
if (m_active)
|
if (m_active)
|
||||||
flush_dirty_lines();
|
flush_dirty_lines();
|
||||||
|
|
|
@ -89,7 +89,8 @@ public:
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
if (is_kernel_buffer()) {
|
if (is_kernel_buffer()) {
|
||||||
// We're transferring directly to a kernel buffer, bypass
|
// We're transferring directly to a kernel buffer, bypass
|
||||||
return f(m_buffer + offset, len);
|
Bytes bytes { m_buffer + offset, len };
|
||||||
|
return f(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The purpose of using a buffer on the stack is that we can
|
// The purpose of using a buffer on the stack is that we can
|
||||||
|
@ -98,7 +99,8 @@ public:
|
||||||
size_t nwritten = 0;
|
size_t nwritten = 0;
|
||||||
while (nwritten < len) {
|
while (nwritten < len) {
|
||||||
auto to_copy = min(sizeof(buffer), len - nwritten);
|
auto to_copy = min(sizeof(buffer), len - nwritten);
|
||||||
KResultOr<size_t> copied_or_error = f(buffer, to_copy);
|
Bytes bytes { buffer, to_copy };
|
||||||
|
KResultOr<size_t> copied_or_error = f(bytes);
|
||||||
if (copied_or_error.is_error())
|
if (copied_or_error.is_error())
|
||||||
return copied_or_error.error();
|
return copied_or_error.error();
|
||||||
auto copied = copied_or_error.value();
|
auto copied = copied_or_error.value();
|
||||||
|
@ -124,7 +126,7 @@ public:
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
if (is_kernel_buffer()) {
|
if (is_kernel_buffer()) {
|
||||||
// We're transferring directly from a kernel buffer, bypass
|
// We're transferring directly from a kernel buffer, bypass
|
||||||
return f(m_buffer + offset, len);
|
return f({ m_buffer + offset, len });
|
||||||
}
|
}
|
||||||
|
|
||||||
// The purpose of using a buffer on the stack is that we can
|
// The purpose of using a buffer on the stack is that we can
|
||||||
|
@ -135,7 +137,8 @@ public:
|
||||||
auto to_copy = min(sizeof(buffer), len - nread);
|
auto to_copy = min(sizeof(buffer), len - nread);
|
||||||
if (!read(buffer, nread, to_copy))
|
if (!read(buffer, nread, to_copy))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
KResultOr<size_t> copied_or_error = f(buffer, to_copy);
|
ReadonlyBytes read_only_bytes { buffer, to_copy };
|
||||||
|
KResultOr<size_t> copied_or_error = f(read_only_bytes);
|
||||||
if (copied_or_error.is_error())
|
if (copied_or_error.is_error())
|
||||||
return copied_or_error.error();
|
return copied_or_error.error();
|
||||||
auto copied = copied_or_error.value();
|
auto copied = copied_or_error.value();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue