SimpleKernel 1.17.0
Loading...
Searching...
No Matches
io_buffer.cpp
Go to the documentation of this file.
1
5#include "io_buffer.hpp"
6
7#include <cassert>
8
9#include "kernel_log.hpp"
10#include "sk_stdlib.h"
11
12IoBuffer::IoBuffer(size_t size, size_t alignment) {
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}
22
24 assert((data_ == nullptr) == (size_ == 0) &&
25 "IoBuffer invariant violated: data_ and size_ must be consistent");
27}
28
29IoBuffer::IoBuffer(IoBuffer&& other) : data_(other.data_), size_(other.size_) {
30 other.data_ = nullptr;
31 other.size_ = 0;
32}
33
34auto IoBuffer::operator=(IoBuffer&& other) noexcept -> IoBuffer& {
35 assert(this != &other && "Self-move assignment is not allowed");
36
37 if (data_ != nullptr) {
38 aligned_free(data_);
39 }
40 data_ = other.data_;
41 size_ = other.size_;
42 other.data_ = nullptr;
43 other.size_ = 0;
44
45 return *this;
46}
47
48auto IoBuffer::GetBuffer() const -> std::span<const uint8_t> {
49 return {data_, size_};
50}
51
52auto IoBuffer::GetBuffer() -> std::span<uint8_t> { return {data_, size_}; }
53
54auto IoBuffer::IsValid() const -> bool { return data_ != nullptr; }
55
57 return DmaRegion{
58 .virt = data_,
59 .phys = v2p(reinterpret_cast<uintptr_t>(data_)),
60 .size = size_,
61 };
62}
动态分配、对齐 IO 缓冲区的 RAII 封装
Definition io_buffer.hpp:85
auto GetBuffer() const -> std::span< const uint8_t >
获取缓冲区数据与大小 (只读)
Definition io_buffer.cpp:48
uint8_t * data_
缓冲区数据指针
auto ToDmaRegion(VirtToPhysFunc v2p=IdentityVirtToPhys) const -> DmaRegion
创建此缓冲区的 DmaRegion 视图
Definition io_buffer.cpp:56
size_t size_
缓冲区大小
IoBuffer()=default
auto operator=(const IoBuffer &) -> IoBuffer &=delete
auto IsValid() const -> bool
检查缓冲区是否有效
Definition io_buffer.cpp:54
auto(*)(uintptr_t virt) -> uintptr_t VirtToPhysFunc
虚拟地址到物理地址转换回调类型
Definition io_buffer.hpp:18
void aligned_free(void *ptr)
Definition memory.cpp:65
void * aligned_alloc(size_t alignment, size_t size)
Definition memory.cpp:58
DMA 可访问内存区域的非拥有描述符
Definition io_buffer.hpp:37
void * virt
虚拟(CPU 可访问)基地址
Definition io_buffer.hpp:39