SimpleKernel 1.17.0
Loading...
Searching...
No Matches
IoBuffer Class Reference

动态分配、对齐 IO 缓冲区的 RAII 封装 More...

#include <io_buffer.hpp>

Collaboration diagram for IoBuffer:
Collaboration graph

Public Member Functions

auto GetBuffer () const -> std::span< const uint8_t >
 获取缓冲区数据与大小 (只读)
 
auto GetBuffer () -> std::span< uint8_t >
 获取缓冲区数据与大小
 
auto IsValid () const -> bool
 检查缓冲区是否有效
 
auto ToDmaRegion (VirtToPhysFunc v2p=IdentityVirtToPhys) const -> DmaRegion
 创建此缓冲区的 DmaRegion 视图
 
构造/析构函数
 IoBuffer ()=default
 
 IoBuffer (size_t size, size_t alignment=kDefaultAlignment)
 构造函数
 
 ~IoBuffer ()
 
 IoBuffer (const IoBuffer &)=delete
 
auto operator= (const IoBuffer &) -> IoBuffer &=delete
 
 IoBuffer (IoBuffer &&other)
 
auto operator= (IoBuffer &&other) noexcept -> IoBuffer &
 

Static Public Attributes

static constexpr size_t kDefaultAlignment = 4096
 IO 缓冲区的默认对齐大小(如页大小)
 

Private Attributes

uint8_t * data_ {nullptr}
 缓冲区数据指针
 
size_t size_ {0}
 缓冲区大小
 

Detailed Description

动态分配、对齐 IO 缓冲区的 RAII 封装

Precondition
Postcondition
缓冲区内存已正确分配并对齐,析构时自动释放

Definition at line 85 of file io_buffer.hpp.

Constructor & Destructor Documentation

◆ IoBuffer() [1/4]

IoBuffer::IoBuffer ( )
default

◆ IoBuffer() [2/4]

IoBuffer::IoBuffer ( size_t  size,
size_t  alignment = kDefaultAlignment 
)
explicit

构造函数

IoBuffer mock for unit tests — uses malloc/free instead of aligned_alloc which requires the memory subsystem.

Parameters
size缓冲区大小
alignment对齐要求
Precondition
size > 0, alignment 必须是 2 的幂
Postcondition
缓冲区内存已正确分配并对齐

Definition at line 12 of file io_buffer.cpp.

12 {
13 assert(size > 0 && "IoBuffer size must be greater than 0");
14 assert((alignment & (alignment - 1)) == 0 &&
15 "IoBuffer alignment must be a power of 2");
16
17 auto* data = static_cast<uint8_t*>(aligned_alloc(alignment, size));
18 assert(data != nullptr && "IoBuffer aligned_alloc failed");
19 data_ = data;
20 size_ = size;
21}
uint8_t * data_
缓冲区数据指针
size_t size_
缓冲区大小
void * aligned_alloc(size_t alignment, size_t size)
Definition memory.cpp:58
Here is the call graph for this function:

◆ ~IoBuffer()

IoBuffer::~IoBuffer ( )

Definition at line 23 of file io_buffer.cpp.

23 {
24 assert((data_ == nullptr) == (size_ == 0) &&
25 "IoBuffer invariant violated: data_ and size_ must be consistent");
27}
void aligned_free(void *ptr)
Definition memory.cpp:65
Here is the call graph for this function:

◆ IoBuffer() [3/4]

IoBuffer::IoBuffer ( const IoBuffer )
delete

◆ IoBuffer() [4/4]

IoBuffer::IoBuffer ( IoBuffer &&  other)

Definition at line 29 of file io_buffer.cpp.

29 : data_(other.data_), size_(other.size_) {
30 other.data_ = nullptr;
31 other.size_ = 0;
32}

Member Function Documentation

◆ GetBuffer() [1/2]

auto IoBuffer::GetBuffer ( ) -> std::span<uint8_t>

获取缓冲区数据与大小

Returns
std::span<uint8_t> 缓冲区数据的可变视图
Precondition
None
Postcondition
返回指向缓冲区数据的 span

Definition at line 52 of file io_buffer.cpp.

52{ return {data_, size_}; }

◆ GetBuffer() [2/2]

auto IoBuffer::GetBuffer ( ) const -> std::span<const uint8_t>

获取缓冲区数据与大小 (只读)

Returns
std::span<const uint8_t> 缓冲区数据的只读视图
Precondition
None
Postcondition
返回指向缓冲区数据的常量 span

Definition at line 48 of file io_buffer.cpp.

48 {
49 return {data_, size_};
50}

◆ IsValid()

auto IoBuffer::IsValid ( ) const -> bool

检查缓冲区是否有效

Returns
bool 有效则返回 true
Precondition
None
Postcondition
返回缓冲区是否已分配且有效的状态

Definition at line 54 of file io_buffer.cpp.

54{ return data_ != nullptr; }

◆ operator=() [1/2]

auto IoBuffer::operator= ( const IoBuffer ) -> IoBuffer &=delete
delete

◆ operator=() [2/2]

auto IoBuffer::operator= ( IoBuffer &&  other) -> IoBuffer&
noexcept

Definition at line 34 of file io_buffer.cpp.

34 {
35 assert(this != &other && "Self-move assignment is not allowed");
36
37 if (data_ != nullptr) {
39 }
40 data_ = other.data_;
41 size_ = other.size_;
42 other.data_ = nullptr;
43 other.size_ = 0;
44
45 return *this;
46}
Here is the call graph for this function:

◆ ToDmaRegion()

auto IoBuffer::ToDmaRegion ( VirtToPhysFunc  v2p = IdentityVirtToPhys) const -> DmaRegion

创建此缓冲区的 DmaRegion 视图

Parameters
v2p地址转换函数(默认:恒等映射)
Returns
描述此缓冲区内存的 DmaRegion
Precondition
IsValid() == true
Postcondition
返回的 DmaRegion 不拥有内存所有权

Definition at line 56 of file io_buffer.cpp.

56 {
57 return DmaRegion{
58 .virt = data_,
59 .phys = v2p(reinterpret_cast<uintptr_t>(data_)),
60 .size = size_,
61 };
62}
DMA 可访问内存区域的非拥有描述符
Definition io_buffer.hpp:37
void * virt
虚拟(CPU 可访问)基地址
Definition io_buffer.hpp:39

Member Data Documentation

◆ data_

uint8_t* IoBuffer::data_ {nullptr}
private

缓冲区数据指针

Definition at line 150 of file io_buffer.hpp.

150{nullptr};

◆ kDefaultAlignment

constexpr size_t IoBuffer::kDefaultAlignment = 4096
staticconstexpr

IO 缓冲区的默认对齐大小(如页大小)

Definition at line 88 of file io_buffer.hpp.

◆ size_

size_t IoBuffer::size_ {0}
private

缓冲区大小

Definition at line 152 of file io_buffer.hpp.

152{0};

The documentation for this class was generated from the following files: