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).
This commit is contained in:
apio 2023-08-16 09:15:00 +02:00
parent b8f81502b8
commit 54cc80f649
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -6,6 +6,7 @@
#include "fs/MBR.h"
#include "memory/MemoryManager.h"
#include <luna/Alignment.h>
#include <luna/Buffer.h>
#include <luna/CType.h>
#include <luna/SafeArithmetic.h>
#include <luna/Vector.h>
@ -758,9 +759,7 @@ Result<u64> 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<u64> 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<u64> 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;