mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:37:35 +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
29
AK/Bitmap.h
29
AK/Bitmap.h
|
@ -285,6 +285,35 @@ public:
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<size_t> find_best_fit(size_t minimum_length) const
|
||||||
|
{
|
||||||
|
size_t start = 0;
|
||||||
|
size_t best_region_start = 0;
|
||||||
|
size_t best_region_size = max_size;
|
||||||
|
bool found = false;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// Look for the next block which is bigger than requested length.
|
||||||
|
auto length_of_found_range = find_next_range_of_unset_bits(start, minimum_length, best_region_size);
|
||||||
|
if (length_of_found_range.has_value()) {
|
||||||
|
if (best_region_size > length_of_found_range.value() || !found) {
|
||||||
|
best_region_start = start;
|
||||||
|
best_region_size = length_of_found_range.value();
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
start += length_of_found_range.value();
|
||||||
|
} else {
|
||||||
|
// There are no ranges which can fit requested length.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
return best_region_start;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
Bitmap()
|
Bitmap()
|
||||||
: m_size(0)
|
: m_size(0)
|
||||||
, m_owned(true)
|
, m_owned(true)
|
||||||
|
|
|
@ -150,7 +150,16 @@ void* kmalloc_impl(size_t size)
|
||||||
size_t chunks_needed = (real_size + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
size_t chunks_needed = (real_size + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
||||||
|
|
||||||
Bitmap bitmap_wrapper = Bitmap::wrap(alloc_map, POOL_SIZE / 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()) {
|
if (!first_chunk.has_value()) {
|
||||||
klog() << "kmalloc(): PANIC! Out of memory (no suitable block for size " << size << ")";
|
klog() << "kmalloc(): PANIC! Out of memory (no suitable block for size " << size << ")";
|
||||||
Kernel::dump_backtrace();
|
Kernel::dump_backtrace();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue