kernel+libluna: Add Buffer::dequeue_data()

This commit is contained in:
apio 2023-07-27 16:35:52 +02:00
parent 48f5f6ea33
commit 1b596027c1
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 17 additions and 7 deletions

View File

@ -49,13 +49,7 @@ Result<usize> ConsoleDevice::read(u8* buf, usize, usize length) const
{ {
TRY(handle_background_process_group(false, SIGTTIN)); TRY(handle_background_process_group(false, SIGTTIN));
if (length > m_input_buffer.size()) length = m_input_buffer.size(); length = m_input_buffer.dequeue_data(buf, length);
memcpy(buf, m_input_buffer.data(), length);
memmove(m_input_buffer.data(), m_input_buffer.data() + length, m_input_buffer.size() - length);
m_input_buffer.try_resize(m_input_buffer.size() - length).release_value();
if (!length && m_may_read_without_blocking) m_may_read_without_blocking = false; if (!length && m_may_read_without_blocking) m_may_read_without_blocking = false;

View File

@ -21,6 +21,8 @@ class Buffer
Result<void> append_data(const u8* data, usize size); Result<void> append_data(const u8* data, usize size);
usize dequeue_data(u8* data, usize size);
u8* data() u8* data()
{ {
return m_data; return m_data;

View File

@ -53,6 +53,20 @@ Result<void> Buffer::append_data(const u8* data, usize size)
return {}; return {};
} }
usize Buffer::dequeue_data(u8* data, usize size)
{
if (size > m_size) size = m_size;
if (!size) return 0;
memcpy(data, m_data, size);
memmove(m_data, m_data + size, m_size - size);
m_size -= size;
return size;
}
u8* Buffer::release_data() u8* Buffer::release_data()
{ {
u8* data = m_data; u8* data = m_data;