mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:28:12 +00:00
Kernel: Support best fit allocation policy in kmalloc()
Add find_best_fit() which implements best fit allocation algorithm. Kmalloc now uses a best fit allocation policy for large allocations.
This commit is contained in:
parent
0d08ed2693
commit
c0a4cf5e8d
2 changed files with 39 additions and 1 deletions
|
@ -150,7 +150,16 @@ void* kmalloc_impl(size_t size)
|
|||
size_t chunks_needed = (real_size + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
||||
|
||||
Bitmap bitmap_wrapper = Bitmap::wrap(alloc_map, POOL_SIZE / CHUNK_SIZE);
|
||||
auto first_chunk = bitmap_wrapper.find_first_fit(chunks_needed);
|
||||
Optional<size_t> first_chunk;
|
||||
|
||||
// Choose the right politic for allocation.
|
||||
constexpr u32 best_fit_threshold = 128;
|
||||
if (chunks_needed < best_fit_threshold) {
|
||||
first_chunk = bitmap_wrapper.find_first_fit(chunks_needed);
|
||||
} else {
|
||||
first_chunk = bitmap_wrapper.find_best_fit(chunks_needed);
|
||||
}
|
||||
|
||||
if (!first_chunk.has_value()) {
|
||||
klog() << "kmalloc(): PANIC! Out of memory (no suitable block for size " << size << ")";
|
||||
Kernel::dump_backtrace();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue