diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-08-04 21:04:27 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-08-04 21:07:20 +0200 |
commit | 609495882fc2ff8bd1369167f31efd00fa19b700 (patch) | |
tree | ca34cb0b4350068cf789b4d27b973d9de29223b8 | |
parent | c06993b7cfa1ba594f6680268ff9a85231355c20 (diff) | |
download | serenity-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.h | 37 |
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; +}; |