From 54cc80f649daf271910184af1409bd9840ff1658 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 16 Aug 2023 09:15:00 +0200 Subject: [PATCH] kernel/ATA: Replace a manually allocated buffer with Buffer This also lets us keep it empty unless it is used, in which case we resize it (if it wasn't already resized). --- kernel/src/arch/x86_64/disk/ATA.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/src/arch/x86_64/disk/ATA.cpp b/kernel/src/arch/x86_64/disk/ATA.cpp index f9343190..e759f533 100644 --- a/kernel/src/arch/x86_64/disk/ATA.cpp +++ b/kernel/src/arch/x86_64/disk/ATA.cpp @@ -6,6 +6,7 @@ #include "fs/MBR.h" #include "memory/MemoryManager.h" #include +#include #include #include #include @@ -758,9 +759,7 @@ Result ATADevice::read(u8* buf, usize offset, usize size) const ScopedKMutexLock<100> lock(m_drive->channel()->lock()); - // FIXME: Don't always allocate this if we don't need it. - auto* temp = (u8*)TRY(malloc_impl(block_size)); - auto guard = make_scope_guard([temp] { free_impl(temp); }); + Buffer temp; if (offset % block_size) { @@ -769,8 +768,10 @@ Result ATADevice::read(u8* buf, usize offset, usize size) const // Maybe we don't even want enough to get to the next block? if (extra_size > size) extra_size = size; - TRY(m_drive->read_lba(offset / block_size, temp, 1)); - memcpy(buf, temp + (offset % block_size), extra_size); + TRY(temp.try_resize(block_size)); + + TRY(m_drive->read_lba(offset / block_size, temp.data(), 1)); + memcpy(buf, temp.data() + (offset % block_size), extra_size); offset += extra_size; size -= extra_size; buf += extra_size; @@ -794,8 +795,9 @@ Result ATADevice::read(u8* buf, usize offset, usize size) const if (size) { - TRY(m_drive->read_lba(offset / block_size, temp, 1)); - memcpy(buf, temp, size); + TRY(temp.try_resize(block_size)); + TRY(m_drive->read_lba(offset / block_size, temp.data(), 1)); + memcpy(buf, temp.data(), size); } return length;