diff options
-rw-r--r-- | Kernel/DoubleBuffer.cpp | 27 | ||||
-rw-r--r-- | Kernel/DoubleBuffer.h | 27 | ||||
-rw-r--r-- | Kernel/Makefile | 1 | ||||
-rw-r--r-- | Kernel/TTY.cpp | 26 | ||||
-rw-r--r-- | Kernel/TTY.h | 24 |
5 files changed, 56 insertions, 49 deletions
diff --git a/Kernel/DoubleBuffer.cpp b/Kernel/DoubleBuffer.cpp new file mode 100644 index 0000000000..3937ed2740 --- /dev/null +++ b/Kernel/DoubleBuffer.cpp @@ -0,0 +1,27 @@ +#include "DoubleBuffer.h" + +void DoubleBuffer::flip() +{ + ASSERT(m_read_buffer_index == m_read_buffer->size()); + swap(m_read_buffer, m_write_buffer); + m_write_buffer->clear(); + m_read_buffer_index = 0; +} + +ssize_t DoubleBuffer::write(const byte* data, size_t size) +{ + m_write_buffer->append(data, size); + return size; +} + +ssize_t DoubleBuffer::read(byte* data, size_t size) +{ + if (m_read_buffer_index >= m_read_buffer->size() && !m_write_buffer->isEmpty()) + flip(); + if (m_read_buffer_index >= m_read_buffer->size()) + return 0; + ssize_t nread = min(m_read_buffer->size() - m_read_buffer_index, size); + memcpy(data, m_read_buffer->data() + m_read_buffer_index, nread); + m_read_buffer_index += nread; + return nread; +} diff --git a/Kernel/DoubleBuffer.h b/Kernel/DoubleBuffer.h new file mode 100644 index 0000000000..46e0e56da3 --- /dev/null +++ b/Kernel/DoubleBuffer.h @@ -0,0 +1,27 @@ +#pragma once + +#include <AK/Types.h> +#include <AK/Vector.h> + +class DoubleBuffer { +public: + DoubleBuffer() + : m_write_buffer(&m_buffer1) + , m_read_buffer(&m_buffer2) + { + } + + ssize_t write(const byte*, size_t); + ssize_t read(byte*, size_t); + + bool is_empty() const { return m_read_buffer_index >= m_read_buffer->size() && m_write_buffer->isEmpty(); } + +private: + void flip(); + + Vector<byte>* m_write_buffer { nullptr }; + Vector<byte>* m_read_buffer { nullptr }; + Vector<byte> m_buffer1; + Vector<byte> m_buffer2; + size_t m_read_buffer_index { 0 }; +}; diff --git a/Kernel/Makefile b/Kernel/Makefile index 6195ec00cb..68628d2700 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -21,6 +21,7 @@ KERNEL_OBJS = \ VirtualConsole.o \ FIFO.o \ Scheduler.o \ + DoubleBuffer.o \ ELFImage.o \ ELFLoader.o diff --git a/Kernel/TTY.cpp b/Kernel/TTY.cpp index 775250bc2a..11f78a1a50 100644 --- a/Kernel/TTY.cpp +++ b/Kernel/TTY.cpp @@ -6,32 +6,6 @@ //#define TTY_DEBUG -void DoubleBuffer::flip() -{ - ASSERT(m_read_buffer_index == m_read_buffer->size()); - swap(m_read_buffer, m_write_buffer); - m_write_buffer->clear(); - m_read_buffer_index = 0; -} - -ssize_t DoubleBuffer::write(const byte* data, size_t size) -{ - m_write_buffer->append(data, size); - return size; -} - -ssize_t DoubleBuffer::read(byte* data, size_t size) -{ - if (m_read_buffer_index >= m_read_buffer->size() && !m_write_buffer->isEmpty()) - flip(); - if (m_read_buffer_index >= m_read_buffer->size()) - return 0; - ssize_t nread = min(m_read_buffer->size() - m_read_buffer_index, size); - memcpy(data, m_read_buffer->data() + m_read_buffer_index, nread); - m_read_buffer_index += nread; - return nread; -} - TTY::TTY(unsigned major, unsigned minor) : CharacterDevice(major, minor) { diff --git a/Kernel/TTY.h b/Kernel/TTY.h index a5e6385489..eb1f18d74f 100644 --- a/Kernel/TTY.h +++ b/Kernel/TTY.h @@ -1,33 +1,11 @@ #pragma once +#include "DoubleBuffer.h" #include <VirtualFileSystem/CharacterDevice.h> #include <VirtualFileSystem/UnixTypes.h> class Process; -class DoubleBuffer { -public: - DoubleBuffer() - : m_write_buffer(&m_buffer1) - , m_read_buffer(&m_buffer2) - { - } - - ssize_t write(const byte*, size_t); - ssize_t read(byte*, size_t); - - bool is_empty() const { return m_read_buffer_index >= m_read_buffer->size() && m_write_buffer->isEmpty(); } - -private: - void flip(); - - Vector<byte>* m_write_buffer { nullptr }; - Vector<byte>* m_read_buffer { nullptr }; - Vector<byte> m_buffer1; - Vector<byte> m_buffer2; - size_t m_read_buffer_index { 0 }; -}; - class TTY : public CharacterDevice { public: virtual ~TTY() override; |