summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-04 21:04:27 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-04 21:07:20 +0200
commit609495882fc2ff8bd1369167f31efd00fa19b700 (patch)
treeca34cb0b4350068cf789b4d27b973d9de29223b8
parentc06993b7cfa1ba594f6680268ff9a85231355c20 (diff)
downloadserenity-609495882fc2ff8bd1369167f31efd00fa19b700.zip
Kernel: Add KBuffer, a simple byte buffer backed by kernel-only memory
This memory is not accessible to userspace and comes from the kernel page allocator, not from the kmalloc heap. This makes it ideal for larger allocations.
-rw-r--r--Kernel/KBuffer.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h
new file mode 100644
index 0000000000..95d1d5e51a
--- /dev/null
+++ b/Kernel/KBuffer.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <AK/Assertions.h>
+#include <Kernel/VM/MemoryManager.h>
+#include <Kernel/VM/Region.h>
+
+class KBuffer : public RefCounted<KBuffer> {
+public:
+ static NonnullRefPtr<KBuffer> 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));
+ }
+
+ static NonnullRefPtr<KBuffer> copy(const void* data, size_t size)
+ {
+ auto buffer = create_with_size(size);
+ memcpy(buffer->data(), data, size);
+ return buffer;
+ }
+
+ u8* data() { return m_region->vaddr().as_ptr(); }
+ const u8* data() const { return m_region->vaddr().as_ptr(); }
+ size_t size() const { return m_size; }
+ size_t capacity() const { return m_region->size(); }
+
+private:
+ explicit KBuffer(NonnullRefPtr<Region>&& region, size_t size)
+ : m_size(size)
+ , m_region(move(region))
+ {
+ }
+
+ size_t m_size { 0 };
+ NonnullRefPtr<Region> m_region;
+};