summaryrefslogtreecommitdiff
path: root/Kernel/DoubleBuffer.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-01-20 16:08:49 +0100
committerAndreas Kling <kling@serenityos.org>2020-01-20 16:08:49 +0100
commitf4f958f99f9c732525d1846c9ee5cc20cef7dea6 (patch)
tree6ac5552e3bf5134725439348f515f84e0ef27f26 /Kernel/DoubleBuffer.h
parent0a282e0a0231c961f769e585a83ca4fe565bc6fa (diff)
downloadserenity-f4f958f99f9c732525d1846c9ee5cc20cef7dea6.zip
Kernel: Make DoubleBuffer use a KBuffer instead of kmalloc()ing
Background: DoubleBuffer is a handy buffer class in the kernel that allows you to keep writing to it from the "outside" while the "inside" reads from it. It's used for things like LocalSocket and TTY's. Internally, it has a read buffer and a write buffer, but the two will swap places when the read buffer is exhausted (by reading from it.) Before this patch, it was internally implemented as two Vector<u8> that we would swap between when the reader side had exhausted the data in the read buffer. Now instead we preallocate a large KBuffer (64KB*2) on DoubleBuffer construction and use that throughout its lifetime. This removes all the kmalloc heap traffic caused by DoubleBuffers :^)
Diffstat (limited to 'Kernel/DoubleBuffer.h')
-rw-r--r--Kernel/DoubleBuffer.h34
1 files changed, 19 insertions, 15 deletions
diff --git a/Kernel/DoubleBuffer.h b/Kernel/DoubleBuffer.h
index 93e3b08219..834d769691 100644
--- a/Kernel/DoubleBuffer.h
+++ b/Kernel/DoubleBuffer.h
@@ -27,34 +27,38 @@
#pragma once
#include <AK/Types.h>
-#include <AK/Vector.h>
+#include <Kernel/KBuffer.h>
#include <Kernel/Lock.h>
class DoubleBuffer {
public:
- DoubleBuffer()
- : m_write_buffer(&m_buffer1)
- , m_read_buffer(&m_buffer2)
- {
- }
+ explicit DoubleBuffer(size_t capacity = 65536);
ssize_t write(const u8*, ssize_t);
ssize_t read(u8*, ssize_t);
bool is_empty() const { return m_empty; }
- // FIXME: Isn't this racy? What if we get interrupted between getting the buffer pointer and dereferencing it?
- ssize_t bytes_in_write_buffer() const { return (ssize_t)m_write_buffer->size(); }
+ size_t space_for_writing() const { return m_space_for_writing; }
private:
void flip();
- void compute_emptiness();
+ void compute_lockfree_metadata();
- Vector<u8>* m_write_buffer { nullptr };
- Vector<u8>* m_read_buffer { nullptr };
- Vector<u8> m_buffer1;
- Vector<u8> m_buffer2;
- ssize_t m_read_buffer_index { 0 };
+ struct InnerBuffer {
+ u8* data { nullptr };
+ size_t size;
+ };
+
+ InnerBuffer* m_write_buffer { nullptr };
+ InnerBuffer* m_read_buffer { nullptr };
+ InnerBuffer m_buffer1;
+ InnerBuffer m_buffer2;
+
+ KBuffer m_storage;
+ size_t m_capacity { 0 };
+ size_t m_read_buffer_index { 0 };
+ size_t m_space_for_writing { 0 };
bool m_empty { true };
- Lock m_lock { "DoubleBuffer" };
+ mutable Lock m_lock { "DoubleBuffer" };
};