mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:07:34 +00:00
Kernel: Create VM::RingBuffer class
This commit is contained in:
parent
8131c0de8c
commit
13d5cdcd08
3 changed files with 81 additions and 0 deletions
|
@ -236,6 +236,7 @@ set(KERNEL_SOURCES
|
||||||
VM/Range.cpp
|
VM/Range.cpp
|
||||||
VM/RangeAllocator.cpp
|
VM/RangeAllocator.cpp
|
||||||
VM/Region.cpp
|
VM/Region.cpp
|
||||||
|
VM/RingBuffer.cpp
|
||||||
VM/ScatterGatherList.cpp
|
VM/ScatterGatherList.cpp
|
||||||
VM/SharedInodeVMObject.cpp
|
VM/SharedInodeVMObject.cpp
|
||||||
VM/Space.cpp
|
VM/Space.cpp
|
||||||
|
|
47
Kernel/VM/RingBuffer.cpp
Normal file
47
Kernel/VM/RingBuffer.cpp
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Kernel/UserOrKernelBuffer.h>
|
||||||
|
#include <Kernel/VM/MemoryManager.h>
|
||||||
|
#include <Kernel/VM/RingBuffer.h>
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
RingBuffer::RingBuffer(String region_name, size_t capacity)
|
||||||
|
: m_region(MM.allocate_contiguous_kernel_region(page_round_up(capacity), move(region_name), Region::Access::Read | Region::Access::Write))
|
||||||
|
, m_capacity_in_bytes(capacity)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RingBuffer::copy_data_in(const UserOrKernelBuffer& buffer, size_t offset, size_t length, PhysicalAddress& start_of_copied_data, size_t& bytes_copied)
|
||||||
|
{
|
||||||
|
size_t start_of_free_area = (m_start_of_used + m_num_used_bytes) % m_capacity_in_bytes;
|
||||||
|
bytes_copied = min(m_capacity_in_bytes - m_num_used_bytes, min(m_capacity_in_bytes - start_of_free_area, length));
|
||||||
|
if (bytes_copied == 0)
|
||||||
|
return false;
|
||||||
|
if (buffer.read(m_region->vaddr().offset(start_of_free_area).as_ptr(), offset, bytes_copied)) {
|
||||||
|
m_num_used_bytes += bytes_copied;
|
||||||
|
start_of_copied_data = m_region->physical_page(start_of_free_area / PAGE_SIZE)->paddr().offset(start_of_free_area % PAGE_SIZE);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RingBuffer::reclaim_space(PhysicalAddress chunk_start, size_t chunk_size)
|
||||||
|
{
|
||||||
|
VERIFY(start_of_used() == chunk_start);
|
||||||
|
VERIFY(m_num_used_bytes >= chunk_size);
|
||||||
|
m_num_used_bytes -= chunk_size;
|
||||||
|
m_start_of_used += chunk_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
PhysicalAddress RingBuffer::start_of_used() const
|
||||||
|
{
|
||||||
|
size_t start = m_start_of_used % m_capacity_in_bytes;
|
||||||
|
return m_region->physical_page(start / PAGE_SIZE)->paddr().offset(start % PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
Kernel/VM/RingBuffer.h
Normal file
33
Kernel/VM/RingBuffer.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Sahan Fernando <sahan.h.fernando@gmail.com>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <Kernel/PhysicalAddress.h>
|
||||||
|
|
||||||
|
namespace Kernel {
|
||||||
|
|
||||||
|
class RingBuffer {
|
||||||
|
public:
|
||||||
|
RingBuffer(String region_name, size_t capacity);
|
||||||
|
|
||||||
|
bool has_space() const { return m_num_used_bytes < m_capacity_in_bytes; }
|
||||||
|
bool copy_data_in(const UserOrKernelBuffer& buffer, size_t offset, size_t length, PhysicalAddress& start_of_copied_data, size_t& bytes_copied);
|
||||||
|
void reclaim_space(PhysicalAddress chunk_start, size_t chunk_size);
|
||||||
|
PhysicalAddress start_of_used() const;
|
||||||
|
|
||||||
|
SpinLock<u8>& lock() { return m_lock; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
OwnPtr<Region> m_region;
|
||||||
|
SpinLock<u8> m_lock;
|
||||||
|
size_t m_start_of_used {};
|
||||||
|
size_t m_num_used_bytes {};
|
||||||
|
size_t m_capacity_in_bytes {};
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue