summaryrefslogtreecommitdiff
path: root/Kernel/KBuffer.h
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-05 11:06:21 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-05 11:07:45 +0200
commit605975adb583c5baad32aa834cb2904bd2e8fc8e (patch)
tree59f91a39434b34ccfd0d68f92d5b365847bfb801 /Kernel/KBuffer.h
parent52cfe9ebae5707674e90d50b728b9c957ca215ee (diff)
downloadserenity-605975adb583c5baad32aa834cb2904bd2e8fc8e.zip
Kernel: Make KBuffer a value-type wrapper around a KBufferImpl
A KBuffer always contains a valid KBufferImpl. If you need a "null" state buffer, use Optional<KBuffer>. This makes KBuffer very easy to work with and pass around, just like ByteBuffer before it.
Diffstat (limited to 'Kernel/KBuffer.h')
-rw-r--r--Kernel/KBuffer.h38
1 files changed, 33 insertions, 5 deletions
diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h
index 95d1d5e51a..53af587ce3 100644
--- a/Kernel/KBuffer.h
+++ b/Kernel/KBuffer.h
@@ -4,16 +4,16 @@
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/Region.h>
-class KBuffer : public RefCounted<KBuffer> {
+class KBufferImpl : public RefCounted<KBufferImpl> {
public:
- static NonnullRefPtr<KBuffer> create_with_size(size_t size)
+ static NonnullRefPtr<KBufferImpl> create_with_size(size_t size)
{
auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(size), "KBuffer");
ASSERT(region);
- return adopt(*new KBuffer(*region, size));
+ return adopt(*new KBufferImpl(*region, size));
}
- static NonnullRefPtr<KBuffer> copy(const void* data, size_t size)
+ static NonnullRefPtr<KBufferImpl> copy(const void* data, size_t size)
{
auto buffer = create_with_size(size);
memcpy(buffer->data(), data, size);
@@ -26,7 +26,7 @@ public:
size_t capacity() const { return m_region->size(); }
private:
- explicit KBuffer(NonnullRefPtr<Region>&& region, size_t size)
+ explicit KBufferImpl(NonnullRefPtr<Region>&& region, size_t size)
: m_size(size)
, m_region(move(region))
{
@@ -35,3 +35,31 @@ private:
size_t m_size { 0 };
NonnullRefPtr<Region> m_region;
};
+
+class KBuffer {
+public:
+ static KBuffer create_with_size(size_t size)
+ {
+ return KBuffer(KBufferImpl::create_with_size(size));
+ }
+
+ static KBuffer copy(const void* data, size_t size)
+ {
+ return KBuffer(KBufferImpl::copy(data, size));
+ }
+
+ u8* data() { return m_impl->data(); }
+ const u8* data() const { return m_impl->data(); }
+ size_t size() const { return m_impl->size(); }
+ size_t capacity() const { return m_impl->size(); }
+
+ const KBufferImpl& impl() const { return m_impl; }
+
+ KBuffer(NonnullRefPtr<KBufferImpl>&& impl)
+ : m_impl(move(impl))
+ {
+ }
+
+private:
+ NonnullRefPtr<KBufferImpl> m_impl;
+};