Skip to content
This repository was archived by the owner on Dec 25, 2025. It is now read-only.

Commit 426aba6

Browse files
committed
Cache the last used node for faster allocations in VMA
1 parent 4b1116e commit 426aba6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

kernel/include/memory/vma.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class VirtualMemoryAllocator {
8888

8989
SpinLock lock;
9090
VmRegion* root;
91+
VmRegion* cached_cursor = nullptr;
9192
VmRegionAllocator metadata_allocator;
9293
};
9394
} // namespace kernel::memory

kernel/src/memory/vma.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ VmRegion* VirtualMemoryAllocator::find_node(uintptr_t start) {
229229
}
230230

231231
uintptr_t VirtualMemoryAllocator::find_hole(size_t size, size_t alignment) {
232+
if (this->cached_cursor) {
233+
// Calculate where the allocation would start if placed after the cursor
234+
uintptr_t candidate = align_up(this->cached_cursor->end(), alignment);
235+
size_t overhead = candidate - this->cached_cursor->end();
236+
237+
// Does the gap after the cursor fit the request?
238+
if (this->cached_cursor->gap >= (size + overhead)) {
239+
// Voila! We found a spot without touching the tree.
240+
return candidate;
241+
}
242+
}
243+
232244
// Try to find a hole inside the tree
233245
uintptr_t found = this->find_hole(this->root, size, alignment);
234246
if (found != 0) {
@@ -284,7 +296,7 @@ uintptr_t VirtualMemoryAllocator::find_hole(VmRegion* node, size_t size, size_t
284296
return this->find_hole(node->right, size, alignment);
285297
}
286298

287-
// We failed, Mr. Stark.
299+
// We failed, Mr. Stark
288300
return 0;
289301
}
290302

@@ -365,6 +377,8 @@ void VirtualMemoryAllocator::insert_region_locked(uintptr_t start, size_t size,
365377
}
366378
}
367379

380+
this->cached_cursor = z;
381+
368382
// Rebalance
369383
this->insert_fixup(z);
370384
}

0 commit comments

Comments
 (0)