From 1ece93c8055142293d5d9745bc26e27abd5c00c5 Mon Sep 17 00:00:00 2001 From: Tom Date: Tue, 1 Sep 2020 16:04:23 -0600 Subject: [PATCH] Kernel: Use removed memory as backup if backup hasn't been allocated It may be impossible to allocate more backup memory after expanding the heap if memory is running low. In that case we wouldn't allocate backup memory until trying to expand the heap again. But we also wouldn't take advantage of using removed memory as backup, which means that no backup memory would be available when the heap needs to grow again, causing subsequent expansion to fail because there is no backup memory. --- Kernel/Heap/kmalloc.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Kernel/Heap/kmalloc.cpp b/Kernel/Heap/kmalloc.cpp index be29e0f5aa..2e83579429 100644 --- a/Kernel/Heap/kmalloc.cpp +++ b/Kernel/Heap/kmalloc.cpp @@ -114,6 +114,10 @@ struct KmallocGlobalHeap { if (m_global_heap.m_subheap_memory[i].vaddr().as_ptr() == memory) { auto region = m_global_heap.m_subheap_memory.take(i); klog() << "kmalloc(): Removing memory from heap at " << region->vaddr() << ", bytes: " << region->size(); + if (!m_global_heap.m_backup_memory) { + klog() << "kmalloc(): Using removed memory as backup: " << region->vaddr() << ", bytes: " << region->size(); + m_global_heap.m_backup_memory = move(region); + } return true; } }