Kernel/USB: Tidy up UHCIDescriptorPool construction

- Use KResultOr<NonnullRefPtr<UHCIDescriptorPool<T>>
- Make the constructor private
- Use TRY() at call sites
This commit is contained in:
Andreas Kling 2021-09-06 00:51:35 +02:00
parent 91fe6b6552
commit cb71a73708
2 changed files with 20 additions and 28 deletions

View file

@ -130,11 +130,7 @@ KResult UHCIController::reset()
UNMAP_AFTER_INIT KResult UHCIController::create_structures()
{
m_queue_head_pool = UHCIDescriptorPool<QueueHead>::try_create("Queue Head Pool");
if (!m_queue_head_pool) {
dmesgln("UHCI: Failed to create Queue Head Pool!");
return ENOMEM;
}
m_queue_head_pool = TRY(UHCIDescriptorPool<QueueHead>::try_create("Queue Head Pool"sv));
// Create the Full Speed, Low Speed Control and Bulk Queue Heads
m_interrupt_transfer_queue = allocate_queue_head();
@ -146,11 +142,7 @@ UNMAP_AFTER_INIT KResult UHCIController::create_structures()
// Now the Transfer Descriptor pool
auto td_pool_vmobject = TRY(Memory::AnonymousVMObject::try_create_physically_contiguous_with_size(PAGE_SIZE));
m_transfer_descriptor_pool = UHCIDescriptorPool<TransferDescriptor>::try_create("Transfer Descriptor Pool");
if (!m_transfer_descriptor_pool) {
dmesgln("UHCI: Failed to create Transfer Descriptor Pool!");
return ENOMEM;
}
m_transfer_descriptor_pool = TRY(UHCIDescriptorPool<TransferDescriptor>::try_create("Transfer Descriptor Pool"sv));
m_isochronous_transfer_pool = MM.allocate_kernel_region_with_vmobject(move(td_pool_vmobject), PAGE_SIZE, "UHCI Isochronous Descriptor Pool", Memory::Region::Access::ReadWrite);
if (!m_isochronous_transfer_pool) {

View file

@ -20,36 +20,23 @@ namespace Kernel::USB {
// that can fit in a single page.
template<typename T>
class UHCIDescriptorPool {
AK_MAKE_NONCOPYABLE(UHCIDescriptorPool);
AK_MAKE_NONMOVABLE(UHCIDescriptorPool);
// Ensure that we can't get into a situation where we'll write past the page
// and blow up
static_assert(sizeof(T) <= PAGE_SIZE);
public:
static OwnPtr<UHCIDescriptorPool<T>> try_create(const StringView name)
static KResultOr<NonnullOwnPtr<UHCIDescriptorPool<T>>> try_create(StringView name)
{
auto pool_memory_block = MM.allocate_kernel_region(PAGE_SIZE, "UHCI Descriptor Pool", Memory::Region::Access::ReadWrite);
if (!pool_memory_block)
return {};
return ENOMEM;
return adopt_own_if_nonnull(new (nothrow) UHCIDescriptorPool(pool_memory_block.release_nonnull(), name));
return adopt_nonnull_own_or_enomem(new (nothrow) UHCIDescriptorPool(pool_memory_block.release_nonnull(), name));
}
UHCIDescriptorPool(NonnullOwnPtr<Memory::Region> pool_memory_block, const StringView& name)
: m_pool_name(name)
, m_pool_region(move(pool_memory_block))
{
// Go through the number of descriptors to create in the pool, and create a virtual/physical address mapping
for (size_t i = 0; i < PAGE_SIZE / sizeof(T); i++) {
auto placement_address = reinterpret_cast<void*>(m_pool_region->vaddr().get() + (i * sizeof(T)));
auto physical_address = static_cast<u32>(m_pool_region->physical_page(0)->paddr().get() + (i * sizeof(T)));
auto* object = new (placement_address) T(physical_address);
m_free_descriptor_stack.push(object); // Push the descriptor's pointer onto the free list
}
}
public:
UHCIDescriptorPool() = delete;
~UHCIDescriptorPool() = default;
[[nodiscard]] T* try_take_free_descriptor()
@ -78,6 +65,19 @@ public:
}
private:
UHCIDescriptorPool(NonnullOwnPtr<Memory::Region> pool_memory_block, StringView name)
: m_pool_name(name)
, m_pool_region(move(pool_memory_block))
{
// Go through the number of descriptors to create in the pool, and create a virtual/physical address mapping
for (size_t i = 0; i < PAGE_SIZE / sizeof(T); i++) {
auto placement_address = reinterpret_cast<void*>(m_pool_region->vaddr().get() + (i * sizeof(T)));
auto physical_address = static_cast<u32>(m_pool_region->physical_page(0)->paddr().get() + (i * sizeof(T)));
auto* object = new (placement_address) T(physical_address);
m_free_descriptor_stack.push(object); // Push the descriptor's pointer onto the free list
}
}
StringView m_pool_name; // Name of this pool
NonnullOwnPtr<Memory::Region> m_pool_region; // Memory region where descriptors actually reside
Stack<T*, PAGE_SIZE / sizeof(T)> m_free_descriptor_stack; // Stack of currently free descriptor pointers