Kernel: DiskCache won't crash when running out of kmalloc memory

This commit is contained in:
Bananymous 2023-06-03 02:36:20 +03:00
parent 59b807189f
commit 884d986bd6
1 changed files with 20 additions and 16 deletions

View File

@ -55,15 +55,17 @@ namespace Kernel
} }
// We try to allocate new cache block for this sector // We try to allocate new cache block for this sector
TRY(m_cache.emplace_back()); if (!m_cache.emplace_back().is_error())
if (paddr_t paddr = Heap::get().take_free_page())
{ {
auto& cache_block = m_cache.back(); if (paddr_t paddr = Heap::get().take_free_page())
cache_block.paddr = paddr; {
cache_block.write_sector(m_device, 0, buffer); auto& cache_block = m_cache.back();
cache_block.sectors[0].sector = sector; cache_block.paddr = paddr;
cache_block.sectors[0].dirty = false; cache_block.write_sector(m_device, 0, buffer);
return {}; cache_block.sectors[0].sector = sector;
cache_block.sectors[0].dirty = false;
return {};
}
} }
// We could not cache the sector // We could not cache the sector
@ -106,15 +108,17 @@ namespace Kernel
} }
// We try to allocate new cache block // We try to allocate new cache block
TRY(m_cache.emplace_back()); if (!m_cache.emplace_back().is_error())
if (paddr_t paddr = Heap::get().take_free_page())
{ {
auto& cache_block = m_cache.back(); if (paddr_t paddr = Heap::get().take_free_page())
cache_block.paddr = paddr; {
cache_block.write_sector(m_device, 0, buffer); auto& cache_block = m_cache.back();
cache_block.sectors[0].sector = sector; cache_block.paddr = paddr;
cache_block.sectors[0].dirty = true; cache_block.write_sector(m_device, 0, buffer);
return {}; cache_block.sectors[0].sector = sector;
cache_block.sectors[0].dirty = true;
return {};
}
} }
// We could not allocate cache, so we must sync it to disk // We could not allocate cache, so we must sync it to disk