mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
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.
This commit is contained in:
parent
c06993b7cf
commit
609495882f
1 changed files with 37 additions and 0 deletions
37
Kernel/KBuffer.h
Normal file
37
Kernel/KBuffer.h
Normal file
|
@ -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;
|
||||
};
|
Loading…
Add table
Reference in a new issue