diff --git a/kernel/src/fs/devices/ConsoleDevice.cpp b/kernel/src/fs/devices/ConsoleDevice.cpp index 6f1218e9..0d469c65 100644 --- a/kernel/src/fs/devices/ConsoleDevice.cpp +++ b/kernel/src/fs/devices/ConsoleDevice.cpp @@ -49,13 +49,7 @@ Result ConsoleDevice::read(u8* buf, usize, usize length) const { TRY(handle_background_process_group(false, SIGTTIN)); - if (length > m_input_buffer.size()) length = m_input_buffer.size(); - - 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(); + length = m_input_buffer.dequeue_data(buf, length); if (!length && m_may_read_without_blocking) m_may_read_without_blocking = false; diff --git a/libluna/include/luna/Buffer.h b/libluna/include/luna/Buffer.h index 405c7dd9..6b378815 100644 --- a/libluna/include/luna/Buffer.h +++ b/libluna/include/luna/Buffer.h @@ -21,6 +21,8 @@ class Buffer Result append_data(const u8* data, usize size); + usize dequeue_data(u8* data, usize size); + u8* data() { return m_data; diff --git a/libluna/src/Buffer.cpp b/libluna/src/Buffer.cpp index 960e848d..57f53051 100644 --- a/libluna/src/Buffer.cpp +++ b/libluna/src/Buffer.cpp @@ -53,6 +53,20 @@ Result Buffer::append_data(const u8* data, usize size) 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* data = m_data;