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

驱动注册表 — 以 ETL vector 存储 DriverEntry,并附带 flat_map 兼容索引。 More...

#include <driver_registry.hpp>

Collaboration diagram for DriverRegistry:
Collaboration graph

Public Member Functions

auto Register (const DriverEntry &entry) -> Expected< void >
 注册一个驱动条目。
 
auto FindDriver (const DeviceNode &node) -> const DriverEntry *
 查找 match_table 中含有 node.compatible 字符串的第一个驱动 (flat_map 查找,O(Cn · log T))。
 
构造 / 析构
 DriverRegistry ()=default
 
 ~DriverRegistry ()=default
 
 DriverRegistry (const DriverRegistry &)=delete
 
 DriverRegistry (DriverRegistry &&)=delete
 
auto operator= (const DriverRegistry &) -> DriverRegistry &=delete
 
auto operator= (DriverRegistry &&) -> DriverRegistry &=delete
 

Private Attributes

etl::vector< DriverEntry, kMaxDriversdrivers_
 
etl::flat_map< const char *, size_t, kMaxCompatEntries, CStrLesscompat_map_
 
SpinLock lock_ {"driver_registry"}
 

Static Private Attributes

static constexpr size_t kMaxDrivers = 32
 
static constexpr size_t kMaxCompatEntries = 96
 所有驱动 MatchEntry 行数上限(32 个驱动 × 约 3 条 compatible 字符串)
 

Detailed Description

驱动注册表 — 以 ETL vector 存储 DriverEntry,并附带 flat_map 兼容索引。

注册时构建 etl::flat_map(compatible 字符串 → 驱动索引), 将 FindDriver 的复杂度从 O(N·M·K) 降至 O(Cn · log T)。

Definition at line 62 of file driver_registry.hpp.

Constructor & Destructor Documentation

◆ DriverRegistry() [1/3]

DriverRegistry::DriverRegistry ( )
default

◆ ~DriverRegistry()

DriverRegistry::~DriverRegistry ( )
default

◆ DriverRegistry() [2/3]

DriverRegistry::DriverRegistry ( const DriverRegistry )
delete

◆ DriverRegistry() [3/3]

DriverRegistry::DriverRegistry ( DriverRegistry &&  )
delete

Member Function Documentation

◆ FindDriver()

auto DriverRegistry::FindDriver ( const DeviceNode node) -> const DriverEntry*
inline

查找 match_table 中含有 node.compatible 字符串的第一个驱动 (flat_map 查找,O(Cn · log T))。

Returns
DriverEntry 指针,若无匹配则返回 nullptr

Definition at line 93 of file driver_registry.hpp.

93 {
94 // 遍历节点的 compatible 字符串列表,对每个字符串执行 flat_map 查找。
95 const char* p = node.compatible;
96 const char* end = node.compatible + node.compatible_len;
97 while (p < end) {
98 const auto it = compat_map_.find(p);
99 if (it != compat_map_.end()) {
100 auto& entry = drivers_[it->second];
101 if (entry.match(const_cast<DeviceNode&>(node))) {
102 return &entry;
103 }
104 }
105 p += kstd::strlen(p) + 1;
106 }
107 return nullptr;
108 }
void * end[]
内核结束
etl::flat_map< const char *, size_t, kMaxCompatEntries, CStrLess > compat_map_
etl::vector< DriverEntry, kMaxDrivers > drivers_
单个设备的硬件资源描述。
size_t compatible_len
char compatible[128]
FDT compatible 字符串列表(以 '\0' 分隔,如 "ns16550a\0ns16550\0")

◆ operator=() [1/2]

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

◆ operator=() [2/2]

auto DriverRegistry::operator= ( DriverRegistry &&  ) -> DriverRegistry &=delete
delete

◆ Register()

auto DriverRegistry::Register ( const DriverEntry entry) -> Expected<void>
inline

注册一个驱动条目。

Precondition
entry.match/probe/remove 委托已绑定
Returns
Expected<void> 注册表已满时返回 kOutOfMemory

Definition at line 70 of file driver_registry.hpp.

70 {
71 LockGuard guard(lock_);
72 if (drivers_.full()) {
73 return std::unexpected(Error(ErrorCode::kOutOfMemory));
74 }
75 const size_t idx = drivers_.size();
76 for (const auto& me : entry.match_table) {
77 if (compat_map_.full()) {
78 return std::unexpected(Error(ErrorCode::kOutOfMemory));
79 }
80 // 重复键时 insert() 为空操作 — 先注册的驱动优先。
81 compat_map_.insert({me.compatible, idx});
82 }
83 drivers_.push_back(entry);
84 return {};
85 }
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
错误类型,用于 std::expected
Definition expected.hpp:343

Member Data Documentation

◆ compat_map_

etl::flat_map<const char*, size_t, kMaxCompatEntries, CStrLess> DriverRegistry::compat_map_
private

Definition at line 126 of file driver_registry.hpp.

◆ drivers_

etl::vector<DriverEntry, kMaxDrivers> DriverRegistry::drivers_
private

Definition at line 125 of file driver_registry.hpp.

◆ kMaxCompatEntries

constexpr size_t DriverRegistry::kMaxCompatEntries = 96
staticconstexprprivate

所有驱动 MatchEntry 行数上限(32 个驱动 × 约 3 条 compatible 字符串)

Definition at line 123 of file driver_registry.hpp.

◆ kMaxDrivers

constexpr size_t DriverRegistry::kMaxDrivers = 32
staticconstexprprivate

Definition at line 121 of file driver_registry.hpp.

◆ lock_

SpinLock DriverRegistry::lock_ {"driver_registry"}
private

Definition at line 127 of file driver_registry.hpp.

127{"driver_registry"};

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